Cv2识别无原图滑动验证码

import cv2

def get_px(target_pic_path,template_pic_path):
    target_img = cv2.imread(target_pic_path, 0)
    template_img = cv2.imread(template_pic_path, 0)
    w, h = target_img.shape[::-1]
    # 6种方法
    # methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
    #            'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
    meth = 'cv2.TM_CCOEFF'
    '''
    exec可以用来执行储存在字符串货文件中的python语句
    例如可以在运行时生成一个包含python代码的字符串
    然后使用exec语句执行这些语句
    eval语句用来计算存储在字符串中的有效python表达式
    '''
    method = eval(meth)
    # 匹配应用
    res = cv2.matchTemplate(target_img, template_img, method)
    mn_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    # 使用不同的方法,对结果的解释不同
    # 方法判断
    # if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
    #     top_left = min_loc
    # else:
    #     top_left = max_loc
    top_left = max_loc

    return {'偏移像素':top_left[0],"偏移百分比":f"{top_left[0]/w*100}%"}

 

posted @ 2021-12-27 18:10  日天达人  阅读(102)  评论(0编辑  收藏  举报