使用Emgu CV进行模板匹配-不适用于多个模板

时间:2018-08-24 10:10:42

标签: c# image-processing emgucv opencv3.0

我正在开发Emgu CV,但我没有在图像中获得多个模板的结果,请检查以下代码,并让我知道我在哪里出错。 我相信我在while上犯了一个错误-请纠正我为获得结果所需要做的事情。

using (Image<Bgr, byte> imgSrc = BaseImage.Copy())
{
    while (true)
    {
        using (Image<Gray, float> result = imgSrc.MatchTemplate(SubImage, TemplateMatchingType.SqdiffNormed))
        {
            CvInvoke.Threshold(result, result, 0.7, 1, ThresholdType.ToZero);
            double[] minValues, maxValues;
            Point[] minLocations, maxLocations;
            result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);
            if (maxValues[0] > Threashold)
            {
                Rectangle match = new Rectangle(maxLocations[0], SubImage.Size);
                imgSrc.Draw(match, new Bgr(Color.Blue), -1);
                rectangles.Add(match);
            }
            else
            {
                break;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

它正在工作并更新了我已更改的注释中的代码。

 using (Image<Bgr, byte> imgSrc = BaseImage.Copy())
    {
        while (true)
        {
           //updated and changed TemplateMatchingType- CcoeffNormed.
            using (Image<Gray, float> result = imgSrc.MatchTemplate(SubImage, TemplateMatchingType.CcoeffNormed)) 
            {
                CvInvoke.Threshold(result, result, 0.7, 1, ThresholdType.ToZero);
                double[] minValues, maxValues;
                Point[] minLocations, maxLocations;
                result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);
                if (maxValues[0] > Threashold)
                {
                    Rectangle match = new Rectangle(maxLocations[0], SubImage.Size);
                    imgSrc.Draw(match, new Bgr(Color.Blue), -1);
                    rectangles.Add(match);
                }
                else
                {
                    break;
                }
            }
        }
    }