Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Reading pixel values from a binary file

I have a series of 128x128 pixel sized frames stored in a binary file. Each pixel has 32 bits of information, I am interested in the last 12 bits of each pixel, these 12 bits should provide me with a grayscale image.

I know openCV doesn't support 12-bit, so I tried to do the masking using 0xfff, not sure if that is right. In my attempt I am looking at the 99th frame of the video, there is a 1024 byte header in each file that I am skipping, and each frame has 66960 bytes, hence the 66960*99 to skip to the 99th frame. I also skip a reserved 4 bytes which is in each.

The resulting image I get looks almost right, except it seems like there is a gray mask over the whole image. I have both what I am getting and what it should look like at the bottom attached.

My attempt is below, I know I am getting the pixel information right because I am checking pixel values by using another software, I believe the problem is in the way I am using the data types and Mat image type (unsigned int, short etc and CV_16U, CV_16S, CV_32). Please help with how to pad the pixel data and/or what type Mat to use. Thanks.

    file = fopen("binary_file", "rb");
    fseek(file, 0, SEEK_END);

    //frame loop
    unsigned int buffer[65536];
    fseek(file,4+1024+66960*99, SEEK_SET);
    fread(buffer, 4, 65536, ff);

    Mat img(128,128,CV_16S);
    int p = 0;
    for (int y = 0; y < img.rows; y++) {
        for (int x = 0; x < img.cols; x++) {
  <short>(y, x) = ((buffer[p] & ((1 << 12) - 1)) & 0xfff);

What I get, looks masked

What it should look like