Click here to Skip to main content
15,887,275 members
Home / Discussions / C#
   

C#

 
GeneralRe: How can I find a Bitmap within another Bitmap? Pin
verence33326-Jul-09 20:00
verence33326-Jul-09 20:00 
AnswerRe: How can I find a Bitmap within another Bitmap? [modified] Pin
Luc Pattyn23-Jul-09 6:59
sitebuilderLuc Pattyn23-Jul-09 6:59 
GeneralRe: How can I find a Bitmap within another Bitmap? Pin
verence33323-Jul-09 21:00
verence33323-Jul-09 21:00 
GeneralRe: How can I find a Bitmap within another Bitmap? Pin
Luc Pattyn24-Jul-09 1:13
sitebuilderLuc Pattyn24-Jul-09 1:13 
GeneralRe: How can I find a Bitmap within another Bitmap? Pin
verence33324-Jul-09 2:12
verence33324-Jul-09 2:12 
GeneralRe: How can I find a Bitmap within another Bitmap? Pin
Luc Pattyn24-Jul-09 2:42
sitebuilderLuc Pattyn24-Jul-09 2:42 
GeneralRe: How can I find a Bitmap within another Bitmap? Pin
verence33324-Jul-09 2:53
verence33324-Jul-09 2:53 
AnswerRe: How can I find a Bitmap within another Bitmap? Pin
verence33328-Jul-09 3:16
verence33328-Jul-09 3:16 
Hi again.

I've made a little method to find a Bitmap inside another one. It's fast enough for what I want. Obviously, the elapsed time depends on the size of the Bitmaps, but it takes about 15-30 ms. to find a small one (like a window button inside a Bitmap of a window, for instance) and 150-180 ms. to find a big one (like a window inside a capture of the screen).

I will try to program other strategies that we've been talking about, specially the convolution alternative (which I think would probably be the fastest if done the right way). In the meanwhile, here is the code so you can tell me your comments and suggestions, hoping that it will help anybody that has the same problem (I really did not find any method like this one anywhere on the Internet).

The method receives a small Bitmap which is expected to be contained in another, bigger Bitmap. It returns a Point which represents the upper left corner of the wanted location.

Note that, if the small Bitmap is not contained in the bigger one, it returns a Point(0,0), exactly the same as if it was contained in such a position. This can and should be easily improved, and I would like to read your comments about other improvements (specially concerning the speed issue) since I'm rather new in C# and I usually work with Java.

Thank you!

private Point searchBitmap(Bitmap smallBmp, Bitmap bigBmp)
        {
            BitmapData smallData = smallBmp.LockBits(new Rectangle(0, 0, smallBmp.Width, smallBmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            BitmapData bigData = bigBmp.LockBits(new Rectangle(0, 0, bigBmp.Width, bigBmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            
            int smallStride = smallData.Stride;
            int bigStride = bigData.Stride;            

            int bigWidth = bigBmp.Width;
            int bigHeight = bigBmp.Height;
            int smallWidth = smallBmp.Width * 3;
            int smallHeight = smallBmp.Height;

            Point location = new Point(0, 0);

            unsafe
            {
                byte* pSmall = (byte*)(void*)smallData.Scan0;
                byte* pBig = (byte*)(void*)bigData.Scan0;
                
                int smallOffset = smallStride - smallBmp.Width * 3;
                int bigOffset = bigStride - bigBmp.Width * 3;

                bool matchFound = true;
                
                for (int y = 0; y < bigHeight; y++)
                {
                    for (int x = 0; x < bigWidth; x++)
                    {
                        byte* pBigBackup = pBig;
                        byte* pSmallBackup = pSmall;

                        //Look for the small picture.
                        for (int i = 0; i < smallHeight; i++)
                        {   
                            int j = 0;
                            matchFound = true;
                            for (j = 0; j < smallWidth; j++)
                            {
                                if (pBig[0] != pSmall[0])
                                {
                                    matchFound = false;
                                    break;
                                }
                                pBig++;
                                pSmall++;
                            }

                            if (!matchFound) break;
                            
                            //We restore the pointers.
                            pSmall = pSmallBackup;
                            pBig = pBigBackup;

                            //Next rows of the small and big pictures.
                            pSmall += (smallWidth  + smallOffset) * (1 + i);
                            pBig += (bigWidth * 3 + bigOffset) * (1 + i);
                        }

                        //If match found, we return.
                        if (matchFound)
                        {
                            location.X = x;
                            location.Y = y;
                            break;
                        }
                        //If no match found, we restore the pointers and continue.
                        else
                        {
                            pBig = pBigBackup;
                            pSmall = pSmallBackup;
                            pBig+=3;                            
                        }                                                
                    }

                    if (matchFound) break;

                    pBig += bigOffset;
                }
            }

            bigBmp.UnlockBits(bigData);
            smallBmp.UnlockBits(smallData);

            return location;
        }

Questionconverting an image in black and white Pin
shekhar25839523-Jul-09 0:16
shekhar25839523-Jul-09 0:16 
AnswerRe: converting an image in black and white Pin
Manas Bhardwaj23-Jul-09 0:20
professionalManas Bhardwaj23-Jul-09 0:20 
AnswerRe: converting an image in black and white Pin
Tamer Oz23-Jul-09 2:03
Tamer Oz23-Jul-09 2:03 
QuestionHelp on how to develop an sms web portal Pin
abbah22-Jul-09 23:59
abbah22-Jul-09 23:59 
AnswerRe: Help on how to develop an sms web portal Pin
ravindarp23-Jul-09 0:31
ravindarp23-Jul-09 0:31 
QuestionGeneric Question Pin
Programm3r22-Jul-09 22:00
Programm3r22-Jul-09 22:00 
AnswerRe: Generic Question Pin
J4amieC22-Jul-09 22:23
J4amieC22-Jul-09 22:23 
QuestionRe: Generic Question Pin
Programm3r22-Jul-09 22:58
Programm3r22-Jul-09 22:58 
AnswerRe: Generic Question Pin
J4amieC22-Jul-09 23:17
J4amieC22-Jul-09 23:17 
GeneralRe: Generic Question Pin
Programm3r22-Jul-09 23:41
Programm3r22-Jul-09 23:41 
GeneralRe: Generic Question Pin
J4amieC22-Jul-09 23:55
J4amieC22-Jul-09 23:55 
GeneralRe: Generic Question Pin
Programm3r23-Jul-09 0:00
Programm3r23-Jul-09 0:00 
GeneralRe: Generic Question Pin
harold aptroot22-Jul-09 23:12
harold aptroot22-Jul-09 23:12 
GeneralRe: Generic Question Pin
Programm3r22-Jul-09 23:30
Programm3r22-Jul-09 23:30 
GeneralRe: Generic Question Pin
J4amieC22-Jul-09 23:33
J4amieC22-Jul-09 23:33 
GeneralRe: Generic Question Pin
Programm3r22-Jul-09 23:45
Programm3r22-Jul-09 23:45 
GeneralRe: Generic Question Pin
J4amieC22-Jul-09 23:55
J4amieC22-Jul-09 23:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.