how to check whether two matrices are identical in OpenCV

前端 未结 9 1754
南方客
南方客 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 11:09

    The problem of using cv::countNonZero is that this function only works for one-channel images. If you want to work with multichannel images, you have to take care of each channel individually. The first step is to split an image into channels. As Antonio explained, you can use cv::split function for this purpose. After splitting, you can use cv::countNonZero for each channel and sum the results along all channels by using iteration. cv::Mat::channels gives you the number of channels.

    This is the code that I use to check whether two matrices are identical.

    bool isEqual(cv::Mat firstImage, cv::Mat secondImage){
        cv::Mat dst;
        std::vectorchannels;
        int count = 0;
        cv::bitwise_xor(firstImage, secondImage, dst);
        cv::split(dst, channels);
        for (int ch = 0; ch

提交回复
热议问题