IplImage to Mat returns no data

asked 2013-12-01 06:06:18 -0500

Hello,

I'm new to OpenCV so sorry if this is a basic question.

I'm converting from the IplImage structure into a Mat matrix however there seem to be a problem because when I try to access an element within the matrix, it return no data. Naturally I have tried debugging it and checking (using visual studios debug tools) to see if the matrix structure contains any data. From what I can see it does not.

Heres my code:

Mat imageMatrix(image);

cout << "Data: " << imageMatrix.at<unsigned char>(0, 0) << endl;

cvWaitKey();

The fact i'm having to use unsigned char is a little worrying as well as the image is binary. It should just be int, should it not?

Can anyone help me at all with this>

Thanks.

edit retag flag offensive close merge delete

Comments

1
  • you could check imageMatrix.empty() (but if it is, you must not try to access its pixels)
  • if it's binary, unsigned char should be ok. (check imageMatrix.type()==0)
  • if possible, try to avoid the whole old c-api (IplImages, cv*functions)
berak gravatar imageberak ( 2013-12-01 06:17:36 -0500 )edit
1

I guess it's just the cout which doesn't work w. uchar as expected, cast it to an int before that, i.e. (int) imageMatrix.at<uchar)(0.0) and you should see a value.

Guanta gravatar imageGuanta ( 2013-12-01 06:27:09 -0500 )edit

Brilliant thanks!! casting to int worked! Thanks for everyone's help. Just out on interest, why would it be using unsigned char???

welton122 gravatar imagewelton122 ( 2013-12-01 07:55:17 -0500 )edit
1

Since a typical RGB image is coded with 1 byte=8 bit for each channel = 24bit / 3byte. There exist images with a wider color range using more bits, see http://en.wikipedia.org/wiki/Color_depth#Deep_color_.2830.2F36.2F48-bit.29 . Also note that OpenCV processes color-images in the BGR order (not RGB).

Guanta gravatar imageGuanta ( 2013-12-01 08:11:21 -0500 )edit