Ask Your Question
1

exception in CountNonZero (too see if 2 mat are equal)

asked 2013-11-18 08:02:48 -0600

nkint gravatar image

updated 2020-09-22 08:05:28 -0600

Hi! I have this exception

 OpenCV Error: Assertion failed (src.channels() == 1 && func != 0) in countNonZero

and I don't understan why. I use it in this function:

bool compare_images(string file1, string file2) {
    cout << "comapre_images > file1: "<<file1<<", file2: "<<file2<<endl;
    cv::Mat m1 = cv::imread(file1);
    cv::Mat m2 = cv::imread(file2);

    if(m1.empty() || m2.empty()) {
        cout << "WARNING: one of the two file is empty" << endl;
        return false;
    }

    if (m1.cols != m2.cols || m1.rows != m2.rows || m1.dims != m2.dims) {
        cout << "WARNING: the two images differs on size" << endl;
        return false;
    }

    cv::Mat diff;
    cv::compare(m1, m2, diff, cv::CMP_NE);

    int nz = cv::countNonZero(diff);

    std::cout << "comparing " << file1 << " and " << file2
              << " the diff pixels are:" << nz;
    return nz==0;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
4

answered 2013-11-18 08:26:19 -0600

berak gravatar image

since your input to compare has 3 channels(you're loading color images), the output also has 3 channels.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-11-18 08:02:48 -0600

Seen: 3,668 times

Last updated: Nov 18 '13