OpenCV的:寻找多个匹配 [英] OpenCv: Finding multiple matches

查看:792
本文介绍了OpenCV的:寻找多个匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的,但我无法弄清楚如何找到源图像中的所有比赛。

I have the following, but I can't figure out how to find ALL the matches in a source image.

    static void Main()
    {
        using (var template = Cv.LoadImage(@"images\logo.png", LoadMode.GrayScale))
        using (var source = Cv.LoadImage(@"images\manyLogos.png", LoadMode.GrayScale))
        using (var sourceColour = Cv.LoadImage(@"images\manyLogos.png", LoadMode.Color))
        {
            var width = source.Width - template.Width + 1;
            var height = source.Height - template.Height + 1;
            using (var result = Cv.CreateImage(Cv.Size(width, height), BitDepth.F32, 1))
            {
                Cv.MatchTemplate(source, template, result, MatchTemplateMethod.SqDiff);
                var THRESHOLD = 0.08D;

                double minVal, maxVal;
                CvPoint minLoc, maxLoc;                    
                Cv.MinMaxLoc(result, out minVal, out maxVal, out minLoc, out maxLoc);

                var outlineColor = (minVal > THRESHOLD) ? CvColor.Green : CvColor.Red;
                Cv.Rectangle(sourceColour, Cv.Point(minLoc.X, minLoc.Y), Cv.Point(minLoc.X + template.Width, minLoc.Y + template.Height), outlineColor, 1, 0, 0);
            }

            using (var window = new CvWindow("Test"))
            {
                while (CvWindow.WaitKey(10) < 0)
                {
                    window.Image = sourceColour;
                }
            }
        }
    }



我可以勾勒出的最佳匹配,只是没有所有的比赛。我需要让所有的比赛弄好了。

I can outline the best match, just not all the matches. I need to get all the matches somehow.

推荐答案

使用matchTemplate方法,你的输出图像会给你像素代表得怎么样值您的模板在这个特定位置相匹配。在你的情况下,数值越低,最好的比赛,因为你使用MatchTemplateMethod.SqDiff。

Using matchTemplate method, your output image will give you pixels values which represents how well your template is matched at this specific location. In you case, the lower the value, the best the match, since you used MatchTemplateMethod.SqDiff.

您的问题是,当您使用minMaxLoc功能,你会得到什么你要求,这是在这种情况下)的最佳匹配,最小。

You problem is that when you use the minMaxLoc function, you get what you asks for, which is the best match in this case, the min).

所有的比赛都是其价值是根据您设置的阈值的像素。
因为我不习惯CSHARP,这里是它如何去在C ++中,你可以做翻译:

All matches are the pixels whose value are under the threshold that you set up. Since I'm not used to csharp, here is how it would go in C++, you can do the translation:

// after your call to MatchTemplate
float threshold = 0.08;
cv::Mat thresholdedImage;
cv::threshold(result, thresholdedImage, threshold, 255, CV_THRESH_BINARY);
// the above will set pixels to 0 in thresholdedImage if their value in result is lower than the threshold, to 255 if it is larger.
// in C++ it could also be written cv::Mat thresholdedImage = result < threshold;
// Now loop over pixels of thresholdedImage, and draw your matches
for (int r = 0; r < thresholdedImage.rows; ++r) {
  for (int c = 0; c < thresholdedImage.cols; ++c) {
    if (!thresholdedImage.at<unsigned char>(r, c)) // = thresholdedImage(r,c) == 0
      cv::circle(sourceColor, cv::Point(c, r), template.cols/2, CV_RGB(0,255,0), 1);
  }
}

这篇关于OpenCV的:寻找多个匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆