¿Determinación rápida de dos mapas de bits son iguales?

How 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?

preguntado el 27 de agosto de 11 a las 16:08

4 Respuestas

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
  • GRATIS
    XOR the image content... abort when the first difference comes up...

Respondido 27 ago 11, 20:08

Marked as answer and voted up because it is ACTUALLY fast. All other suggestions included hashing, which is a very slow and retarded process, when performance is important. I went with CRC32. - Mathias Lykkegaard Lorenzen

A CRC is a (simple) Hash. Creating any kind of Hash requires a calculation on all bytes. If there are only 2 images than a direct comparison is a lot faster, even if the images are equal. A hash is only useful when you want to compare 1 image with a number of others. - Henk Holterman

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

Respondido 27 ago 11, 20:08

Comparing 5000X5000 images with this is not what I would call fast. Performance does matter, as stated in the original question. - Mathias Lykkegaard Lorenzen

It all depends on how much accuracy is desired. You have to pay the piper somewhere, either in accuracy or in speed. MD5 is normally considered a good balance between speed and accuracy when checking for file equality. - Brent Stewart

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

Respondido 27 ago 11, 20:08

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.");
                }
            }
        }
    }
}

Respondido 27 ago 11, 20:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.