OpenCV 中的 MatchTemplate 与 Python
我正在使用 opencv 和 python 绑定。我正在尝试使用模板匹配,但它的执行效果并不完全符合我的需要。如果没有与我提供的模板匹配的图像,我不希望它返回匹配项。无论实际模板是否存在于我提供的图像中,它似乎总是返回匹配项。
我查看了 opencv with Python 的文档,似乎找不到任何关于如何设置匹配模板的最小阈值的内容。在将模板与图像进行比较时,我需要相对严格。
image = LoadImage("c:/image.png")
template = LoadImage("c:/image-crop2.png")
W,H = GetSize(image)
w,h = GetSize(template)
width = W - w + 1
height = H - h + 1
result = CreateImage((width, height), 32, 1)
MatchTemplate(image, template, result, CV_TM_CCORR)
(min_x, max_y, minloc, maxloc) = MinMaxLoc(result)
(x, y) = minloc
print result
I'm using opencv with python bindings. I'm trying to use the template match, but it's not performing exactly as I need it to. If there is no image matching the template I supply it, I don't want it to return a match. It seems to always return a match whether the actual template exists at all in the image I supply it.
I've looked at the documentation for the opencv with Python and can't seem to find any mention of how to set a minimum threshold for matching templates. I need it to be relatively strict when comparing the template to the image.
image = LoadImage("c:/image.png")
template = LoadImage("c:/image-crop2.png")
W,H = GetSize(image)
w,h = GetSize(template)
width = W - w + 1
height = H - h + 1
result = CreateImage((width, height), 32, 1)
MatchTemplate(image, template, result, CV_TM_CCORR)
(min_x, max_y, minloc, maxloc) = MinMaxLoc(result)
(x, y) = minloc
print result
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅此答案:OpenCV。匹配时绘制矩形
您遇到了完全相同的问题 -
MatchTemplate
返回一种相似度图而不是单个匹配位置。See this answer: OpenCV. Drawing rectangle when matching
You are having exactly the same problem -
MatchTemplate
returns a kind of similarity map instead of single match position.