abilash
abilash

Reputation: 897

Programming image comparing

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;
        }

Upvotes: 0

Views: 698

Answers (1)

Jensen
Jensen

Reputation: 3538

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, ...

Upvotes: 1

Related Questions