# print comparison matrix elements

I want to know why trying to access (and print) test matrix results in showing garbage:

Mat A = (Mat_<double>(3,3) << 0, -1.4, 0.2, -1, 5.4, -1, 0.1, -1.4, 0);
cout << "A = " << endl << " " << A << endl << endl;

Mat B = (Mat_<double>(3,3) << 0, -1.4, 0.4, -2, 5.4, -11, 0, -1.42, 0);
cout << "B = " << endl << " " << B << endl << endl;

Mat test = Mat(3,3,CV_64F);

test = ( A == B );

for ( int i = 0; i < 3; i++ )
{
for (int j = 0; j < 2; j++ )
{
cout << test.at<int>(i,j) << endl;
}
}


If I use just cout << test it is ok ,but I want to know why the previous doesn't work.

Thanks!

edit retag close merge delete

the comparison returns a CV_8U Mat, so use: (int)test.at<uchar>(i,j)

@berak: Hmm, so even though I am declaring test matrix to be double , the comparison results in uchar.But the type of test matrix remains double?Thanks! ( make that an answer please)

1

no, the test Mat will get re-allocated with a new type.

please avoid pre-allocating result Mat's in general, as you will only fool yourself

1
1

Sort by » oldest newest most voted because with test = ( A == B ); is a logical operation with uchar as result. This means that you can do simply

Mat test;
test = (A == B);
for (int r = 0; r < test.rows; r++)
{
for (int c = 0; c < test.cols; c++)
{
cout << 1 * test.at<uchar>(r, c) << "\t";
}
cout << endl;
}

more

@pklab:You meant int instead of 1 above?So,even if I declare test as double type ,it will be uchar type.

@ggeo yes cout << (int) test.at<uchar>(r, c) << "\t"; is better. I used multiplication by 1 to have casting on the fly. cout receives chars and prints chars not numbers. cout << char(0) prints something blank. cout << int(0) prints the number 0

Official site

GitHub

Wiki

Documentation