How to verify that two images are exactly identical?

compare two images for similarity python
algorithm to compare two images
compare two images and highlight differences python
compare two images for differences online
face recognition compare two photos python
how to calculate the difference between two images
python pillow compare images
intersection of two images python

I am writing an opencv program where I track position of an object by use of a usb camera. To make sure I get as high frame rate as possible with the camera I am using I made a threaded process that read the images from the camera. The image processing is done in another loop which also writes the object position to the file.

Now I want a way to avoid processing the same frame multiple times. So I thought I could compare the image just processed with that available from the the video stream thread.

First I tried to use if frame1 == frame2, but got error message that "the truth value of an array with more than one element is ambiguous. Use a.any() or a.all()." After some googling I found and the flag CMP_EQ. Made a sample code, and made it work in some way. However, my question is. How could this be done in an easier or better way?

import cv2

cv2.namedWindow('image1', cv2.WINDOW_NORMAL)
cv2.namedWindow('image2', cv2.WINDOW_NORMAL)

frame1 = cv2.imread("sample1.png")
frame2 = frame1
frame3 = cv2.imread("sample2.png")

compare1 =,frame2,0)
compare2 =,frame3,0)

cv2.imshow('image1', compare1)
cv2.imshow('image2', compare2)

if compare1.all():
    print "equal"
    print "not equal"

if compare2.all():
    print "equal"
    print "not equal"


How about giving your Images an index?


class Frame
   cvImage img;
   uint    idx;

Than simply check if the current index is greater than the last one you processed. It is simple and definitely faster than any image processing based approach.

How to check whether two images are similar?, The quickest way to determine if two images have exactly the same contents is to get the difference between the two images, and then calculate the bounding  findimagedupes is a commandline utility which performs a rough "visual diff" to two images. This allows you to compare two images or a whole tree of images and determine if any are similar or identical. On common image types, findimagedupes seems to be around 98% accurate.

open("image1.jpg","rb").read() == open("image2.jpg","rb").read()

should tell you if they are exactly the same ...

How to know if two images are the same?, Check formats. If the same => Perform precise comparison, pixel by pixel. If different formats do this: Do not compare RGB (red,green  The quickest way to determine if two images have exactly the same contents is to get the difference between the two images, and then calculate the bounding box of the non-zero regions in this image. If the images are identical, all pixels in the difference image are zero, and the bounding box function returns None.

I was doing something close to what you are doing; I was trying to get the difference. I used the subtract function. It may help you.


import cv2
import numpy as np
a = cv2.imread("sample1.png")
b = cv2.imread("sample2.png")
difference = cv2.subtract(a, b)    
result = not np.any(difference)
if result is True:
    print "Pictures are the same"
    cv2.imwrite("ed.jpg", difference )
    print "Pictures are different, the difference is stored as ed.jpg"

Check if two images are equal with Opencv and Python, Learn how to compare two images for similarity using Mean Squared Error and arbitrary stamp images and compare them to determine if they are identical, Exactly which method you use for image comparison is highly  Open one of the images in GIMP or Photoshop. Add the second image as a new layer on top of the first one. Set the blend mode of the top layer to "Difference" In the resulting image the black parts show where the original images are identical and anything lighter shows differences.

You can compare the size of two image files as the first level of check for reduced computational complexity. With compression, it is highly unlikely for two different image files to have the same size to the accuracy of the number of bytes. With equal file size, you can then compare the image contents.

How-To: Python Compare Two Images, duplicate = cv2.imread(“images/duplicate.jpg”)[/python]. We loaded the two images we can start making the comparison. First we check if they  Resize both the images to the lowest size diamention. Apply edge detection on each image resulting black and white image (or array of 0 and 1) Compare resulting bitmaps (keep first one still, and rotate the second one by 90 degrees 3 times) and calculate % matching pixcels and get the heighest value.

You should try something like this.

import cv2
import numpy as np

original = cv2.imread("rpi1.jpg")
duplicate = cv2.imread("rpi2.jpg")

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

    print("images are different")

Check if two images are equal with Opencv and Python, Simply drop the first image you wish to compare into the left box, and the other image in the right Comparing two images can be quite a pain. The images will automatically be resized and scaled to the same size before being compared. Use a for loop to check if any of these sizes are the same. if they are the same size, compare a byte of one file to a byte of the other file. If the two bytes are the same, move onto the next byte. If a difference is found, return that the files are different.

Online Image Diff, I want to know if someone has used imagechops to compare if 2 images are exactly the same. I searched for information but couldn't solve my  Microsoft Word is an excellent program to manage your documents. With Microsoft Word, not only can you create and edit documents, but you can even compare two documents for differences. For example, if you are a student and have two versions of the same essay, you can easily view and compare the two docs. If … How to compare two documents in Microsoft Word Read More »

Problem comparing if two images are different ( imagechops , Checking Equality. Grafika can also do a pixel by pixel comparison to determine if two images are exactly the same using the equal() method: require_once '  That’s why if the images are equal, the result will be a black image (which means each pixel will have value 0). A colored image has 3 channels (blue, green and red), so the cv2.subtract() operation makes the subtraction for each single channel and we need to check if all the three channels are black. If they are, we can say that the images

Compare Images, The ability to compare two or more images, or finding duplicate images in a large To get a better idea of exactly how different the images are, you are probably 

  • Have you considered hashing the images and comparing ?
  • How can I hash an image?
  • Using hashlib?.. hashlib.md5(open(full_path, 'rb').read()).hexdigest()
  • That is the way to go. How would you do it in Python? Is there an opencv function for this, or should I just make a code to increase an index for every frame the stream returns?
  • I would do it in Python like in the Example. Make a class that contains an index and the image, this way you can later add more information for Example if you capture from two cameras you can add an source identifier. Then just count the images in your capture loop. I think OpenCV has nothing build in.
  • I forgot to write that I am quite new to programming, so I must admit I don't understand the example you provided, can you explain a little more? Thanks.
  • All you need to know is the concept of classes:'s_Python_Tutorial/Classes
  • Hmm, I have the basic understanding of a class, but I don't understand your pseudocode and how you mean it could be implemented and used. I like your idea, and I implemented something that now works by counting each frame, and avoid the main loop from processing if the counter value has not increased. However I think your solution is way more elegant if I was able to understand it...
  • I think this works only when the PNG files are written using the same compression level, otherwise .read() will report they are different.
  • because they are not identical ... the compression level effects the pixels ... at least as i understand it... I might be wrong ...
  • That will result to an array with the difference for each pixel between a and b. In order to find out whether the two images are the same, you'd then have to compare difference to an array of the same size full of zeros, which is the same problem as comparing a and b.
  • Correct and Thank you, I should have added the following res = not np.any(difference) this would result true if they are the same. I will update the code I posted.
  • A very simple solution would be np.all(a == b), which is very close to what the OP was doing. However, Mailerdaimon's approach of not even comparing the images is more appropriate here.
  • Independent of the OP's context, this is very helpful for answering the short question posed by the topic. One thing to note, though, is that while this is checking for literal equality, it does not check for semantic equality in images containing transparency (e.g. it doesn't provide a way to say that two pixels' render values are equal if their alphas are 0; if the RGB data is not equal, this method won't catch that the alpha channel overrides the RGB data's relevance)
  • Could you provide some explanation for your answer?