我想检查opencv中的模板是否匹配

2024-10-02 02:41:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我想检查模板匹配是否正确

就像这样:

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

img = cv.imread('messi5.jpg',0)
img2 = img.copy() 
template = cv.imread('template.jpg',0)
w, h = template.shape[::-1]
methods = ['cv.TM_CCOEFF', 'cv.TM_CCOEFF_NORMED', 'cv.TM_CCORR',
            'cv.TM_CCORR_NORMED', 'cv.TM_SQDIFF', 'cv.TM_SQDIFF_NORMED']
for meth in methods:
    img = img2.copy()
    method = eval(meth)
    res = cv.matchTemplate(img,template,method)
    min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)

    if template_match:
        //do something

我读过这篇文章,但我不明白

谢谢你的回答


Tags: importimgastemplatecvtmmethodsjpg
1条回答
网友
1楼 · 发布于 2024-10-02 02:41:21
  1. 获取模板的功能

  • An image features, such as edges and interest points, provide rich information on the image content. source


  • 例如:如果下面是你的模板,那么找到它的特征

    enter image description hereenter image description here

     import numpy as np
     import imutils
     import glob
     import cv2
    
     template = cv2.imread("template.jpg")
     template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
     template = cv2.Canny(template, 50, 200)
     (h, w) = template.shape[:2]
    
  1. 获取图像的特征

  • enter image description hereenter image description here
  1. 检查模板特征是否与图像特征匹配

result = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF)
  • cv2.TM_CCOEFF只是一个选项,您可以使用许多其他templates
  1. 查找result变量的最小值和最大值

(_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)
  1. 现在,您可以检查模板匹配是否正确

found = (maxVal, maxLoc, r)

因此,如果检测到模板,found变量返回长度为3的元组,这意味着模板匹配。例如:

(495.000, (148, 26), 1)
  • 495.000是数组中的最大值

  • (148,26)正在开始查找对象的(x, y)坐标

  • 1.0是半径

相关问题 更多 >

    热门问题