I am using the function cv.matchTemplate
to try to find template matches.
result = cv.matchTemplate(img, templ, match_method)
After I run the function I have a bunch of answers in list result
. I want to filter the list to find the best n matches. The data in result
just a large array of numbers so I don't know what criteria to filter based on. Using extremes = cv.minMaxLoc(result, None)
filters the result list in an undesired way before converting them to locations.
The match_method is cv.TM_SQDIFF
. I want to:
How can I acheive this?
You can treshold the result of matchTemplate to find locations with sufficient match. This tutorial should get you started. Read at the bottom of the page for finding multiple matches.
import numpy as np
threshold = 0.2
loc = np.where( result <= threshold) # filter the results
for pt in zip(*loc[::-1]): #pt marks the location of the match
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
Keep in mind depending on the function you use will determine how you filter. cv.TM_SQDIFF
tends to zero as the match quality increases so setting the threshold
closer to zero filters out worse. The opposite is true for cv.TM CCORR
cv.TM_CCORR_NORMED
cv.TM_COEFF
and cv.TM_COEFF_NORMED
matching methods (better tends to 1)