Ejemplo n.º 1
0
        /// <summary>
        /// https://stackoverflow.com/questions/16406958/emgu-finding-image-a-in-image-b
        /// </summary>
        private bool Recognize(World.ImageRecognition recognition, out Rectangle match)
        {
            if (!File.Exists(Configuration.Folder + $"\\images\\{recognition.Name}"))
            {
                GMException.Throw($"Tried to recognize {recognition.Name} but no image was found!", Severity.Medium);
                match = new Rectangle();
                return(false);
            }
            Image <Bgr, byte> source   = BitmapExtension.ToImage <Bgr, Byte>(Utility.CaptureWindow(ProcessHandle()));
            Image <Bgr, byte> template = new Image <Bgr, byte>(Configuration.Folder + $"\\images\\{recognition.Name}");
            bool found = false;

            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.9)
                {
                    match = new Rectangle(maxLocations[0], template.Size);
                    found = true;
                }
                else
                {
                    match = new Rectangle();
                }
            }
            source.Dispose();
            template.Dispose();
            return(found);
        }