Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV matrix comparator don't work right?

I have a strange problem on the OpenCV matrix comparator. My code is really simple, but it seems OpenCV failed to give the correct answer. Is it a bug of OpenCV?

Mat test = (Mat_<double>(3, 3) << 1, 0, 3, 2, 5, 6, 1, 1, 7);
printf("Test matrix!\n");
for(int i=0; i<3; i++) {
    for(int j=0; j<3; j++) {
        printf("%f ", test.at<double>(i, j));
    }
    printf("\n");
}
Mat mask = (test > 2);
for(int i=0; i<mask.rows; i++) {
    for(int j=0; j<mask.cols; j++) {
        printf("%f ", mask.at<double>(i,j));
    }
    printf("\n");
}

The test matrix output is right. However, it is astonished to find that mask matrix are all zeros. Shouldn't it return a matrix of size 3x3 with element equals to one if the corresponding element in test matrix bigger than 2 and equals to zeros if test matrix smaller than 2? Why the result is a 3x3 matrix with all element in zero?

Another strange thing is that if I replace the mask to be:

Mat mask = (test < 8);

what I expected is a 3x3 matrix with all elements equals to one. However, it gives the following result:

nan 0.000000 0.000000 
nan 0.000000 0.000000 
0.000000 0.000000 0.000000

How could this happen? why there are some elements equals to nan? Is it a big bug in OpenCV?