Die Flagge des Marasek



Aktuell Texte Der Comic Impressum Kalender Suche PHP-Klassen Container-Wizard main.s21


Religion & Philosophie

Bayer interpolation with imagemagick

Vorheriger: imagemagickNächster: Informatiker und Jura
Eingeordnet in: Computer, foto

Imagine you want to have a little bit more control over the process of developing a digital RAW image, then imagemagick in conjunction with dcraw comes in handy. Following a description how to extract the color pixels of the Bayer Matrix into a separate file:

  1. dcraw -4 -T -D -c <raw file> | convert - -evaluate multiply 16 -gamma 2.2 <matrix file>
  2. convert <matrix file> -roll +0+0 -sample 50% <matrix file>_blue.png
  3. convert <matrix file> -roll +0+1 -sample 50% <matrix file>_green1.png
  4. convert <matrix file> -roll +1+0 -sample 50% <matrix file>_green2.png
  5. convert <matrix file> -roll +1+1 -sample 50% <matrix file>_red.png

It is assumed that you have a 12 bit raw file with a RGGB pattern. These can now turned into a full color image:

convert -combine <matrix file>_red.png <matrix file>_green1.png <matrix file>_blue.png fullcolor.png

If you don't want to just discard one of the green images, you could first average both green files into a new file and use this when combining. You will most likely notice that the image has a strong greenish/cyan tint. This is because a good deal of red is lost due to the IR blocking filter within digital cameras. Using -normalize will scale up all channels and most likely result in a more realistic image, at the price that red is artifically boosted, strengthening noise.
This approach represents the cheapest conversion possible: treating a group of four Bayer Pixels (I refer to such a group as "Quaxel") as one, without any further optimization. You also have to take into account that no color scaling (save for -normalize) and no color space conversion is done.

Nevertheless, this approach has certain advantages in specific situations: when dealing with pictures taken under monochromatic light (such as Sodiom Low Pressure Lamps) or with an IR pass filter. In both situations, the Bayer pixels get very different information. Under SLP light, blue remains almost dark and has a lot of noise. The situation can even be worse with IR, as it might be that only red is properly saturated. With the Olympus E-510 and a 780nm pass filter, green gets about 50%, blue about 25% of red. It might well be worth considering to trade resolution against a better signal noise ratio and go only for red.
Even if you decide to go through the usual raw process, it might help to inspect various infrared files using this method, as it gives you a feeling on when pictures are properly exposed; the camera will most likely report wrong values, as it is used to react to green. A seemingly dark infrared picture might already clip in the red channel!


In the above sample, I apply gamma correction to the matrix file prior to storage. This saves processing time, as it does not need to be applied again on every channel. However, you might to choose to skip gamma correction for whatever reason.
Also, some cameras might actually write 16 bit raw files - the Olympus E-400 does. In this case, you have to omit -evaluate multiply 16.
A way to determine the depth delivered by dcraw is to call "identify -verbose <matrix file>" (without having multiply). If you find only values below 4096 in the histogram, you most likely have a 12 bit file (or a badly underexposed 16 bit file), if you find one value above 4096, then you have a 16 bit file. However, there might be the case that total raw files have complete white lines or whatever in them. YMMV, just try around a little bit.


This approach is somewhat tiresome if you just want to get a "somewhat RAW" image, that is a color image with no further processing involved, and it surely has the drawback that you loose a lot of resolution in normal circumstances.
An alternative is as follows:

dcraw -4 -T -r 1 1 1 1 -M -o 0 -S 4095 -k 0 <raw file>

Further information about these parameters can be found in raw, dcraw, really raw.

Dieser Text ist Teil der Serie Fun with imagemagick

Bayer interpolation with imagemagick


Bitte beachten: Kommentare sind nicht sofort sichtbar, sondern werden erst nach einer kurzen Prüfung freigegeben, sofern keine rechtliche Beanstandung vorliegt.
Rechtlich bedenkliche Inhalte werden entweder entschärft oder nicht veröffentlicht.

* Titel  
* Nickname  
* Kommentar  

nikoperugia04.02.2012 00:30:57
Thanks! the identify trick worked wonders. spent the day trying to understand why my supposedly 12bit ADC was giving me values above 10000, then realized I was loading D300 files instead of D90. Seeing exif and max value at the same time saved me.