Skip to content

Images Comparison · Tutorials

Detect how similar two images are with Opencv and Python

Access community, courses and source codes
Logo

AI Vision Academy

Access the code of this tutorial, computer vision courses and an exclusive community on AI Vision Academy

  • Access to over 50+ source codes from Pysource.com/blog
  • Dedicated video courses about computer vision
  • Access to an exclusive community of professionals
  • Real-World AI Projects – Get hands-on experience building practical AI Computer Vision solutions with a structured path.
  • Monthly Coaching Calls – get support and any of your questions answered

Subscribe to our newsletter to learn more

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, "%")

9 comments

  1. 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.

  2. 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. 🙂

  3. 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

  4. 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?

  5. 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.

  6. 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.

Join the discussion