How To Measure Image Similarities In Python – DzTechno


First, I’m going to create a folder for the original image. Then, I’m going to place the comparison images in a dataset folder.

This red apple will be our original query image:

Image by Pexels from Pixabay.

We’ll compare it to other fruits:

Image by Shutterbug75 from Pixabay, Image by Varintorn Kantawong from Pixabay, Image by Bikki from Pixabay.

Now, let’s run the Python program to find out which one is the best match:

$ python3 measure_similarity.py red_apple.jpg

Output:

As you can see, the green apple is the winner. If you look at each result, you’ll find out that the next most similar image would be the red pear.

Now, let’s see what happens when it’s more difficult to predict the result. Let’s put a photo of a red tomato in the dataset folder:

+ Image by Sergey Nemo from Pixabay.

I would say that both the green apple and the tomato look like the red apple. Let’s check the outcome according to the math:

The most similar according to SSIM: {'dataset/green_apple.jpg': 0.9379929447852137}
The most similar according to RMSE: {'dataset/tomato.jpg': 0.012483303}
The most similar according to SRE: {'dataset/tomato.jpg': 58.294915655210346}

Note that I’ve omitted the rest of the output for the sake of brevity.

As you can see, the results differ this time.

To make it more exciting, let’s try to compare more diverse images. Since I like painting, I’ve taken photos of my own paintings for this experiment.

The original image:

Image created by the author.

The images dataset:

Images created by the author.

I’m curious about the result. I see two pictures with small houses that resemble the original one.

The most similar according to SSIM:  {'dataset/autumn_house.jpg': 0.8417289368701945}
The most similar according to RMSE: {'dataset/autumn_house.jpg': 0.019009253}
The most similar according to SRE: {'dataset/autumn_house.jpg': 59.83759986562872}

The house with the autumn landscape is the best match.

How about these?

Images created by the author.
The most similar according to SSIM:  {'dataset/northern_lights.jpg': 0.8761146738065145}
The most similar according to RMSE: {'dataset/girl.jpg': 0.014603245}
The most similar according to SRE: {'dataset/girl.jpg': 60.99421415749092}

At first glance, the result is a bit surprising to me. But when I take a closer look, the picture with the girl has a dark blue background like the original image. The painting of the northern lights has similar mountains in the background.

As you can see, the result differs based on different evaluation metrics.

Leave a Reply

Your email address will not be published. Required fields are marked *