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 founder of Pysource.
I help Companies, Freelancers and Students to learn easily and efficiently how to apply visual recognition to their projects.
For Consulting/Contracting Services, check out this page.
Most Read:
-
Train YOLO to detect a custom object (online with free GPU)
-
YOLO object detection using Opencv with Python
-
Detecting colors (Hsv Color Space) – Opencv with Python
-
How to install Python 3 and Opencv 4 on Windows
-
Check if two images are equal with Opencv and Python
-
YOLO V3 – Install and run Yolo on Nvidia Jetson Nano (with GPU)
-
How to install Dlib for Python 3 on Windows
-
Feature detection (SIFT, SURF, ORB) – OpenCV 3.4 with python 3 Tutorial 25