how to check whether two matrices are identical in OpenCV

前端 未结 9 1788
南方客
南方客 2020-11-28 10:44

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 (ha

9条回答
  •  半阙折子戏
    2020-11-28 10:54

    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(), a.end(), b.begin());
    

提交回复
热议问题