Find similarity between two images in Python

In this tutorial, we are going to learn how to find similarities between two images using Python and OpenCV.

First of all, we have to import two libraries known as OpenCV and NumPy. We can download OpenCV and Numpy using the commands:

pip install opencv-python
pip install numpy

I gave you an additional command just in case if Numpy isn’t automatically installed when you install OpenCV. Also, you can check if they are installed successfully by writing Python in your terminal and then run the import cv2 command.

When you are done installing your packages you will have to import them by writing the code as:

import cv2
import numpy as np

Once the installation is completed you can finally begin writing your code. Make sure to load out the images and make sure they are in the same folder as your project, then proceed to write:

Image1 = cv2.imread("first_image_name.jpg")
Image2 = cv2.imread("second_image_name.jpg")

The cv2.imread is used to read or define the pictures that you are going to work on. The Image1 and Image2 are the two images that you are going to compare.

Check if the size of the images is equal

To find that the images are similar you need to show that their size is equal. You can do this by using the .shape method as shown below:

if Image1.shape == Image2.shape:
    print("Images have the same size")
else:
    print("Images do not have the same size")

This will print “Images have the same size” if the shape of both the images is equal otherwise it will print “Images do not have the same size”.

SIFT algorithm to check similarities between two images using OpenCV in Python

Proceed to check for similarities by using matching. This is where you will use the SIFT algorithm and initiate the SIFT detector. Be sure to find the key points and descriptors with SIFT.

sift = cv2.xfeatures2d.SIFT_create()
keypoint1, descriptor1 = sift.detectAndCompute(Image1,None)
keypoint2, descriptor2 = sift.detectAndCompute(Image2,None)

The key points are points in the image that stands out from the other image and each key point that you detect has an associated descriptor that accompanies it. Here we can use the brute force Matcher approach. It just takes the descriptor of one feature in the first set and matches it with all other features in the other set but this method ends up taking a lot of time for a larger dataset so here in place of Brute Force Matcher we will use FLANN based matcher. FLANN stands for Fast Library for Approximate Nearest Neighbors. It is faster than the Brute force matcher for the larger datasets and it can be implemented as:

index_parameter = dict(algorithm = 0,trees = 5)
search_parameter = dict()
flann = cv2.FlannBasedMatcher(index_parameter,search_parameter)

Where index_parameter is a dictionary that says which algorithm we use and search_parameter is an empty dictionary as we are going to use the default value of FLANN based Matcher. Once we have done this we can easily find matches by using:

matches = flann.knnMatch(descriptor1,descriptor2,k=2)

But matches that we find like this create a mess and usually aren’t accurate or useful so now we are only going to use the matches that are good or accurate enough to be useful. For this, we first create an empty array and then we use a for to loop to iterate through the matches,

good_point_array = []
for i, j in matches:
    if i.distance < 0.7*j.distance:
        good_point_array.append(i)
resultant_match = cv2.drawMatches(Image1, keypoint1, Image2, keypoint2, good_point_array, None)

Where i is from the first image and j is from the second image and the lower the distance between these elements the better is the match so we are taking into consideration only those which have a smaller distance and hence more accurate. We append the points with lower distances into the array that we created.
Finally, we use .imshow() to print out the expected result:

cv2.imshow("resultant_match",resultant_match)

Important Note: SIFT operator might not work for you because it is patented. So if you get an error like “cv2.cv2 has no attribute SIFT” in this case you just have to go back to your terminal and type the following commands:

pip uninstall opencv-python
pip install opencv-contrib-python==3.4.2.16

In this way, you will be able to encounter this error.

Leave a Reply

Your email address will not be published. Required fields are marked *