【问题标题】:Multiple results in OpenCVSharp3 MatchTemplateOpenCVSharp3 MatchTemplate 中的多个结果
【发布时间】:2015-12-20 15:27:30
【问题描述】:

我正在尝试在图像中查找图像出现。我编写了以下代码来使用 OpenCVSharp3 获得单个匹配项:

Mat src = OpenCvSharp.Extensions.BitmapConverter.ToMat(Resources.all);
Mat template = OpenCvSharp.Extensions.BitmapConverter.ToMat(Resources.img);
Mat result = src.MatchTemplate(template, TemplateMatchModes.CCoeffNormed);

double minVal, maxVal;
OpenCvSharp.Point minLoc, maxLoc;
result.MinMaxLoc(out minVal, out maxVal, out minLoc, out maxLoc);
Console.WriteLine("maxLoc: {0}, maxVal: {1}", maxLoc, maxVal);

如何根据阈值获得更多匹配项?

谢谢!

【问题讨论】:

    标签: c# opencv opencvsharp


    【解决方案1】:

    我认为该代码应该对您有所帮助:http://opencv-code.com/quick-tips/how-to-handle-template-matching-with-multiple-occurences/

    它是 C++,但转换为 C# 应该没什么大不了的。

    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    
    int main() 
    {
        cv::Mat ref = cv::imread("reference.png");
        cv::Mat tpl = cv::imread("template.png");
        if (ref.empty() || tpl.empty())
            return -1;
    
        cv::Mat gref, gtpl;
        cv::cvtColor(ref, gref, CV_BGR2GRAY);
        cv::cvtColor(tpl, gtpl, CV_BGR2GRAY);
    
        cv::Mat res(ref.rows-tpl.rows+1, ref.cols-tpl.cols+1, CV_32FC1);
        cv::matchTemplate(gref, gtpl, res, CV_TM_CCOEFF_NORMED);
        cv::threshold(res, res, 0.8, 1., CV_THRESH_TOZERO);
    
        while (true) 
        {
            double minval, maxval, threshold = 0.8;
            cv::Point minloc, maxloc;
            cv::minMaxLoc(res, &minval, &maxval, &minloc, &maxloc);
    
            if (maxval >= threshold)
            {
                cv::rectangle(
                    ref, 
                    maxloc, 
                    cv::Point(maxloc.x + tpl.cols, maxloc.y + tpl.rows), 
                    CV_RGB(0,255,0), 2
                );
                cv::floodFill(res, maxloc, cv::Scalar(0), 0, cv::Scalar(.1), cv::Scalar(1.));
            }
            else
                break;
        }
    
        cv::imshow("reference", ref);
        cv::waitKey();
        return 0;
    }
    

    【讨论】:

    • 当 Emgu 中不提供 MinMaxLoc(与标准示例的主要区别)之类的功能时,您能否停止写“没什么大不了的”?谢谢大家!
    • @Jan021981 OP 询问的是 OpenCVSharp3,而不是 Emgu,OpenCVSharp3 的语言结构与 C++ 对应的语言结构非常相似。
    【解决方案2】:

    这是 C++ 代码的一个端口。祝你好运!

    using OpenCvSharp;
    using OpenCvSharp.Util;
    static void RunTemplateMatch(string reference, string template)
    {
        using (Mat refMat = new Mat(reference))
        using (Mat tplMat = new Mat(template))
        using (Mat res = new Mat(refMat.Rows - tplMat.Rows + 1, refMat.Cols - tplMat.Cols + 1, MatType.CV_32FC1))
        {
            //Convert input images to gray
            Mat gref = refMat.CvtColor(ColorConversionCodes.BGR2GRAY);
            Mat gtpl = tplMat.CvtColor(ColorConversionCodes.BGR2GRAY);
    
            Cv2.MatchTemplate(gref, gtpl, res, TemplateMatchModes.CCoeffNormed);
            Cv2.Threshold(res, res, 0.8, 1.0, ThresholdTypes.Tozero);
    
            while (true)
            {
                double minval, maxval, threshold = 0.8;
                Point minloc, maxloc;
                Cv2.MinMaxLoc(res, out minval, out maxval, out minloc, out maxloc);
    
                if (maxval >= threshold)
                {
                    //Setup the rectangle to draw
                    Rect r = new Rect(new Point(maxloc.X, maxloc.Y), new Size(tplMat.Width, tplMat.Height));
    
                    //Draw a rectangle of the matching area
                    Cv2.Rectangle(refMat, r, Scalar.LimeGreen, 2);
    
                    //Fill in the res Mat so you don't find the same area again in the MinMaxLoc
                    Rect outRect;
                    Cv2.FloodFill(res, maxloc, new Scalar(0), out outRect, new Scalar(0.1), new Scalar(1.0));
                }
                else
                    break;
            }
    
            Cv2.ImShow("Matches", refMat);
            Cv2.WaitKey();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多