pythonimage-processingopencvtemplate-matching

Get confidence of template matching for object with multiple instances by using cv2.TM_CCOEFF_NORMED


I'm using template matching for searching for an object with multiple instances.

I'm referring to the tutorial in https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_template_matching/py_template_matching.html#template-matching-with-multiple-objects

This is the code which I use currently

import cv2
import numpy as np
from matplotlib import pyplot as plt

img_rgb = cv2.imread('mario.png')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('mario_coin.png',0)
w, h = template.shape[::-1]

res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
    cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)

cv2.imwrite('res.png',img_rgb)

Here, they are using np.where(res>=threshold) to filter the elements with confidence greater than the given threshold.

How should I modify the code to get the confidence value for each match found in loc? so the ideal result I want is like this

for match in matches:
   x,y,w,h,confidence=match
   print(x,y,w,h,confidence)

In template matching for a single instance, we can use cv2.minMaxLoc(res) to get the confidence, but how to do that for each match in multiple instances?

Sample input image:

input mario

Sample template: template


Solution

  • The "res" variable contains the confidence of all the points in the image except the points near the right and bottom boundary. Confidence of a point means the confidence of the rectangle whose top-left corner is at that point and whose width and height are the same as the template image's width and height.

    Thus to get the confidence of each match found, inside the for loop, add a line:

    confidence = res[pt[1]][pt[0]]
    

    The "confidence" variable will contain the confidence of that match.