目标:识别出图片目标缺口的位置,输入一张带有缺口的验证码图片,输出缺口的位置(一般为缺口左侧横坐标)。
输入:
输出:
利用 OpenCV 进行基本的图像处理来实现的,主要步骤包括:
一、高斯滤波:
OpenCV 提供了一个用于实现高斯模糊的方法,叫做 GaussianBlur,经过高斯滤波处理后,图像会变得模糊
def GaussianBlur(src, ksize, sigmaX, dst=None, sigmaY=None, borderType=None)
#src:即需要被处理的图像。
#ksize:进行高斯滤波处理所用的高斯内核大小,它需要是一个元组,包含 x 和 y 两个维度。
#sigmaX:表示高斯核函数在 X 方向的的标准偏差。
#sigmaY:表示高斯核函数在 Y 方向的的标准偏差,若 sigmaY 为 0,就将它设为 sigmaX,如果 sigmaX 和 sigmaY 都是 0,那么sigmaX 和 sigmaY 就通过 ksize 计算得出。
#这里 ksize 和 sigmaX 是必传参数,对本节样例图片,ksize 我们可以取 (5, 5),sigmaX 可以取 0。
二、边缘检测:
边缘检测应用比较广泛的边缘检测算法是 Canny边缘检测算法,经过边缘检测算法处理后,一些比较明显的边缘信息会被保留下来
def Canny(image, threshold1, threshold2, edges=None, apertureSize=None, L2gradient=None)
#image:即需要被处理的图像。
#threshold1、threshold2:两个阈值,分别为最小和最大判定临界点。
#apertureSize:用于查找图像渐变的 Sobel 内核的大小。
#L2gradient:指定用于查找梯度幅度的等式。
#通常来说,我们只需要设定 threshold1 和 threshold2 即可,其数值大小需要视不同图像而定,比如本例图片可以分别取 200 和 450。
三、轮廓提取:
轮廓提:取用 OpenCV
将边缘轮廓提取出来,这里需要用到 findContours
方法(注意:这个地方返回的参数在OpenCV3
返回三个值:处理的图像(image)轮廓的点集(contours)各层轮廓的索引(hierarchy),与OpenCV2
返回俩个参数不同。)
def findContours(image, mode, method, contours=None, hierarchy=None, offset=None)
#image:即需要被处理的图像。
#mode:定义轮廓的检索模式,详情见 OpenCV 的 RetrievalModes 的介绍。
#method:定义轮廓的近似方法,详情见 OpenCV 的 ContourApproximationModes 的介绍。在这里,选取 mode 为 RETR_CCOMP,method 为 CHAIN_APPROX_SIMPLE,具体的选型标准可以参考 OpenCV 的文档介绍
四、外接矩形:
提取到轮廓之后,为了方便进行判定,可以将轮廓的外界矩形计算出来,这样方便我们根据面积、位置、周长等参数进行判定,以得出该轮廓是不是目标滑块的轮廓。计算外接矩形使用的方法是 boundingRect
,经过轮廓信息和外接矩形判定之后就能成功获取各个轮廓的外接矩形。
def boundingRect(array)
#array:可以是一个灰度图或者 2D 点集,这里可以传入轮廓信息。
五、轮廓面积:
已经得到了各个外接矩形,但是很明显有些矩形不是我们想要的,可以根据面积、周长等来进行筛选,这里就需要用到计算面积的方法,叫做 contourArea
,返回结果就是轮廓的面积。
def contourArea(contour, oriented=None)
#contour:轮廓信息。
#oriented:面向区域标识符。有默认值 False。若为 True,该函数返回一个带符号的面积值,正负取决于轮廓的方向(顺时针还是逆时针)。若为 False,表示以绝对值返回。
六、轮廓周长:
周长的计算也有对应的方法,叫做 arcLength
,返回结果就是轮廓的周长。
def arcLength(curve, closed)
#curve:轮廓信息。
#closed:表示轮廓是否封闭。
最后附上源码:
import cv2
GAUSSIAN_BLUR_KERNEL_SIZE = (5, 5)
GAUSSIAN_BLUR_SIGMA_X = 0
CANNY_THRESHOLD1 = 200
CANNY_THRESHOLD2 = 450
def get_gaussian_blur_image(image):
#def GaussianBlur(src, ksize, sigmaX, dst=None, sigmaY=None, borderType=None)
#src:即需要被处理的图像。ksize:进行高斯滤波处理所用的高斯内核大小,它需要是一个元组,包含 x 和 y 两个维度
#sigmaX:表示高斯核函数在 X 方向的的标准偏差。
#sigmaY:表示高斯核函数在 Y 方向的的标准偏差,若 sigmaY 为 0,就将它设为 sigmaX,如果 sigmaX 和 sigmaY 都是 0,那么 sigmaX 和 sigmaY 就通过 ksize 计算得出
return cv2.GaussianBlur(image, GAUSSIAN_BLUR_KERNEL_SIZE, GAUSSIAN_BLUR_SIGMA_X)
def get_canny_image(image):
return cv2.Canny(image, CANNY_THRESHOLD1, CANNY_THRESHOLD2)
def get_contours(image):
#OpenCV3中:则会返回三个值:处理的图像(image)轮廓的点集(contours)各层轮廓的索引(hierarchy)
image,contours,hierarchy = cv2.findContours(image, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
return contours
#需要先确定怎么来筛选,比如面积我们可以设定一个范围,周长设定一个范围,缺口位置设定一个范围,
#通过实际测量,可以得出目标缺口的外接矩形的高度大约是验证码高度的 0.25 倍,宽度大约是验证码宽度的 0.15 倍。
#在允许误差 20% 的情况下,根据验证码的宽高信息我们大约可以计算出面积、周长的范围,同时缺口位置(缺口左侧)也有一个最小偏移值,比如最小偏移是验证码宽度的 0.2 倍,最大偏移是验证码宽度的 0.85 倍。综合这些内容,定义三个阈值方法
def get_contour_area_threshold(image_width, image_height):
#定义目标轮廓的下限和上限面积
contour_area_min = (image_width * 0.15) * (image_height * 0.25) * 0.8
contour_area_max = (image_width * 0.15) * (image_height * 0.25) * 1.2
return contour_area_min, contour_area_max
def get_arc_length_threshold(image_width, image_height):
#定义目标轮廓的下限和上限周长
arc_length_min = ((image_width * 0.15) + (image_height * 0.25)) * 2 * 0.8
arc_length_max = ((image_width * 0.15) + (image_height * 0.25)) * 2 * 1.2
return arc_length_min, arc_length_max
def get_offset_threshold(image_width):
#定义目标轮廓左侧的下限和上限偏移量
offset_min = 0.2 * image_width
offset_max = 0.85 * image_width
return offset_min, offset_max
def main():
#读取原图
image_raw = cv2.imread('captcha.png')
image_height, image_width, _ = image_raw.shape
print(image_height,image_width,_)#320 520 3
#高斯滤波,把一张图像变得模糊化,减少一些图像噪声干扰
image_gaussian_blur = get_gaussian_blur_image(image_raw)
cv2.imwrite('image_gaussian_blur.png', image_gaussian_blur)
#Canny边缘检测
image_canny = get_canny_image(image_gaussian_blur)
cv2.imwrite('image_canny.png', image_canny)
#轮廓提取
contours = get_contours(image_canny)
# cv2.imwrite('image_contours.png', contours)
contour_area_min, contour_area_max = get_contour_area_threshold(image_width, image_height)
arc_length_min, arc_length_max = get_arc_length_threshold(image_width, image_height)
offset_min, offset_max = get_offset_threshold(image_width)
offset = None
for contour in contours:
#print(contour)
x, y, w, h = cv2.boundingRect(contour)
if arc_length_min < cv2.arcLength(contour, True) < arc_length_max and\
contour_area_min < cv2.contourArea(contour) < contour_area_max \
and offset_min < x < offset_max:
#同时目标缺口的外接矩形调用了 rectangle 方法进行了标注
cv2.rectangle(image_raw, (x, y), (x + w, y + h), (0, 0, 255), 2)
offset = x
cv2.imwrite('image_label.png', image_raw)
print('offset', offset)#成功提取出来了目标滑块的位置
if __name__ == '__main__':
main()
OpenCV利用cv2.rectangle()绘制矩形框