Skip to content

Images Comparison · Tutorials

Check if two images are equal 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

Finding if two images are equal with Opencv, is a quite simple operation.

There are 2 fundamental elements to consider:

  1. The images have both the same size and channels
  2. 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()

 

15 comments

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

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

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

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

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

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

Join the discussion