Ask Your Question
0

Why is cv::Mat::data always pointing to a uchar?

asked 2016-01-13 03:00:56 -0600

nyd gravatar image

I try to read a NEF file using LibRaw and then put it in a cv::Mat. The NEF file stores data as 12bit, this means I need 16 bit, so I ought to use CV_16UC4 like this:

Mat img1(height, width, CV_16UC4);

Libraw stores data as ushort*[4], so I thought that this should work:

for (i = 0; i < iwidth*height; i++) {       
    img1.data[4*i+1] = Processor.imgdata.image[i][0];
    img1.data[4*i+2] = Processor.imgdata.image[i][1];
    img1.data[4*i+3] = Processor.imgdata.image[i][2];
    img1.data[4*i+4] = Processor.imgdata.image[i][3];
}

I also get a build error that data may be lost since a ushort to uchar conversion is going to take place, which makes sense, but still, how do I put data bigger than uchar in the data?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2016-01-13 04:16:34 -0600

kbarni gravatar image

updated 2016-01-13 04:18:03 -0600

You shouldn't use directly img1.data!

Either use the img1.ptr() or img1.ptr(line) to get the pointer to the data. Then cast it to the desired data format:

ushort *pImg=(ushort*)img1.ptr();

Note that the image is not always continuous in the memory; so (pImg+ywidth+x) is not sure that will give the pixel (x,y). Check this with img1.isContinous()

You can also use the img.at<type>(y,x) operator.

img.at<vec3s>(y,x)[0]=blue.

etc...

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2016-01-13 03:00:56 -0600

Seen: 1,239 times

Last updated: Jan 13 '16