C++ – how to check whether two matrices are identical in OpenCV

c++opencv

I have two instances of cv::Mat : m1 and m2. They are of the same numeric type and sizes.
Is there any function in OpenCV that returns whether the matrices are identical (have all the same values)?

Best Solution

As mentioned by Acme, you can use cv::compare although it is not as clean as you might hope.
In the following example, cv::compare is called by using the != operator:

// Get a matrix with non-zero values at points where the 
// two matrices have different values
cv::Mat diff = a != b;
// Equal if no elements disagree
bool eq = cv::countNonZero(diff) == 0;

Presumably it would be quicker to just iterate through comparing the elements though? If you know the type you could use the STL equal function:

bool eq = std::equal(a.begin<uchar>(), a.end<uchar>(), b.begin<uchar>());