OpenCV - Fastest method to check if two images are 100% same or not

怎甘沉沦 提交于 2020-01-23 01:35:10

问题


There are many questions over here which checks if two images are "nearly" similar or not.

My task is simple. With OpenCV, I want to find out if two images are 100% identical or not.

They will be of same size but can be saved with different filenames.


回答1:


You can use a logical operator like xor operator. If you are using python you can use the following one-line function:

Python

def is_similar(image1, image2):
    return image1.shape == image2.shape and not(np.bitwise_xor(image1,image2).any())

where shape is the property that shows the size of matrix and bitwise_xor is as the name suggests. The C++ version can be made in a similar way!

C++

Please see @berak code.


Notice: The Python code works for any depth images(1-D, 2-D, 3-D , ..), but the C++ version works just for 2-D images. It's easy to convert it to any depth images by yourself. I hope that gives you the insight! :)

Doc: bitwise_xor

EDIT: C++ was removed. Thanks to @Micka and @ berak for their comments.




回答2:


the sum of the differences should be 0 (for all channels):

bool equal(const Mat & a, const Mat & b)
{
    if ( (a.rows != b.rows) || (a.cols != b.cols) )
        return false;
    Scalar s = sum( a - b );
    return (s[0]==0) && (s[1]==0) && (s[2]==0);
}



回答3:


If they are same files except being saved in different file-names, you can check whether their Checksums are identical or not.




回答4:


import cv2
import numpy as np
a = cv2.imread("picture1.png")
b = cv2.imread("picture2.png")
difference = cv2.subtract(a, b)    
result = not np.any(difference)
if result is True:
    print("Pictures are the same")
else:
    print("Pictures are different")


来源:https://stackoverflow.com/questions/23195522/opencv-fastest-method-to-check-if-two-images-are-100-same-or-not

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!