Simply put: findimage The find_template
and find_all_template
project have added a parameter: autoscale
.
indicates whether to automatically scale im_template to find a match. If it is None, it means no scaling. If scaling is required, pass a tuple: (min_scale, max_scale, step), where min_scale and max_scale are the lower and upper limits of the scaling factor, both of which are decimals , min_scale is between 0~1, max_scale is greater than 1, step indicates the step size from min to max, the default is 0.1.
Example
Target
Still in the image below, look for the symbol '#':
The incoming template image is:
method
It can be seen that it is obviously larger than the # in the source image. If it matches directly, the result will not be found, but after using the autoscale parameter:
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)
result
You can see the search result image:
All #s are found. If we look at the console output:
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}
It can be seen that when zoomed to 0.7 times, the search results are output, and the matching degree of each position is greater than 0.9.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。