乐趣区

关于python:findimage-支持自动缩放了

简略说:findimage 我的项目的 find_templatefind_all_template办法,都新增了一个参数:autoscale

示意是否主动缩放 im_template 来查找匹配,如果为 None 示意不缩放,如果须要缩放,那么传一个 tuple:(min_scale, max_scale, step),其中 min_scale 和 max_scale 别离是缩放倍数的上限和下限,都是小数,min_scale 介于 0~1 之间,max_scale 大于 1, step 示意从 min 尝试到 max 之间的步长, 默认为 0.1。

示例

指标

还是在下图中,查找符号 ’#’:

传入的模板图是:

办法

能够看出显著比源图中的 #要大,如果间接匹配,是找不到后果的,但应用 autoscale 参数之后:

from cv2 import cv2
import time

from findimage import find_all_template

image_origin = cv2.imread('seg_course_menu.png')
image_template = cv2.imread('seg_sharp_resize_1.5.png')

start_time = time.time()
match_results = find_all_template(image_origin, image_template, threshold=0.8, auto_scale=(0.6, 1.2), debug=True)
print("total time: {}".format(time.time() - start_time))

img_result = image_origin.copy()
for match_result in match_results:
    rect = match_result['rectangle']
    cv2.rectangle(img_result, (rect[0][0], rect[0][1]), (rect[3][0], rect[3][1]), (0, 0, 220), 2)
    print(match_result)
cv2.imwrite('result.png', img_result)

后果

能够看到查找后果图像:

所有的 #都被找到了。如果咱们查看控制台输入:

try resize template in scale 0.7 to find match
matchTemplate time: 0.004000186920166016
find max time: 0.0009999275207519531
found 7 results, top confidence is:0.9912415146827698
total time: 0.05300307273864746
{'result': (45.5, 266.5), 'rectangle': ((36, 257), (36, 276), (55, 257), (55, 276)), 'confidence': 0.9912415146827698}
{'result': (45.5, 146.5), 'rectangle': ((36, 137), (36, 156), (55, 137), (55, 156)), 'confidence': 0.9912384152412415}
{'result': (45.5, 226.5), 'rectangle': ((36, 217), (36, 236), (55, 217), (55, 236)), 'confidence': 0.9912384152412415}
{'result': (45.5, 306.5), 'rectangle': ((36, 297), (36, 316), (55, 297), (55, 316)), 'confidence': 0.9912353157997131}
{'result': (45.5, 346.5), 'rectangle': ((36, 337), (36, 356), (55, 337), (55, 356)), 'confidence': 0.9912353157997131}
{'result': (45.5, 186.5), 'rectangle': ((36, 177), (36, 196), (55, 177), (55, 196)), 'confidence': 0.99123215675354}
{'result': (45.5, 386.5), 'rectangle': ((36, 377), (36, 396), (55, 377), (55, 396)), 'confidence': 0.99123215675354}

能够看到是在缩放到 0.7 倍的时候,输入了查找后果,并且每个地位的匹配度都大于 0.9。

退出移动版