栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【python】使用opencv识别滑动验证码的缺口

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【python】使用opencv识别滑动验证码的缺口

安装opencv-python 高斯滤波 GaussianBlur 需传入ksize和sigmaX 边缘检测 Canny需传入threshold1和threshold2两个阈值 轮廓提取 findContours需传入image,mode,method 外接矩形 boundingRect 传入一个array 轮廓面积 contourArea,需传contour轮廓信息,oriented方向标识符 ,返回轮廓面积 轮廓周长arcLength,需传入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):
    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


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)  # 获取轮廓信息


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


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)


识别过程中图片:


最后返回偏移量offset 163

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/953766.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号