Ask Your Question
0

print comparison matrix elements

asked 2015-12-19 04:17:03 -0600

ggeo gravatar image

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 flag offensive close merge delete

Comments

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

berak gravatar imageberak ( 2015-12-19 04:33:15 -0600 )edit

@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)

ggeo gravatar imageggeo ( 2015-12-19 04:37:30 -0600 )edit
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

berak gravatar imageberak ( 2015-12-19 04:44:33 -0600 )edit
1

@berak sorry I'm answering while you are typing :)

pklab gravatar imagepklab ( 2015-12-19 04:51:51 -0600 )edit
1

@pklab, sorry, i was typing, while you were answering ;)

berak gravatar imageberak ( 2015-12-19 04:59:43 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-12-19 04:48:26 -0600

pklab gravatar image

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;
}
edit flag offensive delete link more

Comments

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

ggeo gravatar imageggeo ( 2015-12-19 04:56:38 -0600 )edit

@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

pklab gravatar imagepklab ( 2015-12-19 05:03:53 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-12-19 04:17:03 -0600

Seen: 120 times

Last updated: Dec 19 '15