0

I need to check how similar two images are. I need to have this in a number; a percentage.

public static void Compare(Bitmap bmp1, Bitmap bmp2)
{
    int width;
    int height;
    if (bmp1.width < bmp2.width)
    {
        width = bmp1.width;
    }
    else
    {
        width = bmp2.width;
    }
    if (bmp1.heigth < bmp2.width)
    {
        heigth = bmp1.heigth;
    }
    else
    {
        heigth = bmp2.heigth;
    }
    int contador = 0;

    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < height; y++)
        {
            if (bmp1.GetPixel(x, y) == bmp2.GetPixel(x, y))
            {
                contador++
            }
        }
    }

    Messagebox.Show("The percent of similar is:" + (contador / (height * width) * 100).ToString());
}

This works perfectly while the images are not of different size, because for example when putting a white line to one of the 2 images the percentage of comparison is 0% that if I put it at the beginning and if I put it at the end then it does not take it into consideration.

I surfed quite looking but I always find examples of images that have the same height and length.

Is there any way to see how much similarity has one image of the other? The resources are not problems since usually the images are quite small.

Nyerguds
  • 5,360
  • 1
  • 31
  • 63
  • What are your constraints? I mean, how are images with different widths and heights "similar"? Do you have to search the larger for the smaller image, etc? If this is for image recognition, you might want to look into neural networks. – ProgrammingLlama Apr 16 '18 at 06:17
  • 1
    Yes, the first question here is : what exactly is "similar" ? – Fabjan Apr 16 '18 at 06:30
  • 1
    @CristianGerani look at my answer at https://stackoverflow.com/questions/35151067/algorithm-to-compare-two-images-in-c-sharp/35153895#35153895 – fubo Apr 16 '18 at 06:40
  • Cristian this is an english site. – Cleptus Apr 16 '18 at 06:45
  • For example, we have an image of cats of the same color and one cat has mustaches and the other does not, I would like to see the difference @bradbury , sorry – Cristian Gerani Apr 16 '18 at 06:48
  • 1
    `we have an image of cats of the same color and one cat has mustaches` this just blew a simple code solution out of the water. you will need to use AI or some type of neural network – TheGeneral Apr 16 '18 at 06:50
  • @TheGeneral I feel like I'm getting into code that I still do not understand well ..., you could leave a small example to guide me? – Cristian Gerani Apr 16 '18 at 06:56
  • Note, `GetPixel` is a _very_ slow operation since it needs to lock memory on every single pixel you retrieve. Consider looking into `Lockbits` and `Marshal.Copy` to get the entire image contents as byte array in advance. – Nyerguds Apr 16 '18 at 07:06
  • 1
    Also, as noted, on the subject of what "image similarity" means... this code will probably give 0% similarity between an image and the same image re-saved as jpeg. – Nyerguds Apr 16 '18 at 07:07
  • I would: start by resizing BOTH images to a MUCH smaller and same size. Then loop over the pixels and compare not for identity but for similarity of colors. This will still not work if one image is skewed, titlted or tinted. For these case one would have to normalize the image. Non-trivial. Which is why there are libs to help. – TaW Apr 16 '18 at 07:58
  • [Here](https://stackoverflow.com/questions/26224095/how-to-find-the-difference-between-two-images/26225153?s=2|43.9828#26225153) is an exmaple of fast diffence images and [here](https://stackoverflow.com/questions/27374550/how-to-compare-color-object-and-get-closest-color-in-an-color/27375621?s=1|54.0034#27375621) one about color comparison. – TaW Apr 16 '18 at 08:03

0 Answers0