Compare 2 cv :: Mat

I have 2 cv :: Mat array (with the same size) and when I want to compare them (if they are identical) I used cv :: compare

cv::compare(mat1,mat2,dst,cv::CMP_EQ);

      

Is there any function that returns true / false?

+3


source to share


3 answers


If you need to compare 2 cv :: Mat sizes, you can check

if(mat1.size() == mat2.size())
    //do stuff
else
    //do other stuff

      



If you need to check if 2 cv :: Mat are equal, you can execute the operator AND XOR and check if the result of cv :: Mat is full of zeros:

cv::bitwise_xor(mat1, mat2, dst);        
if(cv::countNonZero(dst) > 0) //check non-0 pixels
   //do stuff in case cv::Mat are not the same
else
  //do stuff in case they are equal

      

+7


source


If you need to check if 2 cv :: Mat are equal, you can execute the AND operator and check if the result of cv :: Mat is full of zeros:

The AND operator is not suitable for this task. If a matrix is โ€‹โ€‹0, it will always return true, whether the other matrix is โ€‹โ€‹0 or not.

In this case, XOR should be used.



Here's a modified version of blackibiza's code:

cv::bitwise_xor(mat1, mat2, dst);        
if(cv::countNonZero(dst) > 0) //check non-0 pixels
   //do stuff in case cv::Mat are not the same
else
  //do stuff in case they are equal

      

+3


source


This function returns true / false based on similarity (untested)

bool MyCompare(Mat img1, Mat img2)
{
    int threshold = (double)(img1.rows * img1.cols) * 0.7; 

    cv::compare(img1 , img2  , result , cv::CMP_EQ );
    int similarPixels  = countNonZero(result);

    if ( similarPixels  > threshold ) {
        return true;
    }
    return false;
}

      

0


source







All Articles