Ask Your Question

Revision history [back]

compare/countNonZero return bigger value then expected

If I take 2 binary images (pixel values 0 or 255) and compare them with compare/countNonZero and then with two nested loops I don't get same "score".

So first I use this code:

compare(mat1, mat2, mat_tmp, CMP_EQ);

score = countNonZero(mat_tmp);

and then this:

for (int x = 0; x < mat1.cols; ++x)
{
    for (int y = 0; y < mat2.rows; ++y)
    {
        Scalar s1 = mat1.at<uchar>(y,x);
        Scalar s2 = mat2.at<uchar>(y,x);
        score += (s1.val[0]/255)*(s2.val[0]/255);
    }
}

the score values are significantly different (e.g. compare/countNonZero score=206 814 and for nested loops is 1022).

Shouldn't it be same? Why is it so? Have I misunderstood something?