public static Emgu.CV.Image <Rgb, byte> MatchBestTemplate(Emgu.CV.Image <Rgb, byte> source, Emgu.CV.Image <Rgb, byte> template, Color matchHighlight) { Image <Rgb, byte> imageToShow = source.Copy(); using (Image <Gray, float> result = source.MatchTemplate(template, Emgu.CV.CvEnum.TemplateMatchingType.CcoeffNormed)) { double[] minValues, maxValues; Point[] minLocations, maxLocations; result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations); if (maxValues[0] > 0.4) { // This is a match. Do something with it, for example draw a rectangle around it. Rectangle match = new Rectangle(maxLocations[0], template.Size); imageToShow.Draw(match, new Rgb(matchHighlight), 3); } } // Show imageToShow in an ImageBox (here assumed to be called imageBox1) return(imageToShow); }
public static Emgu.CV.Image <Rgb, byte> MatchAllTemplate(Emgu.CV.Image <Rgb, byte> source, Emgu.CV.Image <Rgb, byte> template, Color matchHighlight) { Image <Rgb, byte> imageToShow = source.Copy(); using (Image <Gray, float> result = source.MatchTemplate(template, Emgu.CV.CvEnum.TemplateMatchingType.CcoeffNormed)) { for (int i = 0; i < result.Height; i++) { for (int j = 0; j < result.Width; j++) { var a = result.Data.GetValue(i, j, 0); if ((float)a > 0.4) { // This is a match. Do something with it, for example draw a rectangle around it. Rectangle match = new Rectangle(new Point(j, i), template.Size); imageToShow.Draw(match, new Rgb(matchHighlight), 3); } } } } // Show imageToShow in an ImageBox (here assumed to be called imageBox1) return(imageToShow); }