Accessing Raw Bayer data


I just downloaded and compiled the libraw APIs. I was trying to use the APIs to access the Raw bayer stream from a canon CR2 file.

After instantiating a LibRaw object and calling its open_file(), unpack() and raw2image() functions, im able to print out pixel values using the imgdata.image[pixelindex][channelindex] data array. (I see a nice RGBG pattern coming out here)

However, if i just instantiate the libraw object, call open_file(), unpack() and then access pixel values from the .imgdata.rawdata.raw_image array, i see only 16384 (the max possible 16bit value) getting dumped out.

After raw2image(), does the imgdata.image array contain raw bayer data or does it undergo any processing? If it is infact the raw bayer, i was hoping to see the same values in the raw_image array (but as a single channel of pixel values) What am i missing here?


Code snippet :
LibRaw RawProcessor;
for(i = 0;i < 100; i++)
printf("%d ",(RawProcessor.imgdata.rawdata.raw_image[i]));
output : 16384 16384 16384 ....


1) The max possible 16-bit

1) The max possible 16-bit value is 65535
2) Many cameras, including Canons, contains 'masked pixels' around visible area. So, for first 100 values you query these service pixels.
Try this code:

#define R RawProcessor
int first_visible_pixel = R.imgdata.sizes.raw_width*R.imgdata_sizes.top_margin + R.imgdata.sizes.left_margin;
for(i=0; i< 100; i++)
  printf("%d ", R.imgdata.rawdata.raw_image(i+first_visible_pixel);

3) After raw2image the image[][] array contains values (only visible area!) for one component out of 4, three other pixel components are zero.

-- Alex Tutubalin @LibRaw LLC

Thanks Alex. I see the

Thanks Alex. I see the correct pixel values now!

I was not aware of these masked pixels ... are they provided for black-level calibration?


Yes, in Canon data these

Yes, in Canon data these pixels used for black level calibration.
On other cameras (e.g. Nikons) these pixels may be used for banding noise elimination

-- Alex Tutubalin @LibRaw LLC

What do you mean for one

What do you mean for one component out of 4? I actually noticed this, but I don't really understand what's going on.

Bayer image contains only one

Bayer image contains only one color component per pixel.
image[][4] array has 4 components per pixel (R,G,B,G2 or CMYG or RGBE).

raw2image() copies bayer data into image array

-- Alex Tutubalin @LibRaw LLC

Can I ask follow up question relate to this matter?

So, to my knowledge, bayer image would be greyscale (but with 10/12/or 14 bit depending on your camera raw image). Is it correct?
If yes, them if I map those Bayer values into (int)(0 -255), would it be similar to the greyscale.jpg (8 bit) file?

Short answer: No

Short answer: No
RAW values unpacked from RAW file are:
- in linear gamma
- white balance not applied
- black level not subtracted (for most cases; some cameras do black subtraction).

-- Alex Tutubalin @LibRaw LLC

Hi Alex,

Thanks for the quick reply. Then I think I will need to do heavy processing for useful data.
Can I ask your opinion on this matter:
-I am currently working on "image matching with stereo camera". I am looking for away to match the same pixel between left image and right image.
-With the 8 bit data in jpg, sometimes the matching will not be so correct, for example: (pixel of 112.02 will become 112) and (pixel of 111.8 will also become 112). Therefore, they might be considered as a match but it is not true.
Do you think by taking the raw data for processing I can get better matching result? or Raw data are just too noisy for the matching task?

I think that matching pixel

I think that matching pixel by exact values will not work well due to value differences (because of noise, for example).

-- Alex Tutubalin @LibRaw LLC