python 爬虫系列教程(8.2)--- 使用OpenCV识别滑动验证码的缺口 - 85 * image_width
python 爬虫系列教程(8.2)--- 使用OpenCV识别滑动验证码的缺口
1. 滑动验证码简介
滑动验证码是一种常见的安全验证手段,用于区分用户是自动化脚本还是真实人类。代表性的服务商包括极验、网易易盾等。用户需要拖动滑块填充拼图,完成验证。
2. 基本原理
本节介绍使用OpenCV技术识别滑动验证码缺口的方法。输入一张带有缺口的验证码图片,输出标明缺口位置的图片。
2.1 处理步骤
高斯模糊滤波处理:使用高斯滤波去除噪声,为边缘检测做准备。 边缘检测:使用Canny算法识别图片中滑块的边缘。 轮廓提取:通过findContours方法提取边缘轮廓。 外接矩形计算:使用boundingRect计算轮廓的外接矩形。 轮廓面积和周长计算:使用contourArea和arcLength方法筛选出缺口位置。
3. 准备工作
确保安装了python-opencv库。
pip3 install python-opencv
准备一张滑动验证码图片。
4. OpenCV基础知识
4.1 高斯模糊
高斯滤波用于去除图片中的噪声。
使用GaussianBlur方法实现。 参数:src(源图像),ksize(高斯核大小),sigmaX(X方向上的标准偏差)。
import cv2
def GaussianBlur(image, ksize=(5, 5), sigmaX=0):
return cv2.GaussianBlur(image, ksize, sigmaX)
4.2 边缘检测
Canny算法用于检测图片中的边缘。
使用Canny算法,通过调整阈值识别边缘。 使用Canny方法,参数包括image(源图像),threshold1和threshold2(阈值)。
def Canny(image, threshold1=200, threshold2=450):
return cv2.Canny(image, threshold1, threshold2)
4.3 轮廓提取
提取边缘的轮廓。
使用findContours方法提取边缘轮廓。 参数:image(源图像),mode(轮廓检索模式),method(轮廓近似方法)。
def findContours(image):
contours, _ = cv2.findContours(image, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
return contours
4.4 外接矩形
计算轮廓的外接矩形。
使用boundingRect计算轮廓的外接矩形。 参数:array(轮廓信息)。
def boundingRect(contour):
return cv2.boundingRect(contour)
4.5 轮廓面积
使用contourArea和arcLength方法计算轮廓的面积和周长。 计算轮廓的面积。
def contourArea(contour):
return cv2.contourArea(contour)
4.6 轮廓周长
计算轮廓的周长。
def arcLength(curve, closed=True):
return cv2.arcLength(curve, closed)
5. 缺口识别算法
5.1 高斯滤波、边缘检测和轮廓提取
image_raw = cv2.imread('captcha.png') 拼多多帮助力入口
image_height, image_width = image_raw.shape
image_gaussian_blur = GaussianBlur(image_raw)
image_canny = Canny(image_gaussian_blur)
contours = findContours(image_canny)
5.2 定义筛选条件
定义了三个方法来获取面积、周长和偏移量阈值:get_contour_area_threshold,get_arc_length_threshold,get_offset_threshold。
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) * 0.8
arc_length_max = (image_width * 0.15) + (image_height * 0.25) * 2.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
5.3 筛选目标轮廓
遍历轮廓信息,根据条件筛选出目标轮廓。
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:
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)
6. 总结
本节介绍了利用OpenCV技术识别滑动验证码缺口的方法,涉及的关键图像处理技术包括高斯滤波、边缘检测、轮廓提取等。这些技术可以应用于其他图像识别任务。
文章持续跟新,可以微信搜一搜公众号 [ rain雨雨编程 ],或扫描下方二维码,第一时间阅读,涉及爬虫,机器学习,Java编程,实战项目等。
python 爬虫系列教程(8.2)--- 使用OpenCV识别滑动验证码的缺口
相关阅读