使用OpenCV识别滑动验证码缺口

使用OpenCV识别滑动验证码验证码的缺口

随着互联网技术的发展,各种新型验证码层出不穷,最具有代表性的便是滑动验证码。本文就介绍一个简易的利用图像处理技术识别滑动验证码缺口的方法。

1.滑动验证码

如果我们想用爬虫自动化完成这一流程,关键步骤有两个:

(1)识别目标缺口的位置
(2)将滑块拖到缺口位置

2.基本原理

具体的步骤为:
(1)对验证码图片进行高斯模糊滤波处理,消除部分噪声干扰;

(2)利用边缘检测算法,通过调整相应阈值识别出验证码图片中滑块的边缘;

(3)基于上一步得到的各个边缘轮廓信息,对比面积、位置、周长等特征,筛选出最可能的轮廓,得到缺口位置

3.准备工作

确保已经安装好了opencv-python库,安装方式如下:

1
pip3 install opencv-python

准备一张如下的测试图片:
滑动验证码

4.基础知识

1.高斯滤波

高斯滤波用来去除图片中的一些噪声,减少噪声干扰,把一张图片模糊化,为边缘检测做好准备。
OpenCV提供了一个用于实现高斯模糊的方法,叫做GaussianBlur,其声明如下:

1
def GaussianBlur(src, ksize, sigmaX, dst=None, sigmaY=None, borderType=None)

2.边缘检测

目前应用比较广泛的边缘检测算法是Canny,OpenCV也实现了算法,方法名就叫Canny,其声明如下:

1
def Canny(image, threshold1, threshold2, edges=None, apertureSize=None, L2gradient=None)

5.缺口识别

具体代码实现如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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):
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):
contours,_ = cv2.findContours(image,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
return contours

# 定义目标轮廓的面积上限和面积下线
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


# 读取原始图片
image_raw = cv2.imread('captcha.png')

# 进行高斯滤波、边缘检测,最后得到各个边缘的轮廓信息
image_height,image_width,_ = image_raw.shape
image_gaussian_blur = get_gaussian_blur_image(image_raw)
image_canny = get_canny_image(image_gaussian_blur)
contours = get_contours(image_canny)

# 调用三个方法判断阈值
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)

# 遍历阈值,对其进行筛选,最终得到x值就是目标缺口的偏移量
offset = None
for contour in contours:
x, y, w, h =cv2.boundingRect(contour)
if contour_area_min < cv2.contourArea(contour) < contour_area_max and arc_length_min < cv2.arcLength(contour, True) < arc_length_max and offset_min < x < offset_max:
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)
print(contours)

最后得到的目标图像为:
目的图片

6.总结

本文中我们介绍了OpenCV技术识别滑动验证码缺口的方法,其中涉及一些关键图像处理和识别技术——高斯滤波、边缘检测、轮廓提取等算法。


使用OpenCV识别滑动验证码缺口
http://zphxd.top/2023/03/01/使用OpenCV识别滑动验证码缺口/
作者
i1548708011
发布于
2023年3月1日
许可协议