c# - EmguCV MatchTemplate 获取所有匹配项

标签 c# emgucv

我在尝试使用 EmguCV MatchTemplate 函数返回所有匹配项时遇到问题。作为另一个要求,我需要它对颜色敏感(红色方 block 不是蓝色方 block )。 编辑:如果我的测试不正确,我认为它实际上已经对颜色敏感了。

这是我想出的代码,但我不确定如何修改它以满足我的需求:

Image<Bgr, byte> template = new Image<Bgr, byte>(pathNeedle);
Image<Bgr, byte> source = new Image<Bgr, byte>(pathHaystack);
Image<Bgr, byte> imageToShow = source.Copy();

Stopwatch watch = Stopwatch.StartNew();
using (Image<Gray, float> result = source.MatchTemplate(template, TemplateMatchingType.CcoeffNormed))
{
    double[] minValues, maxValues;
    Point[] minLocations, maxLocations;
    result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);

    watch.Stop();
    if (maxValues[0] > threshold)
    {
        // Match success
        Rectangle match = new Rectangle(maxLocations[0], template.Size);

        imageToShow.Draw(match, new Bgr(Color.Red), 3);

        // do stuff with match
        // etc..
    }

}

最佳答案

您需要获取本地最大值

private static List<Point> GetLocalMaxs(Image<Bgr, byte> image, Image<Bgr, byte> template)
        {
            Image<Gray, float> result = image.MatchTemplate(template, TemplateMatchingType.CcoeffNormed);
            var listOfMaxs = new List<Point>();
            var resultWithPadding = new Image<Gray, float>(image.Size);
            var heightOfPadding = (image.Height - result.Height) / 2;
            var widthOfPadding = (image.Width - result.Width) / 2;
            resultWithPadding.ROI = new Rectangle() { X = heightOfPadding, Y = widthOfPadding, Width = result.Width, Height = result.Height };
            result.CopyTo(resultWithPadding);
            resultWithPadding.ROI = Rectangle.Empty;

            for (int i = 0; i < resultWithPadding.Width; i++)
            {
                for (int j = 0; j < resultWithPadding.Height; j++)
                {
                    var centerOfRoi = new Point() { X = i + template.Width / 2, Y = j + template.Height / 2 };
                    var roi = new Rectangle() { X = i, Y = j, Width = template.Width, Height = template.Height };
                    resultWithPadding.ROI = roi;
                    double[] minValues, maxValues;
                    Point[] minLocations, maxLocations;
                    resultWithPadding.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);
                    resultWithPadding.ROI = Rectangle.Empty;
                    var maxLocation = maxLocations.First();
                    if (maxLocation.X == roi.Width / 2 && maxLocation.Y == roi.Height / 2)
                    {
                        var point = new Point() { X = centerOfRoi.X, Y = centerOfRoi.Y };
                        listOfMaxs.Add(point);
                    }

                }
            }

            return listOfMaxs;
        }

关于c# - EmguCV MatchTemplate 获取所有匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53514377/

相关文章:

c# - 为什么 Random.Next() 返回一个 int 而不是 uint?

image - 逐步JPEG压缩的代码

c# - 在 EmguCV 中将视频转换为列表的最佳方法是什么

algorithm - findContours 是否内置了边缘检测功能?

c# - 正确处理 EMGU 中的图像

c# - C# 中的扩展精度浮点危险

c# - 摆脱 C# datagridview 行中的蓝色背景

c# - 空只读 Action 委托(delegate)

c# - Emgu CV Blob 检测

c# - 从 Unity 3D 中按比例缩小的 UI 图像上的相对 x、y 位置获取像素