Exemplo n.º 1
0
        private Point matchTemple(Mat src, Mat temple, TemplateMatchingType type, Mat dest)
        {
            Mat    srcCopy = new Mat();
            Point  center = new Point();
            Point  matchLoc = new Point();
            double minVal = new double(); double maxVal = 0;
            Point  minLoc = new Point(); Point maxLoc = new Point();

            CvInvoke.CvtColor(src, srcCopy, ColorConversion.Bgr2Gray);
            Mat MatchResult = new Mat(srcCopy.Rows - temple.Rows + 1, srcCopy.Cols - temple.Cols + 1, DepthType.Cv32F, 1);

            CvInvoke.MatchTemplate(srcCopy, temple, MatchResult, type);
            CvInvoke.MinMaxLoc(MatchResult, ref minVal, ref maxVal, ref minLoc, ref maxLoc);
            if (type == TemplateMatchingType.SqdiffNormed ||
                type == TemplateMatchingType.Sqdiff)
            {
                matchLoc = minLoc;
            }
            else
            {
                matchLoc = maxLoc;
            }
            Rectangle MatchRect = new Rectangle(matchLoc, temple.Size);

            CvInvoke.Rectangle(dest, MatchRect, new Bgr(Color.Red).MCvScalar);
            center.X        = matchLoc.X + temple.Width / 2;
            center.Y        = matchLoc.Y + temple.Height / 2;
            imageBox9.Image = temple;
            //imageBox10.Image = dest;
            return(center);
        }
Exemplo n.º 2
0
 /// <summary>
 /// A class that uses OpenCV and WindowsInput to automate Windows using computer vision
 /// </summary>
 /// <param name="matchingAccuracy">specifies the matching accuracy used on the result of OpenCV MatchTemplate (0-1). 0.75 seems adequate</param>
 public Automator(double matchingAccuracy)
 {
     screenMaxWidth        = Screen.PrimaryScreen.Bounds.Width;
     screenMaxHeight       = Screen.PrimaryScreen.Bounds.Height;
     inputSimulator        = new InputSimulator();
     mouse                 = inputSimulator.Mouse;
     keyboard              = inputSimulator.Keyboard;
     this.matchingAccuracy = matchingAccuracy;
     matchingType          = TemplateMatchingType.CcoeffNormed;
 }
        private void EmguCV_MatchTemplateViewer_Load(object sender, EventArgs e)
        {
            Image <Bgr, byte> imageToShow = _TemplateImg.Copy();

            if (_MatchAlgoritmh == "CCOEFF_NORMALIZED")
            {
                method = TemplateMatchingType.CcoeffNormed;
            }
            if (_MatchAlgoritmh == "CCORR_NORMALIZED")
            {
                method = TemplateMatchingType.CcorrNormed;
            }
            if (_MatchAlgoritmh == "SQDIFF_NORMALIZED")
            {
                method = TemplateMatchingType.SqdiffNormed;
            }


            using (Image <Gray, float> result = _SearchImage.MatchTemplate(_TemplateImg, method))
            {
                double[] minValues, maxValues;
                Point[]  minLocations, maxLocations;
                result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);
                _ConfidenceScore       = maxValues[0];
                lblFindMatchScore.Text = "Find Match Score: " + _ConfidenceScore;

                if (maxValues[0] > _MatchTemplateScore)
                {
                    Rectangle match = new Rectangle(maxLocations[0], _TemplateImg.Size);

                    imageToShow.Draw(match, new Bgr(0, 255, (double)byte.MaxValue), 3);
                    pbSearchImage.Image    = _SearchImage.Bitmap;
                    pbTemplate.Image       = imageToShow.Bitmap;
                    lblFindMatchScore.Text = "Match Score: " + maxValues[0].ToString();

                    Bitmap bmp = new Bitmap(this.Width, this.Height);
                    DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
                    _ImageEvidence = new Image <Bgr, byte>(bmp);
                }
            }

            if (!_Debug)
            {
                Timer.Start();
            }
            this.Refresh();
        }
        private bool MatchPatternInPicture(Bitmap picture, Bitmap pattern, TemplateMatchingType matchingType)
        {
            Image <Gray, float> screenImage = new Image <Gray, float>(picture);
            Image <Gray, float> patterImage = new Image <Gray, float>(pattern);


            using (Image <Gray, float> result = screenImage.MatchTemplate(patterImage, matchingType))
            {
                double[] minValues, maxValues;
                Point[]  minLocations, maxLocations;
                result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);

                if (Thresholds[matchingType] < maxValues[0])
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            string patchImagePath = @"";
            string fullImagePath  = @"";

            Image <Bgr, Byte> patchImage = new Image <Bgr, byte>(patchImagePath);
            Image <Bgr, Byte> fullImage  = new Image <Bgr, byte>(fullImagePath);

            TemplateMatchingType matchMethod = TemplateMatchingType.CcoeffNormed;

            Image <Gray, float> imgMatch = fullImage.MatchTemplate(patchImage, matchMethod);

            //i have seen people wanting to normalize here, but i dont fully understand why
            CvInvoke.Normalize(imgMatch, imgMatch, 0, 1, NormType.MinMax, DepthType.Default, new Mat());

            //find the best match with minMax
            double[] min, max;
            Point[]  pointMin, pointMax;
            imgMatch.MinMax(out min, out max, out pointMin, out pointMax);

            Point matchLoc;

            //for Sqdiff and SqdiffNormed, the best matches are lower values. For all the other methods, the higher the better
            if (matchMethod == TemplateMatchingType.Sqdiff || matchMethod == TemplateMatchingType.SqdiffNormed)
            {
                matchLoc = pointMin[0];
            }
            else
            {
                matchLoc = pointMax[0];
            }

            //draw a red box around the the match
            Rectangle region = new Rectangle(matchLoc.X, matchLoc.Y, patchImage.Width, patchImage.Height);

            fullImage.Draw(region, new Bgr(Color.Red));

            ImageViewer.Show(fullImage, "template matching");
        }
Exemplo n.º 6
0
        void MatchTemplaeExample()
        {
            //Argumentos de entrada necesarios para algoritmo.
            object objectVariable1 = Image.FromFile(@"C:\Images\Debug\Test1.jpg"); //Search image
            object objectVariable2 = Image.FromFile(@"C:\Images\Debug\Test2.jpg"); //Template image
            string upper           = "CCOEFF_NORMALIZED";                          //Example:CCOEFF_NORMALIZED, CCORR_NORMALIZED, SQDIFF_NORMALIZED  Matching Algorithm
            bool   booleanVariable = true;                                         //Show debug image


            TemplateMatchingType method = new TemplateMatchingType();
            bool flag = false;

            if (upper == "CCOEFF_NORMALIZED")
            {
                method = TemplateMatchingType.CcoeffNormed;
                flag   = true;
            }
            else if (upper == "CCORR_NORMALIZED")
            {
                method = TemplateMatchingType.CcorrNormed;
                flag   = true;
            }
            else if (upper == "SQDIFF_NORMALIZED")
            {
                method = TemplateMatchingType.SqdiffNormed;
                flag   = false;
            }
            try
            {
                if (objectVariable1 != null && objectVariable2 != null)
                {
                    Image <Bgr, Byte> myImage1 = new Image <Bgr, Byte>((Bitmap)objectVariable1); //Convert from bitmap to Emgu.CV.Image
                    Image <Bgr, Byte> myImage2 = new Image <Bgr, Byte>((Bitmap)objectVariable2); //Convert from bitmap to Emgu.CV.Image
                    IImage            image1   = (IImage)myImage1;
                    IImage            image2   = (IImage)myImage2;
                    //IImage image1 = (IImage)objectVariable1; //Original
                    //IImage image2 = (IImage)objectVariable2; //Original
                    Image <Bgr, byte>   image3   = new Image <Bgr, byte>(image1.Bitmap);
                    Image <Bgr, byte>   template = new Image <Bgr, byte>(image2.Bitmap);
                    Image <Gray, float> image4   = image3.MatchTemplate(template, method);
                    //this.VariableSpace.UpdateStatusText("OpenCV_MatchTemplate: Locating Best Matches Found in Image...");
                    double[] minValues;
                    double[] maxValues;
                    Point[]  minLocations;
                    Point[]  maxLocations;
                    image4.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);
                    double x    = maxValues[0];
                    double num1 = minValues[0];
                    double num2;
                    if (flag)
                    {
                        if (maxValues.Length > 1)
                        {
                            //this.VariableSpace.UpdateStatusText("OpenCV_MatchTemplate: Multiple Optimal Matches Have Been Found in the Image, returning the first best match.");
                        }

                        num2 = Math.Pow(x, 3.0) * 100.0;
                        //this.VariableSpace.UpdateStatusText(string.Format("OpenCV_MatchTemplate: First Best Match Located At [{0}, {1}]. Match Confidence = [{2}%].", (object)maxLocations[0].X, (object)maxLocations[0].Y, (object)num2));
                        //this.VariableSpace.setVariable(new ScriptVariable("ReturnValue0", VariableType.Float, (object)num2));
                        //this.VariableSpace.setVariable(new ScriptVariable("ReturnValue1", VariableType.Integer, (object)maxLocations[0].X));
                        //this.VariableSpace.setVariable(new ScriptVariable("ReturnValue2", VariableType.Integer, (object)maxLocations[0].Y));
                        //this.VariableSpace.setVariable(new ScriptVariable("ReturnValue3", VariableType.Object, (object)new Image<Gray, byte>(image4.Bitmap)));
                    }
                    else
                    {
                        if (minValues.Length > 1)
                        {
                            //this.VariableSpace.UpdateStatusText("OpenCV_MatchTemplate: Multiple Optimal Matches Have Been Found in the Image, returning the first best match.");
                        }
                        num2 = Math.Pow(1.0 - num1, 3.0) * 100.0;
                        //this.VariableSpace.UpdateStatusText(string.Format("OpenCV_MatchTemplate: First Best Match Located At [{0},{1}]. Match Confidence = [{2}%].", (object)minLocations[0].X, (object)minLocations[0].Y, (object)num2));
                        //this.VariableSpace.setVariable(new ScriptVariable("ReturnValue0", VariableType.Float, (object)num2));
                        //this.VariableSpace.setVariable(new ScriptVariable("ReturnValue1", VariableType.Integer, (object)minLocations[0].X));
                        //this.VariableSpace.setVariable(new ScriptVariable("ReturnValue2", VariableType.Integer, (object)minLocations[0].Y));
                        //this.VariableSpace.setVariable(new ScriptVariable("ReturnValue3", VariableType.Object, (object)new Image<Gray, byte>(image4.Bitmap)));
                    }
                    if (booleanVariable)
                    {
                        Image <Bgr, byte> image5 = image3.ConcateVertical(new Image <Bgr, byte>(image4.Bitmap));
                        Point[]           pts    = new Point[4];
                        if (flag)
                        {
                            pts[0].X = maxLocations[0].X;
                            pts[0].Y = maxLocations[0].Y;
                            pts[1].X = pts[0].X;
                            pts[1].Y = pts[0].Y + template.Height;
                            pts[2].X = pts[0].X + template.Width;
                            pts[2].Y = pts[0].Y + template.Height;
                            pts[3].X = pts[0].X + template.Width;
                            pts[3].Y = maxLocations[0].Y;
                        }
                        else
                        {
                            pts[0].X = minLocations[0].X;
                            pts[0].Y = minLocations[0].Y;
                            pts[1].X = pts[0].X;
                            pts[1].Y = pts[0].Y + template.Height;
                            pts[2].X = pts[0].X + template.Width;
                            pts[2].Y = pts[0].Y + template.Height;
                            pts[3].X = pts[0].X + template.Width;
                            pts[3].Y = minLocations[0].Y;
                        }
                        image5.DrawPolyline(pts, true, new Bgr((double)byte.MaxValue, 0.0, 0.0), 2, Emgu.CV.CvEnum.LineType.EightConnected, 0);
                        //int num3 = (int)new ImageViewer((IImage)image5, string.Format("Pattern Match Confidence: [{0}%].", (object)num2)).ShowDialog();
                        ImageViewer imageViewer = new ImageViewer(image5.Bitmap, num2);
                        imageViewer.Show();
                        image5.Dispose();
                    }
                    image3.Dispose();
                    image4.Dispose();
                }
            }
            catch (Exception ex)
            {
            }
        }