Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify that two images are exactly identical?

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 cv2.compare 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 = cv2.compare(frame1,frame2,0)
compare2 = cv2.compare(frame1,frame3,0)

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

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

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

cv2.waitKey(0)
cv2.destroyAllWindows()
like image 210
blim Avatar asked Jan 08 '16 03:01

blim


1 Answers

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

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

like image 103
Joran Beasley Avatar answered Sep 23 '22 12:09

Joran Beasley