Member-only story
How To Measure Image Similarities in Python
Use out-of-the-box evaluation metrics with the image-similarity-measures Python library
Nowadays, image similarity detection has become a hot topic. Modern sites like Pinterest use a search engine to return results based on visual similarities.
In this article, we’ll see how to use the image-similarity-measures
library to compare images. I picked this library because it provides various evaluation metrics we can use out of the box. It’s a quick and convenient way to start experimenting with this challenging topic.
Let’s dive into it!
Introduction
According to the library’s documentation, we can use eight different evaluation metrics to calculate the similarity between images:
- Root mean square error (RMSE),
- Peak signal-to-noise ratio (PSNR),
- Structural Similarity Index (SSIM),
- Feature-based similarity index (FSIM),
- Information theoretic-based Statistic Similarity Measure (ISSM),
- Signal to reconstruction error ratio (SRE),
- Spectral angle mapper (SAM)
- Universal image quality index (UIQ)
We’re not going to discuss how each of these metrics works. If you’re eager to learn how they work on a deeper level, check out the provided links.
Fortunately, all of the scary math has been implemented for us and we can start measuring the image similarities right away. We just need to call the name of the chosen evaluation metric and pass two images as parameters. For example:
rmse(query_image, data_image)sam(query_image, data_image)
Note that the default metric is psnr
.
There are two ways to use this package: You can execute commands in your terminal or write a separate Python script.
If you want to do a quick evaluation between two images, run this command in your terminal: