How to find one image inside of another?

前端 未结 4 1162
难免孤独
难免孤独 2020-11-27 15:06

I have 2 bmp images. ImageA is a screenshot (example) ImageB is a subset of that. Say for example, an icon.

I want to find the X,Y coordinates of ImageB within Imag

4条回答
  •  天命终不由人
    2020-11-27 15:44

    Here's a quick sample but it is slow take around 4-6 seconds, but it does exactly what you looking for and i know this post is old but if anyone else visiting this post recently you can look this thing you need .NET AForge namespace or framework google it and install it include AForge name space in your project and that's it it finds the pictiure with another and gives out the coordinates.

    System.Drawing.Bitmap sourceImage = (Bitmap)Bitmap.FromFile(@"C:\SavedBMPs\1.jpg");
                System.Drawing.Bitmap template = (Bitmap)Bitmap.FromFile(@"C:\SavedBMPs\2.jpg");
                // create template matching algorithm's instance
                // (set similarity threshold to 92.1%)
    
               ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.921f);
                    // find all matchings with specified above similarity
    
                    TemplateMatch[] matchings = tm.ProcessImage(sourceImage, template);
                    // highlight found matchings
    
               BitmapData data = sourceImage.LockBits(
                    new Rectangle(0, 0, sourceImage.Width, sourceImage.Height),
                    ImageLockMode.ReadWrite, sourceImage.PixelFormat);
                foreach (TemplateMatch m in matchings)
                {
    
                        Drawing.Rectangle(data, m.Rectangle, Color.White);
    
                    MessageBox.Show(m.Rectangle.Location.ToString());
                    // do something else with matching
                }
                sourceImage.UnlockBits(data);
    

提交回复
热议问题