I am trying to write code to find out if two images are the same. I was able to read the images in as BGR
and was able to subtract one from another. I need to do a check and see if the difference contains only zeros. I know there is a function called countNonZero
, but it only works on single channel, not 3 channels. An solution I had was looping through the entire matrix and checking if everything is zero, but that is too slow. Is there any efficient way of checking? Here is my code below.
int main(int argc, char** argv)
{
if(argc != 3)
{
printf("usage: DisplayImage.out <testImg.png> <copy.png>\n");
return -1;
}
Mat image;
Mat copy;
Mat output;
image = imread(argv[1], 1);
copy = imread(argv[2], 1);
absdiff(image, copy, output);
if(countNonZero(output) == 0)
{
cout << "Same" << endl;
}
else
{
cout << "Different" << endl;
}
imshow("outut", output);
waitKey();
return 0;
}