python验证码滑块图像识别

文章目录

  • 1、案例图片
  • 1、需求说明
  • 2、代码实现
  • 总结

  • 1、案例图片

    1、需求说明

    python 3.10,写一个滑块验证码的自动化程序。需要一个opencv的函数,能准确的计算,在这同一张图片上,滑块形状和缺口形状的坐标位置及两个形状之间在X轴上的距离。请注意,没有单独的滑块图片,而是滑块和缺口都在同一张图片上,没有分开,补充:并不存在滑块和背景两张图,滑块和缺口都在一张图上。不能用额外的滑块图片匹配,而是要在一张图上,找出两个形状一样的图,并计算坐标距离。

    2、代码实现

    import cv2
    import numpy as np
     
    def find_slider_and_gap(image_path):
        # 读取图片
        image = cv2.imread(image_path)
        if image is None:
            raise ValueError("Image not found or unable to read")
     
        # 转换为灰度图像
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
     
        # 使用高斯模糊减少噪声
        blurred = cv2.GaussianBlur(gray, (5, 5), 0)
     
        # 使用Canny边缘检测
        edges = cv2.Canny(blurred, 50, 150)
     
        # 使用轮廓检测找到滑块和缺口
        contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
     
        # 找到最大的两个轮廓(假设滑块和缺口是最明显的两个轮廓)
        contours = sorted(contours, key=cv2.contourArea, reverse=True)[:2]
     
        if len(contours) < 2:
            raise ValueError("Not enough contours found")
     
        # 计算轮廓的边界框
        slider_box = cv2.boundingRect(contours[0])
        gap_box = cv2.boundingRect(contours[1])
     
        # 计算滑块和缺口的中心点
        slider_center = (slider_box[0] + slider_box[2] // 2, slider_box[1] + slider_box[3] // 2)
        gap_center = (gap_box[0] + gap_box[2] // 2, gap_box[1] + gap_box[3] // 2)
     
        # 计算滑块和缺口在X轴上的距离
        distance_x = abs(slider_center[0] - gap_center[0])
     
        # 绘制边界框和中心点
        cv2.rectangle(image, slider_box, (0, 255, 0), 2)
        cv2.rectangle(image, gap_box, (0, 0, 255), 2)
        cv2.circle(image, slider_center, 5, (0, 255, 0), -1)
        cv2.circle(image, gap_center, 5, (0, 0, 255), -1)
     
        # 显示结果图像
        cv2.imshow('Result', image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
     
        return slider_center, gap_center, distance_x
     
    # 示例使用
    image_path = 'path_to_your_image.png'
    slider_center, gap_center, distance_x = find_slider_and_gap(image_path)
    print(f"滑块中心坐标: {slider_center}")
    print(f"缺口中心坐标: {gap_center}")
    print(f"滑块和缺口在X轴上的距离: {distance_x}")
     
    

    总结

    这是我在社区问答中看到的一个问题,本人给出了回答,也很荣幸被题主采纳,这里再次记录一下,希望更多人看到。

    作者:凌霜残雪

    物联沃分享整理
    物联沃-IOTWORD物联网 » python验证码滑块图像识别

    发表回复