0

I try to compare 2 images. For this i use 2 PreentScreens doing one after another(it is idential). When I compare this screens using pixels comparing:

public static CompareResult Compare(Bitmap bmp1, Bitmap bmp2)
{
    CompareResult cr = CompareResult.ciCompareOk;

    if (bmp1.Size != bmp2.Size)
    {
        cr = CompareResult.ciSizeMismatch;
    }
    else
    {
        for (int x = 0; x < bmp1.Width 
             && cr == CompareResult.ciCompareOk; x++)
        {
            for (int y = 0; y < bmp1.Height 
                         && cr == CompareResult.ciCompareOk; y++)
            {
                if (bmp1.GetPixel(x, y) != bmp2.GetPixel(x, y))
                    cr = CompareResult.ciPixelMismatch;
            }
        }
    }
    return cr;
}

I get correct result that says - compare are identical, but it take a lot of time and when I'm try to hash this Bitmaps and compare it values - I get wrong result. When I compare image with itself - everything OK. What can be wrong? Here is code for hash comparing:

public enum CompareResult
        {
            ciCompareOk,
            ciPixelMismatch,
            ciSizeMismatch
        };

        public static CompareResult Compare(Bitmap bmp1, Bitmap bmp2)
        {
            CompareResult cr = CompareResult.ciCompareOk;

            //Test to see if we have the same size of image
            if (bmp1.Size != bmp2.Size)
            {
                cr = CompareResult.ciSizeMismatch;
            }
            else
            {
                //Convert each image to a byte array
                System.Drawing.ImageConverter ic = 
                       new System.Drawing.ImageConverter();
                byte[] btImage1 = new byte[1];
                btImage1 = (byte[])ic.ConvertTo(bmp1, btImage1.GetType());
                byte[] btImage2 = new byte[1];
                btImage2 = (byte[])ic.ConvertTo(bmp2, btImage2.GetType());

                //Compute a hash for each image
                SHA256Managed shaM = new SHA256Managed();
                byte[] hash1 = shaM.ComputeHash(btImage1);
                byte[] hash2 = shaM.ComputeHash(btImage2);

                //Compare the hash values
                for (int i = 0; i < hash1.Length && i < hash2.Length 
                                  && cr == CompareResult.ciCompareOk; i++)
                {
                    if (hash1[i] != hash2[i])
                        cr = CompareResult.ciPixelMismatch;
                }
            }
            return cr;
        }
abilash
  • 897
  • 3
  • 12
  • 32
  • How is this not "Debug my code" kind of question? I suggest you try making it into SO format, so that it useful for others. – Kromster Oct 03 '12 at 10:56
  • I know this isn't Code Review, but prefixes on enums (like `ci` in `ciCompareOk`) in C# are unnessasary as you must always reference the enum first. E.g. `CompareResult.CompareOk` so there is no room for confusion. – weston Oct 03 '12 at 11:39
  • Two equal hash codes doesn't mean that the images are equal. – Dan Byström Oct 03 '12 at 11:53
  • Ok How than I can compare images(faster than px by px)? It can be a litle diferent(brightness for example), but context must be the same. – abilash Oct 03 '12 at 11:58
  • 2
    You *must* do it pixel by pixel. And it will be fast once you throw out GetPixel and use .LockBits + pointers. – Dan Byström Oct 03 '12 at 12:03
  • Is it much more faster? Can you give more concrete digits? – abilash Oct 03 '12 at 12:07
  • 1
    I would *guess* that it will run at least 200 times as fast. – Dan Byström Oct 03 '12 at 12:11

1 Answers1

1

Possible duplicate of How to compare Image objects with C# .NET? which contains sample code.

Another useful link is this blog post by Dominic Green. The code is somewhat smaller and uses Base64 hashing instead of SHA256 hashing. This is significantly faster, but you should be aware that comparing images is not a light operation.

But returning to the question itself, how sure are you that both images are equal? Is it possible there might be a slight difference between the two images? Your mouse cursor moved, the clock display updated, ...

Community
  • 1
  • 1
Jensen
  • 3,498
  • 2
  • 26
  • 43
  • Example based on Base64 gave the same result(wrong). But I try to compare px by px yet couple times and it's work. Why hashing are not work? – abilash Oct 03 '12 at 11:08
  • When I made sreens i kept in mind all you comments. All should work (but it doesn't work). Px by px comparing work – abilash Oct 03 '12 at 11:14