How to interpret matchtemplate output? (openCV, Python)

After reading the documents and searching all over the Internet, I still don’t understand how to interpret the output of the matchTemplate function from openCV.

What I understand:

result = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)

I understand that I am getting a matrix view with an appropriate value for each part of the image. Each element in this matrix determines how similar it is to a template.

eg. I can filter out all locations with an appropriate value below 0.7 with

numpy.where(result >= 0.7)

What I do not understand is how this information is stored in the output that I get from the matchTemplate function, and how the position of the match can be extracted from the output.

Basically, what I want to do is map several templates to one image, and then determine which template is best suited for this location (it has the maximum match between the values ​​of all applicable templates for the location).

My idea is to extract the corresponding value into the matrix for each template, and then compare the matrices (their elements) with each other to find the best match.

Thanks for the help and please correct me where I am wrong,

Cheers Don

+4
source share
1 answer

You can use the following code:

min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)

When using cv2.TM_CCOEFF_NORMED max_loc will be the location of the template in your img. And max_val will be a correlation of correspondence

0
source

All Articles