Find all matches with matchtemplate opencv opencvsharp

I am using opencv with opencvsharp.

When I execute a matchtemplate and then minmaxloc, I get only the first match. How to 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 find only the first match, and I know there are more matches.

+4
source share
2 answers

See my other answer here , where I show how to do exactly what you ask. It is written in C ++, but should be pretty trivial for a C # port. Instead of using std::queue use .NET Queue .

Essentially, you need to scan through your resImg search for all the maximum (or minimum, depending on the comparison algorithm) points and record as many as you want in some container (list, queue, priority queue, etc ...) . MinMaxLoc will only return the top match, so you only get one match.

0
source
 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); } 
+1
source

Source: https://habr.com/ru/post/1394596/


All Articles