Finding if two images are equal with Opencv, is a quite simple operation.
There are 2 fundamental elements to consider:
- The images have both the same size and channels
- Each pixel has the same value
We’re going first to load the images. If you want to download the images I used, you can go at the end of this article to download the entire source code with the files.
First, we load the original and then the duplicate.
import cv2
import numpy as np
original = cv2.imread("imaoriginal_golden_bridge.jpg")
duplicate = cv2.imread("images/duplicate.jpg")
We loaded the two images we can start making the comparison.
First we check if they have the same size and channels. If they have the same size and channels, we continue further with the operation, if they don’t then that they’re not equal.
If they have the same sizes and channels, we proceed by subtracting them. The operation cv2.subtract(image1, image2) simply subtract from each pixel of the first image, the value of the corresponding pixel in the second image.
If for example the value of the pixel of the first image in the position (0, 0) is 255 and the value of the pixel in the corresponding position of the second image is also 255, it will be a simple subtraction: 255 – 255 which is equal to 0.
That’s why if the images are equal, the result will be a black image (which means each pixel will have a value of 0).
A colored image has 3 channels (blue, green, and red), so the cv2.subtract() operation makes the subtraction for every single channel and we need to check if all the three channels are black.
If they are, we can say that the images are equal.
# 1) Check if 2 images are equals
if original.shape == duplicate.shape:
print("The images have same size and channels")
difference = cv2.subtract(original, duplicate)
b, g, r = cv2.split(difference)
if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0:
print("The images are completely Equal")
If we want to display the images we can use the code below:
cv2.imshow("Original", original)
cv2.imshow("Duplicate", duplicate)
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.
hello, thanks for this tutorial, can you please tell how to do if two images have different sizes or different pixel
Please check the entire series, there are 4 videos.
You find your answer in the second one: https://pysource.com/2018/07/20/find-similarities-between-two-images-with-opencv-and-python/
Great Video! thanks for share your knowledge.
Greetings from Mexico
Great Video series sir!
Say I have scanned image of bank statement(submitted by customer) and standard template for that bank statement and I want to find if the submitted document is manipulated or correct. As the document is scanned I cannot use template matching and also the image contains text that is why I cannot use SIFT. Can you please tell me any way to proceed?
Image comparison in this case is not a good solution. You could change letters or numbers in the text and still have 99% of the matches.
You should develop a more advanced solution, like an OCR that can understand the text and then compare the text of both images.
What should I do when I get thiz error(AttributeError: ‘NoneType’ object has no attribute ‘shape’)
You need to specify path of the image. Example: original = cv2.imread(“c:/users/imgecomp/a.png” )
Even though I specified got the same error to me.
Hi, Nice tutorial +1
need a suggestion here, i have two same images, but the slight animation is going on in background like changing color from light blue to blue, as this method will always give difference as it’s comparing pixel colors, right??
but i need no difference here, as the content are same. so what should i use in this situation??
my thoughts –
1) shall i convert image into black/white and then compare?
2) or is there any opencv function which compares edges/boxes but not color?
Regards
If we have two image sketches of person one is Forensic sketch and other is a synthesized sketch then how we know (through coding) that the synthesized sketch is the same as that of the forensic sketch.
Hi Sam,
I am trying to solve a similar problem.I have got two docs one standard bank doc and one scanned doc by client which has client signature or some other modification.want to find whether the doc is modified or not by client
Hi Sam, Greetings.
It was a wonderful video. could you tell me how can I search an Image inside another.Basically my requirement is to search an image(scatter chart graph captured from Excel sheet via snipping tool) inside an another image( a png).
thank so much. it’s great
Hello thanks for this video..will you tell me whether this will work for…Human faces!?
hey how to indentify Near Duplicate images ?