def popup_sub_crop(): util.log_title('弹窗判断') shape_dict = {} for i in range(len(c.popup_flag_img_paths)): shape,score = template_match(c.popup_flag_img_paths[i],c.img_sc_path) shape_dict[shape] = (score,i) print(shape_dict) max_shape = max(shape_dict, key=shape_dict.get) score,i = shape_dict[max_shape] print(f'最大区域 {max_shape} 最终得分为 {score}' ) if score >=3 : sub_shape = ( max_shape[0]+c.popup_move_shapes[i][0], max_shape[1]+c.popup_move_shapes[i][1], max_shape[2]+c.popup_move_shapes[i][2], max_shape[3]+c.popup_move_shapes[i][3] ) print(f'弹框区域 {sub_shape}') return crop(c.img_sc_path,c.popup_sub_img_path,sub_shape) print(f'没有弹框') return False
def template_match(template_path,src_path):
img = cv.imread(src_path,0) img2 = img.copy() template = cv.imread(template_path,0) w, h = template.shape[::-1] methods = ['cv.TM_CCOEFF', 'cv.TM_CCOEFF_NORMED','cv.TM_CCORR', 'cv.TM_CCORR_NORMED', 'cv.TM_SQDIFF', 'cv.TM_SQDIFF_NORMED'] shape_dict = {} for meth in methods: img = img2.copy() method = eval(meth) # Apply template Matching res = cv.matchTemplate(img,template,method) min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res) if method in [cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED]: top_left = min_loc else: top_left = max_loc bottom_right = (top_left[0] + w, top_left[1] + h) # cv.rectangle(img,top_left, bottom_right, 255, 2) # plt.subplot(121),plt.imshow(res,cmap = 'gray') # plt.title('Matching Result'), plt.xticks([]), plt.yticks([]) # plt.subplot(122),plt.imshow(img,cmap = 'gray') # plt.title('Detected Point'), plt.xticks([]), plt.yticks([]) # plt.suptitle(meth) # plt.show() shape = (top_left[0],top_left[1],bottom_right[0],bottom_right[1]) # print(shape) if shape_dict.get(shape) == None: shape_dict[shape] = 1; else: shape_dict[shape] = shape_dict[shape]+1 max_shape = max(shape_dict, key=shape_dict.get) return max_shape,shape_dict[max_shape]
|