Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding all matches with matchtemplate opencv opencvsharp

I am using opencv with opencvsharp.

When doing a matchtemplate and afterwards minmaxloc I only get the first match. How do I get all matches?

            Cv.MatchTemplate(tempImg, templateSymbol.Img, resImg, MatchTemplateMethod.CCorrNormed);
            double min_val, max_val;
            Cv.MinMaxLoc(resImg, out min_val, out max_val);
            if (max_val > 0.5)
            {

                symbolsFound.Add(templateSymbol.Description);
                Console.WriteLine(templateSymbol.Description);
            }

I only find the first match and I know there are more matches.

like image 490
Casper Thule Hansen Avatar asked Feb 03 '12 11:02

Casper Thule Hansen


1 Answers

try
        {

            IplImage tpl = Cv.LoadImage("template path", LoadMode.Color);
            IplImage img = Cv.LoadImage("main image path", LoadMode.Color);

            IplImage res = Cv.CreateImage(Cv.Size(img.Width - tpl.Width + 1, img.Height - tpl.Height + 1), BitDepth.F32, 1);
            Cv.MatchTemplate(img, tpl, res, MatchTemplateMethod.CCoeffNormed);

            Cv.Threshold(res, res, 0.9, 255, ThresholdType.ToZero);

            while (true)
            {
                CvPoint minloc, maxloc;
                double minval, maxval, threshold = 0.95;

                Cv.MinMaxLoc(res, out minval, out maxval, out minloc, out maxloc, null);
                if (maxval > threshold)
                {
                    Console.WriteLine("Matched " + maxloc.X + "," + maxloc.Y);
                    Cv.FloodFill(res, maxloc, new CvScalar());
                }
                else
                {
                    Console.WriteLine("No More Matches");
                    break;
                }
            }

            Cv.ReleaseImage(res);
            Cv.ReleaseImage(img);


        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
like image 199
Mayank Kumawat Avatar answered Oct 06 '22 23:10

Mayank Kumawat