Compare 2 cv :: Mat
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 to share
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 to share
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 to share