Detect how similar two images are with Opencv and Python
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 Freelancers to easily and efficiently build Computer Vision Software.

Learn to build Computer Vision Software easily and efficiently.
This is a FREE Workshop where I'm going to break down the 4 steps that are necessary to build software to detect and track any object.
Sign UP for FREE