开发者

Quickly determining of two bitmaps are the same?

How c开发者_如何学Can I (as fast as possible) determine if two bitmaps are the same, by value, and not by reference? Is there any fast way of doing it?

What if the comparison doesn't need to be very precise?


you can check the dimensions first - and abort the comparison if they differ.

For the comparison itself you can use a variaty of ways:

  • CRC32
    very fast but possibly wrong... can be used as a first check, if it differs they are dfferent... otherwise further checking needed
  • MD5 / SHA1 / SHA512
    not so fast but rather precise
  • XOR
    XOR the image content... abort when the first difference comes up...


You can just use a simple hash like MD5 to determine if their contents hash to the same value.


You will need a very precise definition of "not very precise".

All the Checksum or Hash methods already posted work for an exact (pixel and bit) match only.

If you want an answer that corresponds to "they look (somewhat) alike" you will need something more complicated.

  • some preprocessing based on their aspect ratio. Can a 600x400 picture be like a 300x300 one?
  • use a graphics algorithm to scale them down to, say, 100x100.
  • Also reduce the colors.
  • Then compare the results pixel by pixel (and set an error treshold).


Try comparing the hashs of the two files

using System;
using System.IO;
using System.Security.Cryptography;

class FileComparer
{
    static void Compare()
    {
        // Create the hashing object.
        using (HashAlgorithm hashAlg = HashAlgorithm.Create())
        {
            using (FileStream fsA = new FileStream("c:\\test.txt", FileMode.Open),
                fsB = new FileStream("c:\\test1.txt", FileMode.Open)){
                // Calculate the hash for the files.
                byte[] hashBytesA = hashAlg.ComputeHash(fsA);
                byte[] hashBytesB = hashAlg.ComputeHash(fsB);

                // Compare the hashes.
                if (BitConverter.ToString(hashBytesA) == BitConverter.ToString(hashBytesB))
                {
                    Console.WriteLine("Files match.");
                } else {
                    Console.WriteLine("No match.");
                }
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜