james joyce
james joyce

Reputation: 493

how to find the template matching accuracy

i am doing a template matching
now,what i want to do is find the accuracy of template matching
I have done template matching, but how do i get the accuracy i think i have to subtract the matched region and template image. how do i achieve this

CODE

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread('image.jpg',0)
img1 = img.copy()
template = cv.imread('template.jpg',0)
w, h = template.shape[::-1]

method = ['cv.TM_CCOEFF_NORMED','cv.TM_CCORR_NORMED']
for meth in method:
    img = img1.copy()
    method = eval(meth)

    res = cv.matchTemplate(img,template,method)
    min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)

    bottom_right = (top_left[0] + w, top_left[1] + h)
    cv.rectangle(img,top_left, bottom_right, 255, 2)
    plt.subplot(121)
    plt.imshow(res,cmap = 'gray')
    plt.title('Matching Result')

    plt.subplot(122)
    plt.imshow(img,cmap = 'gray')
    plt.title('Detected Point') 
    plt.show()

Upvotes: 1

Views: 3559

Answers (2)

Shawn Mathew
Shawn Mathew

Reputation: 2337

Please don't use absolute dif or any similar method to calculate accuracy. You already have accuracy values in the variables min_val, max_val.

The OpenCV template matching uses various forms of correlation to calculate the match. So when you use cv.matchTemplate(img,template,method) the value stored in the res image is the result of this correlation.

So, when you use cv.minMaxLoc(res) you are calculating the minimum and maximum result of this correlation. I simply use max_val to tell me how well it has matched. Since both min_val and max_val are in the range [-1.0, 1.0], if max_val is 1.0 I take that as a 100% match, a max_val of 0.5 as a 50% match, and so on.

I've tried using a combination of min_val and max_val to scale the values to get a better understanding, but I found that simply using max_val gives me the desired results.

Upvotes: 5

simonet
simonet

Reputation: 295

There are several examples of metrics one can use to compare images. Some of them are:

All these require some elementary operations on the pixel values of the images to compare.

Upvotes: 0

Related Questions