We have seen in the previous tutorial if two images are completely equal (same size, same channels, and same pixels values).
But what if they’re not equal?
The subtraction method doesn’t work anymore, as we can’t subtract pixels from images that have different sizes, we would get an error.
In this article you will learn how to compare and find similarities between two images when they’re similar but not exactly identical.
For example you can take an Image and compare it with the same image that has different filters applied on.
I took as example the image below (Golden Bridge in San Francisco), then I applied to it different filters or edited them, as you can see in the images below.

These images below are a few examples of the editing that were made to the original picture: blue, blurred, cartoonized, exposured, mixed colors, old photo, overlay, portion of image, rotated, sharpened, sunburst, textured.



Look for image similarities if they’re not equal:
The approach we’re going to use to find similarities if the images are not equal is Feature detection and Feature matching.
We find the features of both images.

On line 19 we load the sift algorithm.
On lines 20 and 21 we find the keypoints and descriptors of the original image and of the image to compare.
# 2) Check for similarities between the 2 images sift = cv2.xfeatures2d.SIFT_create() kp_1, desc_1 = sift.detectAndCompute(original, None) kp_2, desc_2 = sift.detectAndCompute(image_to_compare, None)
On lines 22, 23 and 24 we load FlannBasedMatcher which it the method used to find the matches between the descriptors of the 2 images.
On line 26 we find the matches between the 2 images. We’re storing the matches in the array ‘matches’.
The array will contain all possible matches, so many false matches as well.
]index_params = dict(algorithm=0, trees=5) search_params = dict() flann = cv2.FlannBasedMatcher(index_params, search_params) matches = flann.knnMatch(desc_1, desc_2, k=2)
In this part we apply the ratio test to select only the good matches.
The quality of a match is define by the distance. The distance is a number, and the lower this number is, the more similar the features are.
By applying the ratio test we can decide to take only the matches with lower distance, so higher quality.
If you decrease the ratio value, for example to 0.1 you will get really high quality matches, but the downside is that you will get only few matches.
If you increase it you will get more matches but sometimes many false ones.
good_points = [] ratio = 0.6 for m, n in matches: if m.distance < ratio*n.distance: good_points.append(m) print(len(good_points)) result = cv2.drawMatches(original, kp_1, image_to_compare, kp_2, good_points, None)
In this last part we show all the images on the screen.
cv2.imshow("result", result)
cv2.imshow("Original", original)
cv2.imshow("Duplicate", image_to_compare)
cv2.waitKey(0)
cv2.destroyAllWindows()

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.
i want to know ,when two similar images are slightly shifted means how could you find the difference of them?? please share me the solutions
Hi, nice vídeo, but i have a problem with my codes.
Can you Help Me?
C:\Users\v_lop\Desktop\img_similar>cv2.error: OpenCV(3.4.3) 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’
It’s not working with the latest version of Opencv 3.4.3, there are a few options on the web how to install it enabling the SIFT and SURF algorithm.
I suggest you tu install an older version of Opencv and opencv-python-contrib , version 3.4.2 works fine with the source code of this tutorial.
I could not understand the m.distance() part! Could you please shed a bit more light on that?
Apart from that , another fantastic tutorial! I am really grateful to you 🙂
How can I find the angle of difference from two images? ie; a reference image and a live image with a certain angle?
I am getting this error and i dont have any idea how to resolve this kindly help please
kp_1, desc_1 = sift.detectAndCompute(original, None)
cv2.error: OpenCV(3.4.2) C:\projects\opencv-python\opencv\modules\core\src\matrix.cpp:367: error: (-215:Assertion failed) u != 0 in function ‘cv::Mat::create’
hello,there,Dose the vision of OPENCV-contrib infulence the result of picture.
i change the version ,result goes wber bad .
Hi there. this code took about ~5s to complete the test.
Is there a way to make it faster ?
It was going great until I got this error:
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
I tried to install older version but that doesn’t work either:
ERROR: Could not find a version that satisfies the requirement opencv-contrib-python==3.4.2.17 (from versions: 3.4.8.29, 3.4.9.31, 4.1.2.30, 4.2.0.32)
So I’m stuck. Any suggestions?
Thanks
Hi, thanks for your video!
while running your code i encountered an error:
AttributeError: module ‘cv2.cv2’ has no attribute ‘xfeatures2d’
i tried to re-install opencv-contrib-python and opencv-pyhton
any insight on what the issue may be?
thanks!
Same error as above. SIFT and SURF aren’t available in OpenCV newer than 3.4.2.16 anymore, it seems.
How to compare equality of 2 images instead?
It’s possible use SIFT and SURF using a old version of OpenCV (<=3.4.2.16) or use a recent version of OpenCV compiled with the flag -DOPENCV_ENABLE_NONFREE=ON, someone compiled a recent version of python-opencv and opencv-python-contrib in pip with nonfree
pip install opencv-python-nonfree opencv-contrib-python-nonfree
Hi sir
Thank you very much, it is a great lesson for me. Could you please tell me what is the key point? Do the key points are equal to the total pixels of the image sir?
The patent on the SIFT algorithm has expired as of 2020-03-07. SIFT has been moved back to the main repository in OpenCV version 4.4.0.
Python 3.6.6 |Anaconda, Inc.| (default, Oct 9 2018, 12:34:16)
[GCC 7.3.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import cv2
>>> print(cv2.__version__)
4.4.0
>>> sift = cv2.SIFT_create()
>>>