如何知道 matchTemplate 是否找到对象?

发布于 2024-12-21 12:35:49 字数 229 浏览 3 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弥枳 2024-12-28 12:35:49

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

网名女生简单气质 2024-12-28 12:35:49

在我的项目中,我使用 与多个对象匹配的模板
示例,刚刚检查了“loc”数组的长度之一:

 res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
 threshold = 0.20
 loc = np.where( res >= threshold) 

 if len(loc[0])>0:
     detection = True
 else:
     detection = False

In my project I used Template Matching with Multiple Objects
example and just checked length one of "loc" array:

 res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
 threshold = 0.20
 loc = np.where( res >= threshold) 

 if len(loc[0])>0:
     detection = True
 else:
     detection = False
怕倦 2024-12-28 12:35:49

如果 method true 模板匹配,您可以使用此示例代码查找对象

   public bool MatchTemplateRotated(Mat image, Mat template, out Point matchLocation, out double foundAngle, out double foundScale, out Mat rtemplate, double threshold = 0.5)
   {
       var angles = new[] { 0, 45, 90, 135, 180,270,360 }; // Kullanmak istediğiniz açılar
       var scales = new[] { 0.5, 1.0, 1.5 }; // Kullanmak istediğiniz ölçekler
       //var angles = Enumerable.Range(-90, 181).ToArray(); // -90 ile 90 derece arasında 1 derecelik artışlarla
       //var scales = Enumerable.Range(75, 51).Select(x => (double)x / 100).ToArray(); // 0.75 ile 1.25 arasında 0.01'lik artışlarla
       foreach (var angle in angles)
       {
           foreach (var scale in scales)
           {
               var rotatedTemplate = RotateAndScaleTemplate(template, angle, scale);
               var result = new Mat();
               Cv2.MatchTemplate(image, rotatedTemplate, result, TemplateMatchModes.CCoeffNormed);

               double minVal, maxVal;
               Point minLoc, maxLoc;
               Cv2.MinMaxLoc(result, out minVal, out maxVal, out minLoc, out maxLoc);

               if (maxVal > threshold)
               {
                   matchLocation = maxLoc;
                   foundAngle = angle;
                   foundScale = scale;
                   rtemplate = rotatedTemplate;
                   return true;
               }
           }
       }
       matchLocation = new Point(0, 0);
       foundAngle = 0;
       foundScale = 1.0;
       rtemplate = new Mat();
       return false;
   }

This sample code find object if metod true template matched you can use

   public bool MatchTemplateRotated(Mat image, Mat template, out Point matchLocation, out double foundAngle, out double foundScale, out Mat rtemplate, double threshold = 0.5)
   {
       var angles = new[] { 0, 45, 90, 135, 180,270,360 }; // Kullanmak istediğiniz açılar
       var scales = new[] { 0.5, 1.0, 1.5 }; // Kullanmak istediğiniz ölçekler
       //var angles = Enumerable.Range(-90, 181).ToArray(); // -90 ile 90 derece arasında 1 derecelik artışlarla
       //var scales = Enumerable.Range(75, 51).Select(x => (double)x / 100).ToArray(); // 0.75 ile 1.25 arasında 0.01'lik artışlarla
       foreach (var angle in angles)
       {
           foreach (var scale in scales)
           {
               var rotatedTemplate = RotateAndScaleTemplate(template, angle, scale);
               var result = new Mat();
               Cv2.MatchTemplate(image, rotatedTemplate, result, TemplateMatchModes.CCoeffNormed);

               double minVal, maxVal;
               Point minLoc, maxLoc;
               Cv2.MinMaxLoc(result, out minVal, out maxVal, out minLoc, out maxLoc);

               if (maxVal > threshold)
               {
                   matchLocation = maxLoc;
                   foundAngle = angle;
                   foundScale = scale;
                   rtemplate = rotatedTemplate;
                   return true;
               }
           }
       }
       matchLocation = new Point(0, 0);
       foundAngle = 0;
       foundScale = 1.0;
       rtemplate = new Mat();
       return false;
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文