【问题标题】:EmguCV - MatchTemplateEmguCV - 匹配模板
【发布时间】:2011-03-04 02:19:39
【问题描述】:

我有一些这样的代码可以在结果图像中查找模板的所有实例。

Image<Gray, Byte> templateImage = new Image<Gray, Byte>(bmpSnip);
Image<Gray, float> imgMatch = sourceImage.MatchTemplate(templateImage, Emgu.CV.CvEnum.TM_TYPE.CV_TM_CCOEFF_NORMED);

然后循环通过 imgMatch.Data[,,] 属性检查分数是否超过阈值(例如 > 0.75)并在图像上放置关于匹配的标记。但是比赛毫无意义,我怀疑我的坐标是错误的。

        float[,,] matches = imgMatch.Data;
        for (int x = 0; x < matches.GetLength(0); x++)
        {
            for (int y = 0; y < matches.GetLength(1); y++)
            {
                double matchScore = matches[x, y, 0];
                if (matchScore > 0.75)
                {
                    Rectangle rect = new Rectangle(new Point(x,y), new Size(1, 1));
                    imgSource.Draw(rect, new Bgr(Color.Blue), 1);
                }

            }

        }

如果我使用 MinMax 如下:

double[] min, max;
Point[] pointMin, pointMax;
imgMatch.MinMax(out min, out max, out pointMin, out pointMax);

并设置一个标记(矩形)来突出显示匹配我得到一个非常好的结果。所以我很确定这与识别 imgMatch.Data[,,]

的坐标有关

关于我错在哪里的任何想法?

【问题讨论】:

    标签: c# .net opencv emgucv


    【解决方案1】:

    您在数组中放错了 X 和 Y 坐标。试试这个:

            float[, ,] matches = imgMatch.Data;
            for (int y = 0; y < matches.GetLength(0); y++)
            {
                for (int x = 0; x < matches.GetLength(1); x++)
                {
                    double matchScore = matches[y, x, 0];
                    if (matchScore > 0.75)
                    {
                       Rectangle rect = new Rectangle(new Point(x,y), new Size(1, 1));
                       imgSource.Draw(rect, new Bgr(Color.Blue), 1);
                    }
    
                }
    
            }
    

    【讨论】:

      【解决方案2】:

      其实答案是:

      由于您使用的是 Emgu.CV.CvEnum.TM_TYPE.CV_TM_CCOEFF_NORMED,因此结果取决于模板大小。您得到的任何结果,即匹配点实际上是对模板左上角的引用,因此要找到匹配的中心,只需从 X 中减去模板宽度的一半,从 Y 中减去模板高度的一半。

      if (matchScore > 0.75)
      {
          Rectangle rect = new Rectangle(new Point(x - templateImage.Width ,y - templateImage-Height), new Size(1, 1));
          imgSource.Draw(rect, new Bgr(Color.Blue), 1);
      }
      

      除此之外,您仅使用 0.75 的阈值,0.9 或更高的阈值将产生更理想的结果。要准确评估您需要的值阈值,请直接查看 imgMatch 或查看数据的直方图形成。

      保重 克里斯

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多