I use the img.find_template function to find the template, but when looking for multiple templates, the speed will drop, is there any way to improve the efficiency of multiple templates?
Have you tried diamond search ?
Yes, I use the box to find, the code is as follows, I want to know how to improve the efficiency and speed of the search under multiple templates.
r1 = img.find_template(template1, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
r2 = img.find_template(template2, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
r3 = img.find_template(template3, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
r4 = img.find_template(template4, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
r5 = img.find_template(template5, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
r6 = img.find_template(template6, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
r7 = img.find_template(template7, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
if r1:
img.draw_rectangle(r1)
elif r2:
img.draw_rectangle(r2)
elif r3:
img.draw_rectangle(r3)
elif r4:
img.draw_rectangle(r4)
elif r5:
img.draw_rectangle(r5)
elif r6:
img.draw_rectangle(r6)
elif r7:
img.draw_rectangle(r7)
That’s the exhaustive search, try:
r1 = img.find_template(template1, search=SEARCH_DS)
Hi!
I don’t understand the error (OSError: Could not find the path),
The files are located on the camera…(template = image.Image(“/1.pgm”))
Removing the leading slash.
Deleted…, now it complains about the line: (r = img.find_template(template, 0.70, step=4, search=SEARCH_EX) # , roi=(10, 0, 60, 60)))
TypeError: Can’t convert list to type
What’s the whole script?
template_matching_1.py
import time
import sensor
import image
from image import SEARCH_EX
sensor.reset()
sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.QQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)
template = image.Image("1.pgm")
clock = time.clock()
while True:
clock.tick()
img = sensor.snapshot()
r = img.find_template(template, 0.70, step=4, search=SEARCH_EX)
if r:
img.draw_rectangle(r)
print(clock.fps())
Please provide me the pgm image and what you are testing against, e.g. the frame buffer.
Problem solved..
The picture had no contrast..
Thank you very much!