Ask Your Question

# Revision history [back]

If you are using c++, cv::subtract return a Mat with tons of pixels value but not a single value which tell you how many non zero pixels in the matrix. You can call countNonZero in c++ to count the number of non zero pixels.

//there are three high level ways to subtract matrix value in c++
//every solution have their pros and cons
auto const dst = img1 - img2;
if(countNonZero(dst) == 0){
cout<<"this is same picture"<<endl;
}else{
cout<<"this is not the same picture"<<endl;
}


If you are using c++, cv::subtract return give you a Mat with tons of pixels value but not a single value which tell you how many non zero pixels in the matrix. You can call countNonZero in c++ to count the number of non zero pixels.

//there are three high level ways to subtract matrix value in c++
//every solution have their pros and cons
auto const dst = img1 - img2;
if(countNonZero(dst) == 0){
cout<<"this is same picture"<<endl;
}else{
cout<<"this is not the same picture"<<endl;
}


If you are using c++, cv::subtract give you a Mat with tons of pixels value but not a single value which tell you how many non zero pixels in the matrix. You can call countNonZero in c++ to count the number of non zero pixels.

//there are three high level ways to subtract matrix value in c++
//every solution have their pros and cons
auto const dst = diff= img1 - != img2;
if(countNonZero(dst) == 0){
cout<<"this is same picture"<<endl;
}else{
cout<<"this is not the same picture"<<endl;
}


Edit : I replace subtraction by comparison, it is easier to read and avoid the problem of value clip.

If you are using c++, cv::subtract give you a Mat with tons of pixels value but not a single value which tell you how many non zero pixels in the matrix. You can call countNonZero in c++ to count the number of non zero pixels.

auto const diff= img1 != img2;
if(countNonZero(dst) == 0){
cout<<"this is same picture"<<endl;
}else{
cout<<"this is not the same picture"<<endl;
}


You can use stl algorithm and iterator to do the comparison too. I do not know which one is faster, you better run some test on it.

bool const equal = std::equal(img1.begin<uchar>(), img1.end<uchar>(), img2.begin<uchar>());

If you want to compare similarity of images with different size, try out img_hash module of opencv_contrib.

Edit : I replace subtraction by comparison, it is easier to read and avoid the problem of value clip.

If you are using c++, cv::subtract give you a Mat with tons of pixels value but not a single value which tell you how many non zero pixels in the matrix. You can call countNonZero in c++ to count the number of non zero pixels.

auto const diff= img1 != img2;
if(countNonZero(dst) == 0){
cout<<"this is same picture"<<endl;
}else{
cout<<"this is not the same picture"<<endl;
}


You can use stl algorithm and iterator to do the comparison too. I do not know which one is faster, you better run some test on it.

bool const equal = std::equal(img1.begin<uchar>(), img1.end<uchar>(), img2.begin<uchar>());img2.begin<uchar>());


If you want to compare similarity of images with different size, try out img_hash module of opencv_contrib.opencv_contrib.

Edit : I replace subtraction by comparison, it is easier to read and avoid the problem of value clip.