如何找到模板匹配精度 [英] how to find the template matching accuracy

查看:41
本文介绍了如何找到模板匹配精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做模板匹配
现在,我要做的就是找到模板匹配的准确性
我已经完成了模板匹配,但是如何获得准确性我想我必须减去匹配的区域和模板图像.我该如何实现

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

代码

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()

推荐答案

请不要使用绝对dif或任何类似方法来计算精度.您已经在变量 min_val,max_val 中获得了精度值.

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.

OpenCV模板匹配使用

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.

因此,当您使用 cv.minMaxLoc(res)时,您正在计算此关联的最小和最大结果.我只是使用 max_val 告诉我它的匹配程度如何.由于 min_val max_val 均在 [-1.0,1.0] 范围内,因此如果 max_val 为1.0,则为100%匹配,而 max_val 为0.5则为50%匹配,依此类推.

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.

我尝试使用 min_val max_val 的组合来缩放值以获得更好的理解,但是我发现仅使用 max_val 给了我想要的结果.

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.

这篇关于如何找到模板匹配精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆