Ask Your Question
0

Display raw 12 bpp grayscale image

asked 2019-07-11 14:57:13 -0600

pgorley gravatar image

updated 2019-07-12 08:32:36 -0600

I'm reading images from a 12 bits per pixel grayscale frame grabber. It returns these images in an array of uint16_t, where the 4 msb are 0. I'm packing this array into an array of uint8_t (2 uint16_t give 3 uint8_t) and saving it to disk raw. So the resulting file is a bunch of 12 bit chunks one after the other in big endian.

Later on, I'd like to read such a file and get its histogram using calcHist. I'm reading the file into an std::vector<uint8_t>, unpacking it into a std::vector<uint16_t> (3 uint8_t give 2 uint16_t, with the 4 msb zeroed). So far, everything works.

I'm trying to display the image with OpenCV using the following:

cv::Mat img(1024, 1280, CV_16UC1, &data[0]); // data is my std::vector<uint16_t>
cv::imshow("Image", img);
cv::waitKey(0);

I'm expecting an image that looks like: this

But what I'm getting is: this

Any help would be appreciated!

edit retag flag offensive close merge delete

Comments

1
  • please upload your images here, not on imgur (can't be seen from tr,cn, etc.)
  • you're trying to use a 1024x1024x16bit Mat, but you only have 1024x1024x12bit data, that'll segfault later

(i don't think, you can use your 12bit packed data pointer directly.)

berak gravatar imageberak ( 2019-07-11 16:01:57 -0600 )edit

When I'm reading the 12 bit data file, I'm unpacking it into 16-bit data so it doesn't segfault.

pgorley gravatar imagepgorley ( 2019-07-12 08:33:28 -0600 )edit

For now, I'm dropping the 4 least significant bits and using CV_8UC1, which works.

pgorley gravatar imagepgorley ( 2019-07-12 08:34:26 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-07-16 11:39:02 -0600

pgorley gravatar image

What I ended up doing is dropping the 4 least significant bits of each pixel (I don't need that much precision) when reading the file into a vector. That way I can load the image using CV_8UC1.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-07-11 14:57:13 -0600

Seen: 1,569 times

Last updated: Jul 16 '19