如何知道 matchTemplate 是否找到对象?
我使用 this 答案并编写了自己的程序,但我有一个特定的问题。
如果图像没有该对象,则 matchTemplate
不会抛出错误,并且我不知道有什么方法可以检查 matchTemplate
是否找到该对象,任何人都可以给我建议,或者给我一个检查这个的函数名称。
I used this answer and wrote my own program, but I have a specific problem.
If the image does not have the object, matchTemplate
does not throw an error, and I do not know of any method to check if matchTemplate
found the object or not, can anyone give me advice, or give me a function name which checks this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
matchTemplate()
返回一个矩阵,其值指示对象位于该像素中心的概率。如果您知道该对象(并且只有一个对象)在那里,您所要做的就是寻找最大值的位置。如果你不知道,你必须找到最大值,如果它高于某个阈值,你的对象应该在那里。
现在,选择该阈值很棘手 - 由您来专门为您的应用程序找到合适的阈值。当然,您会遇到一些误报(当没有对象,但最大值大于阈值时)和一些误报(您的对象没有创建足够大的峰值)
选择阈值的方法是收集一个相当大的图像数据库,里面有和没有你的物体,统计物体在里面时峰值有多大,不在里面时峰值有多大,然后选择最能区分这两类的阈值
matchTemplate()
returns a matrix whose values indicate the probability that your object is centered in that pixel. If you know the object (and only one object) is there, all you have to do is look for the location of the maximum value.If you don't know, you have to find the max value, and if it is above a certain threshold, your object should be there.
Now, selection of that threshold is tricky - it's up to you to find the good threshold specifically for your app. And of course you'll have some false positives (when there is no object, but the max is bigger than threshold), and some false negatives (your object does not create a big enough peak)
The way to choose the threshold is to collect a fairly large database of images with and without your object inside, and make a statistic of how big is the peak when object is inside, and how big is when it isn't, and choose the threshold that best separates the two classes
在我的项目中,我使用 与多个对象匹配的模板
示例,刚刚检查了“loc”数组的长度之一:
In my project I used Template Matching with Multiple Objects
example and just checked length one of "loc" array:
如果 method true 模板匹配,您可以使用此示例代码查找对象
This sample code find object if metod true template matched you can use