Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Display raw 12 bpp grayscale image

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!

Display raw 12 bpp grayscale image

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 like: this

But what I'm getting is this.is: this

Any help would be appreciated!