While in the previous tutorial we learnt to detect if there are similarities between two images, but what if we would like to know how similar they are?
We are going to see in this tutorial, how starting from the features of the two images we can define a percentage of similarity from 0 to 100, where 0 it means they’re completely different, while 100 they are equal, even if they have different size.
Calculate percentage of how similar two images are:
In the code below from Line 35 to Line 46 we detect how similar two images are.
Considering that high quality images (high quality in this case it means high number of pixels) might have thousands of features, so thousands of keypoints while low quality images might have only a few hundreds, we need to find a proportion between the matches found and the keypoints.
We check the number of keypoints of both images using len(kp_1) and len(kp_2) and we take the number of the images that has less keypoints.
# Define how similar they are
number_keypoints = 0
if len(kp_1) <= len(kp_2):
number_keypoints = len(kp_1)
else:
number_keypoints = len(kp_2)
print("Keypoints 1ST Image: " + str(len(kp_1)))
print("Keypoints 2ND Image: " + str(len(kp_2)))
Finally we divide the good matches by the number of keypoints. We will get a number between 0 (if there were no matches at all) and 1 (if all keypoints were a match) and then we multiply them by 100 to have a percentage score.
print("GOOD Matches:", len(good_points))
print("How good it's the match: ", len(good_points) / number_keypoints * 100, "%")

Hi there, I’m the founder of Pysource.
I’m a Computer Vision Consultant, developer and Course instructor.
I help Companies and Developers to build efficient computer vision software.
very useful, thanks
put the line looks uwfull for me, can you replace the line with some number, similar number with similar color in same place with a tiny mark (circle or retangle) will be nice.
What is the meaning of the distance in the matches?
The distance between where to where?
What is the difference between distance m to distance n?
Thank you. 🙂
I can’t use the cv2 in Python 3.6
And what’s the difference between cv2 and opencv-python
And how I use the last one as cv2 in your source code
Thanks
The sift function is not working , it shows me an error like this
cv2.error: OpenCV(4.1.2) C:\projects\opencv-python\opencv_contrib\modules\xfeatures2d\src\sift.cpp:1207: error: (-213:The function/feature is not implemented) This algorithm is patented and is excluded in this configuration; Set OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function ‘cv::xfeatures2d::SIFT::create’
How can I solve this?
install openCv version 3.3.0.10
Can’t dowload the code
Thank you for the wonderful tutorials. They are very clear and will help me a lot in my work. Just so you know I had a problem – the line
print(“How good it’s the match: “, (len(good_points) / number_keypoints * 100)
was only giving me 0 or 100 percent. It was probably something in my instance of python, but I was able to fix it by rewriting it as
print(“How good it’s the match: “, (len(good_points)*1.0) / number_keypoints * 100).
Just in case it is useful to anyone else 🙂 Thank you again for the work.
Hi,
I offered a bad solution before – multiplying by 1.0, while it turns the number into a float, also inverts the results. I think it might be better to put from __future__ import division at the beginning of the script.