public Rectangle[] FindMatches(Image<Bgr, Byte> source, Image<Bgr, Byte> target, bool copy = true)
{
//rectangles = new List<Rectangle>();
rectangles.Clear();
//stopwatch = new Stopwatch();
//stopwatch.Start();
Image<Bgr, Byte> imgSrc = copy ? source.Copy() : source;
// FindImage all occurences of imgFind
double[] minValues, maxValues;
Point[] minLocations, maxLocations;
while (true)
{
using (
Image<Gray, float> result = imgSrc.MatchTemplate(target,
Emgu.CV.CvEnum.TemplateMatchingType.CcoeffNormed))
{
result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);
// You can try different values of the threshold. I guess somewhere between 0.75 and 0.95 would be good.
if (maxValues[0] < Threshold || minValues[0] == maxValues[0]) break;
// This is a match. Do something with it, for example draw a rectangle around it.
Rectangle match = new Rectangle(maxLocations[0], target.Size);
// Fill the drawing with red in order to ellimate this as a source.
imgSrc.Draw(match, fillColor, -1);
// Add the found rectangle to the results.
rectangles.Add(match);
}
}
if(copy) imgSrc.Dispose();
//stopwatch.Stop();
return rectangles.ToArray();
}