convert Bayer greyscale into RGB data (debayer)

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
jbeale
Posts: 8
Joined: 2007-12-07T11:15:22-07:00

convert Bayer greyscale into RGB data (debayer)

Post by jbeale »

Can I do this with ImageMagick? My problem: I have a raw bayer bitmap, which happens to be 16 bits per pixel although the data has only 10-bit resolution. The bayer pattern is fairly standard RGBG..., GRGB or similar (I think- I'll figure that out). There is no header at all, just the pixel data as a single plane, gray bitmap. I want to do a debayer to get RGB data. I don't need any color manipulation, I can do that myself later. Can anyone advise if there is some simple way to do this using LIBRAW, DCRAW, ImageMagick or similar tool, without writing a lot of code myself?

In case of interest, the file is from the just-released Raspberry Pi CSI camera, which has an option to record raw data, but no software yet exists to interpret it. I've made a start and I now have the 10 bit data in a standard 16-bpp Grayscale PNG format file, but now I need to do a debayer.
jbeale
Posts: 8
Joined: 2007-12-07T11:15:22-07:00

Re: convert Bayer greyscale into RGB data (debayer)

Post by jbeale »

As an addendum, for anyone interested, more info about this Raspberry Pi camera raw-decoder project, and links to sample data is at: http://www.raspberrypi.org/phpBB3/viewt ... 43&t=44918

Crop of raw bayer image: http://www.bealecorner.org/best/test/raw-crop1.png
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: convert Bayer greyscale into RGB data (debayer)

Post by GreenKoopa »

IM delegates demosaicing bayer to dcraw. See:
http://www.imagemagick.org/Usage/formats/#crw
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert Bayer greyscale into RGB data (debayer)

Post by snibgo »

Yes, I suggest you talk to Dave Coffin, with the specs of the format and sample images. http://www.cybercom.net/~dcoffin/

EDIT: I might add: if you have an image with (2x,2y) RGGB values, ImageMagic can easily (with crop and sample) create four single channel images, each (x,y) pixels. Average the two green channels, choose appropriate multipliers, and combine. Convert to sRGB if required.

This is, of course, fairly primitive. dcraw uses clever algorithms to guess missing values, giving an image with (2x,2y) pixels.

EDIT 2: ImageMagic (Windows 7) script:

Code: Select all

set BASE=raw-crop1


FOR /F "tokens=1,2,3,4" %%i IN ('%IM%identify -format "%%w %%h %%w-1 %%h-1" %BASE%.png') DO (
  set WIDTH=%%i
  set HEIGHT=%%j
  set /A WIDTHm1=%%k
  set /A HEIGHTm1=%%l
)

rem We need an even number of pixels in each direction.
rem If we have an odd number, drop the last column/row.
set /A calcW=%WIDTH%/2*2
set /A calcH=%HEIGHT%/2*2
if not %calcW%==%WIDTH% (
  set /A WIDTH=%WIDTHm1%
  set /A WIDTHm1=!WIDTH!-1
)

if not %calcH%==%HEIGHT% (
  set /A HEIGHT=%HEIGHTm1%
  set /A HEIGHTm1=!HEIGHT!-1
)


"%IMG%convert" %BASE%.png -crop %WIDTH%x%HEIGHT%+0+0 -sample 50%% %BASE%_B.tiff
"%IMG%convert" %BASE%.png -crop %WIDTHm1%x%HEIGHT%+0+0 +repage -sample 50%% %BASE%_G0.tiff
"%IMG%convert" %BASE%.png -crop %WIDTH%x%HEIGHTm1%+0+0 +repage -sample 50%% %BASE%_G1.tiff
"%IMG%convert" %BASE%.png -crop %WIDTHm1%x%HEIGHTm1%+0+0 +repage -sample 50%% %BASE%_R.tiff

"%IMG%convert" %BASE%_G0.tiff %BASE%_G1.tiff -average %BASE%_G.tiff

"%IMG%identify" %BASE%_R.tiff
"%IMG%identify" %BASE%_G.tiff
"%IMG%identify" %BASE%_B.tiff


rem The following numbers are guesses. They seem reasonable.
set RedBalance=1.8
set GreenBalance=2.0
set BlueBalance=2.0

"%IMG%convert" ^
  ( %BASE%_R.tiff -evaluate multiply %RedBalance% ) ^
  ( %BASE%_G.tiff -evaluate multiply %GreenBalance% ) ^
  ( %BASE%_B.tiff -evaluate multiply %BlueBalance% ) ^
  -channel RGB -combine -gamma 2.2 %BASE%_comb.tiff
All the converts could be combined into one.

The pattern may different to RGGB. You should test it on an image containing saturated colours, like a car park.
snibgo's IM pages: im.snibgo.com
Post Reply