Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello can anyone help me please, i cannot seem to figure out why this code can't find the image i'm looking for if they are located on more than 50% of my screen and works perfectly and locate perfect if the image is on the left 50% of my screen. :/

C#
private int[,] isImageThere(Bitmap fullImage, Bitmap smallImage)
{
    Color[,] fullPixels = ImageToArray(fullImage);
    Color[,] smallPixels = ImageToArray(smallImage);
    int R = 0;
    int G = 0;
    int B = 0;
    try
    {
        // Iterate through every row
        for (int fullColumn = 0; fullColumn < fullImage.Width; fullColumn++)
        {
            //1200 error
            if (fullColumn >= fullImage.Width)
                break;
            // Iterate through every pixel in the row
            for (int fullRow = 0; fullRow < fullImage.Height; fullRow++)
            {
                if (fullRow >= fullImage.Height)
                    break;
                // If the current pixel on the full image is the
                // same as the top left pixel on the small image...
                if (/*(fullPixels[fullRow, fullColumn] == smallPixels[0, 0])*/
                    (Convert.ToInt16(fullPixels[fullColumn, fullRow].R) + Convert.ToInt16(fullPixels[fullColumn, fullRow].G) + Convert.ToInt16(fullPixels[fullColumn, fullRow].B) - (Convert.ToInt16(smallPixels[0, 0].R) + Convert.ToInt16(smallPixels[0, 0].G) + Convert.ToInt16(smallPixels[0, 0].B))) >= -2 &&
                    (Convert.ToInt16(fullPixels[fullColumn, fullRow].R) + Convert.ToInt16(fullPixels[fullColumn, fullRow].G) + Convert.ToInt16(fullPixels[fullColumn, fullRow].B) - (Convert.ToInt16(smallPixels[0, 0].R) + Convert.ToInt16(smallPixels[0, 0].G) + Convert.ToInt16(smallPixels[0, 0].B))) <= 2
                    )
                {
                    // So far, we haven't found a mismatched pixel
                    bool badPixel = false;
                    try
                    {
                        // Iterate through every row in the small image
                        for (int smallRow = 0; smallRow < smallImage.Width; smallRow++)
                        {
                            // Iterate through every pixel in the row
                            for (int smallColumn = 0; smallColumn < smallImage.Height; smallColumn++)
                            {
                                // If the pixel on the full image isn't the same as on the small image...
                                R = (fullPixels[fullColumn + smallColumn, fullRow + smallRow].R - smallPixels[smallColumn, smallRow].R);
                                G = (fullPixels[fullColumn + smallColumn, fullRow + smallRow].G - smallPixels[smallColumn, smallRow].G);
                                B = (fullPixels[fullColumn + smallColumn, fullRow + smallRow].B - smallPixels[smallColumn, smallRow].B);
                                if (R < 0)
                                    R = R * -1;
                                if (G < 0)
                                    G = G * -1;
                                if (B < 0)
                                    B = B * -1;
                                //if (fullPixels[fullColumn + smallColumn, fullRow + smallRow] != smallPixels[smallColumn, smallRow])
                                if (
                                    (R >= 2) &&
                                    (G >= 2) &&
                                    (B >= 2)
                                   )
                                {
                                    // There is a mismatched pixel so set bad pixel to true
                                    badPixel = true;
                                    break;
                                }
                                else
                                    badPixel = false;
                                R = 0;
                                G = 0;
                                B = 0;
                            }
                            // If we found a bad pixel, break out of outer loop
                            if (badPixel == true)
                            {
                                break;
                            }
                        }
                    }
                    catch { }
                    // If we passed through all pixels in small image, return location
                    if (badPixel == false)
                    {
                        int[,] res = new int[,] { { fullColumn, fullRow } };
                        return res;
                    }
                }
            }
        }
    }
    catch { }
    // If we looped until here, not match was found
    return null;
}
Posted
Updated 11-Feb-11 15:15pm
v2
Comments
Sergey Alexandrovich Kryukov 11-Feb-11 21:21pm    
No explanation. How it supposed to work, what you want to achieve?

The two line below will raise a IndexOutOfException.
SQL
//1200 error
 if (fullColumn >= fullImage.Width)
     break;


C#
if (fullRow >= fullImage.Height)
                    break;


It should be like this;
C#
if (fullColumn >= fullImage.Width-smallImage.Width)
                break;


your algorithm is very very slow. Every Image has some key point(also corner point), that is, the highest color change ratio point. So you can use same algorithm extract the corner-point-constellation than match the constellation.
 
Share this answer
 
You can use this method for re size image
public Bitmap ReSizeBitmap(Bitmap btp, int width, int height)
        {
            int originalW = btp.Width;
            int originalH = btp.Height;

            Bitmap bmp = new Bitmap(width, height);
            //create a new graphic from the Bitmap
            Graphics graphic = Graphics.FromImage((Image)bmp);
            graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //draw the newly resized image
            graphic.DrawImage(btp as Image, 0, 0, width, height);
            graphic.Dispose();
            //return the image
            return bmp;
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900