Ask Your Question

Display raw 12 bpp grayscale image

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

pgorley gravatar image

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

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

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


  • 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 -0500 )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 -0500 )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 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

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

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


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

Seen: 647 times

Last updated: Jul 16 '19