OpenCV: Working with 12bit gray scale raw data

14,046

Solution 1

Well I wanted to do 16 bit processing on this image. The secret is then reveled in another question .

For 16 bit I will have to switch to PNG not bmp!

This document says I can use 16 bit PNG in OpenCV

Solution 2

From what I know, OpenCV is only handling 8, 16 and 32 bits images correctly.

depending on what you want to do, you should

I'd advise you to look closer at how your pixels are exactly placed in your image before doing anything. This will greatly help you to know what is best. A good way to do this is using bless or another hex editor

EDIT:

Taken from here, Your data are (I think) encoded over 16 bits with padding

Simply convert from 16 bits to 8 bits using Opencv should do the trick, as it is what is done while converting to JPEG. Have you at least tried ?

Solution 3

Left shift the bits and store them as 16bit.

For a monochrome camera:

cv::Mat imgOpenCV = cv::Mat(cv::Size(img->width, img->height), CV_16UC1, (char*)img->imageData, cv::Mat::AUTO_STEP); //Convert the raw image
imgOpenCV.convertTo(imgOpenCV, CV_16UC1, 1.0*(2^4), 0.0); //Shift left 4 bits

For a color camera:

// Convert the image using the manufacturer's SDK (Imperx in this case)
IpxImage* imgConverted = nullptr;
IpxError err = IPX_CAM_ERR_OK;
IpxSize imgSize;
imgSize.height = img->height;
imgSize.width = img->width;
uint32_t pixelType = II_PIX_BGR12;
err = IpxCreateImage(&imgConverted, imgSize, pixelType);
err = IpxBayer_ConvertImage(hBayer, img, imgConverted);

// Create OpenCV converted image
cv::Mat imgOpenCV = cv::Mat(cv::Size(imgConverted->width, imgConverted->height), CV_16UC3, (char*)imgConverted->imageData, cv::Mat::AUTO_STEP); //Convert the image
imgOpenCV.convertTo(imgOpenCV, CV_16UC3, 1.0*(2^4), 0.0); //Shift left 4 bits

You could also use the manufacturer's SDK to convert to 16 bit directly, but I suspect this scales the bit depth instead of shifting it in some cases. I'd rather preserve the raw bit depth information, so doing the shift myself prevents me from having to look under the hood when I switch manufacturers (GigE cameras force you to use the manufacturer SDK unfortunately).

Share:
14,046
gpuguy
Author by

gpuguy

Updated on June 07, 2022

Comments

  • gpuguy
    gpuguy almost 2 years

    In past I have used OpenCv for processing 32 bit .bmp files, and have got successful results. How do I make use of Opencv functions to process 12bit raw data? My processing will be mostly operations like finding the some of 1000 pixels and so on.

    EDIT The file that I am getting is .bin file. And the Pixels in Image are placed like this(the file has all the content like this, coz I am taking a picture of white paper):

    (FF)(0F)(FF)(0F)(FF)(0F)(FF)(0F)(FF)(0F)(FF)(0F)(FF)(0F)
    

    Clearly I can do 16 bit processing on this.

  • gpuguy
    gpuguy almost 12 years
    Plz see the edit above. Could you plz revised your solution based on this info?
  • Anoop K. Prabhu
    Anoop K. Prabhu almost 12 years
    Are you sure that the data is not 16 or 32 bit. It seems to follow the pattern FF0F. If it is 12 bit, the first pixel will be FF0, 2nd pixel: FFF, 3rd pixel 0FF etc based on your info!
  • jlengrand
    jlengrand almost 12 years
    No I can't, because your line is even more confusing than your question already was. Are you at least sure of what you are doing, and how are your data ecncoded?
  • gpuguy
    gpuguy almost 12 years
    NO ,The data is I am sure 12bit. The data is actually aligned: 0FFF, 0FFF, 0FFF....
  • gpuguy
    gpuguy almost 12 years
    No I think, "0FFF" is 12 bits,, (ignoring the 0): 1111 1111 1111. To display it obviously I need minimum 16 bit.
  • Anoop K. Prabhu
    Anoop K. Prabhu almost 12 years
    Aah..my mistake. I was trying to mean it as 16bit. The 12-bit raw data is already represented as 16-bit. So there wont be a problem in accessing them right? You can write the code in the same way similar to the 32-bit image code that you had implemented. As the 4 MSB's are already 0, sum of pixels can be easily calculated.
  • gpuguy
    gpuguy almost 12 years
    Can you please elaborate your algo with some sample data?
  • Anoop K. Prabhu
    Anoop K. Prabhu almost 12 years
    My first algorithm will not fit to the current situation. For your doubt on number of bits for RGBchannels: I have no clue of it. It varies depending on from where the output is obtained. Seeing 0x0FFF, i think the raw image is probably the output of some high quality camera. If so, then the you will have to look through Bayer Filter. My above algorithm was meant to calculate the sum of 16bit grayscale data (not taking channels into account).