关于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。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理