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:
But what I'm getting is:
Any help would be appreciated!
(i don't think, you can use your 12bit packed data pointer directly.)
When I'm reading the 12 bit data file, I'm unpacking it into 16-bit data so it doesn't segfault.
For now, I'm dropping the 4 least significant bits and using CV_8UC1, which works.