OpenCV matrix comparator don't work right?

asked 2014-09-09 23:28:45 -0500

vc++ gravatar image

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 ",<double>(i, j));
Mat mask = (test > 2);
for(int i=0; i<mask.rows; i++) {
    for(int j=0; j<mask.cols; j++) {
        printf("%f ",<double>(i,j));

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?

edit retag flag offensive close merge delete



mask is uchar, not double ;)

berak gravatar imageberak ( 2014-09-10 00:55:08 -0500 )edit

You can print a cv::Mat just by cout << mat << endl if you are not sure which datatype to use (of if you want to save some typing)

FooBar gravatar imageFooBar ( 2014-09-10 00:59:27 -0500 )edit