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}%"}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章