随笔 - 31  文章 - 0  评论 - 0  阅读 - 2429

<2> 滑块验证码距离识别

复制代码
import io
import cv2
import numpy as np
from PIL import Image


class DetectDistanceXY:

    def detectDistanceX(self, imgSlider, imgBackground):
        """X坐标缺口检测"""

        # imgSlider = cv2.cvtColor(np.array(Image.open(io.BytesIO(imgSlider))), cv2.COLOR_BGR2GRAY)
        # imgBackground = cv2.cvtColor(np.array(Image.open(io.BytesIO(imgBackground))), cv2.COLOR_BGR2GRAY)

        imgSliderGray = cv2.cvtColor(np.array(Image.open(imgSlider)), cv2.COLOR_BGR2GRAY)
        imgBackgroundGray = cv2.cvtColor(np.array(Image.open(imgBackground)), cv2.COLOR_BGR2GRAY)
        # 寻找最佳匹配
        res = cv2.matchTemplate(self._tran_canny(imgSliderGray), self._tran_canny(imgBackgroundGray), cv2.TM_CCOEFF_NORMED)
        # 最小值,最大值,并得到最小值, 最大值的索引
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

        return max_loc[0]

    def detectDistanceY(self, imgSlider, imgBackground):
 
# 传入二进制图片 # target_gray = cv2.cvtColor(np.array(Image.open(io.BytesIO(imgSlider))), cv2.COLOR_BGR2GRAY) # template_gray = cv2.cvtColor(np.array(Image.open(io.BytesIO(imgBackground))), cv2.COLOR_BGR2GRAY) # 根据路径读取图片 imgSliderGray = cv2.cvtColor(np.array(Image.open(imgSlider)), cv2.COLOR_BGR2GRAY) imgBackgroundGray = cv2.cvtColor(np.array(Image.open(imgBackground)), cv2.COLOR_BGR2GRAY) res = cv2.matchTemplate(imgSliderGray, imgBackgroundGray, cv2.TM_CCOEFF_NORMED) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) return max_loc[0] def _tran_canny(self, image): """消除噪声""" image = cv2.GaussianBlur(image, (3, 3), 0) return cv2.Canny(image, 100, 200) if __name__ == '__main__': detectdistancexy = DetectDistanceXY() targetFile = r'target.png' templateFile = r'template.png' print(detectdistancexy.detectDistanceX(targetFile, templateFile)) print(detectdistancexy.detectDistanceY(targetFile, templateFile))
复制代码

 

posted on   不是霉蛋  阅读(93)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示