2

I have more than 3000 image, 1500 small image & 1500 large image. i want to check if small image is the same as large image as an example large image & small image.

I use the following code for check images but return false result for all images ( same or not ) can anyone help me to solve this?

    public static bool IsEqual(this BitmapImage image1, BitmapImage image2)
{
    if (image1 == null || image2 == null)
    {
        return false;
    }
    return image1.ToBytes().SequenceEqual(image2.ToBytes());
}

public static byte[] ToBytes(this BitmapImage image)
{
    byte[] data = new byte[] { };
    if (image != null)
    {
        try
        {
            var encoder = new BmpBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(image));
            using (MemoryStream ms = new MemoryStream())
            {
                encoder.Save(ms);
                data = ms.ToArray();
            }
            return data;
        }
        catch (Exception ex)
        {
        }
    }
    return data;
}
Azak
  • 240
  • 1
  • 4
  • 15
  • 1
    Classifying the same to a computer is a very difficult task, I'm not sure this will be very easily doable as you'd have to compare an area of pixels to just one pixel. – Draken May 10 '16 at 10:45
  • Are these images exactly the same file or they are "very simmilar images"? If they're exact copies then I would use a CRC/checksum comprobation instead of comparing all the content after loading it in a bitmap (faster). If they're not exactly the same file the compression can change a bit the content, so an exact comparison will not work. If you know two of these images which would be the same do an "fc file1 file2" on the console, it will tell you if the files are exactly the same. – Gusman May 10 '16 at 10:50
  • If smallimage1 is a copy of largeimage1 you can get hash from those file and compare it. But if size of images are different this is not a easy work – mohsen May 10 '16 at 10:59
  • You said images are same but why you said large and small images? – mohsen May 10 '16 at 11:01
  • 1
    @mohsen Sorry for that are similar but in different resolution – Azak May 10 '16 at 11:03

1 Answers1

3

Because of the size differences you'll need to check how much these images looks similar.

To measure similarity you'll need a correlation technique.

I think it's best to perform the following steps:

  • Resize large image to size of small image
  • Substract each x,y pixel of the small image from the corresponding x,y pixel of the resized image and sum the absolute value (with respect to the RGB values).
  • Divide the total by the total pixel count
  • The sum is a measurement for similarity. Define a threshold value of which amount of "similarity" is acceptable.

You'll need to play around a bit because you'll get some error's. But tweaking the threshold would give you a 90%+ accuracy.

If you want a more mathematical approach, check wikipedia for correlation and try to implement a better technique:

https://en.wikipedia.org/wiki/Correlation_and_dependence

Stefan
  • 14,240
  • 9
  • 51
  • 69