如何使用OpenCV MatchTemplate? [英] How do I use OpenCV MatchTemplate?
问题描述
我正在尝试在另一个图像中查找图像.
I'm attempting to find an image in another.
im = cv.LoadImage('1.png', cv.CV_LOAD_IMAGE_UNCHANGED)
tmp = cv.LoadImage('e1.png', cv.CV_LOAD_IMAGE_UNCHANGED)
w,h = cv.GetSize(im)
W,H = cv.GetSize(tmp)
width = w-W+1
height = h-H+1
result = cv.CreateImage((width, height), 32, 1)
cv.MatchTemplate(im, tmp, result, cv.CV_TM_SQDIFF)
print result
运行此命令时,一切执行正常,不会引发任何错误.但是我不确定从这里开始该怎么做.该文档说result
存储比较结果图".我尝试打印它,但是它给了我宽度,高度和台阶.
When I run this, everything executes just fine, no errors get thrown. But I'm unsure what to do from here. The doc says that result
stores "A map of comparison results". I tried printing it, but it gives me width, height, and step.
如何使用此信息来查找一个图像是否在另一个图像中/位于该图像中?
How do I use this information to find whether or not one image is in another/where it is located?
推荐答案
MatchTemplate
返回相似度图,而不是位置.
然后,您可以使用此地图查找位置.
MatchTemplate
returns a similarity map and not a location.
You can then use this map to find a location.
如果您只想寻找一个匹配项,则可以执行以下操作来获取位置:
If you are only looking for a single match you could do something like this to get a location:
minVal,maxVal,minLoc,maxLoc = cv.MinMaxLoc(result)
然后minLoc
具有最匹配的位置,而minVal
描述了模板的适合程度.您需要确定minVal
的阈值,以确定您是否认为此结果匹配.
Then minLoc
has the location of the best match and minVal
describes how well the template fits. You need to come up with a threshold for minVal
to determine whether you consider this result a match or not.
如果每个图像要查找多个匹配项,则需要使用非最大压缩等算法.
If you are looking for more than one match per image you need to use algorithms like non-maximum supression.
这篇关于如何使用OpenCV MatchTemplate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!