Processing of digital photo raw images

This is an example how Bayer color pattern images can be processed. Because of the simple command line syntax of SImg, it is easy to create scripts using your favorite language. This is especially useful for processing large amounts of images automatically. For processing single images there are also some handier GUI tools like ufraw.

Below is an example script that can do the most common processing steps, in particular

  1. Dark field correction (optional)
  2. Flat field correction (optional)
  3. Cropping the image (optional)
  4. Color interpolation, bad pixel correction
  5. Deconvolution (for sharpening, optional)
  6. Contrast stretching and color curve adjustment

In steps 1-3 the image is processed as uninterpolated Bayer color pattern image. The color interpolation to achieve a normal RGB image is done in step 4. In this step also a bad pixel correction can be made. In step 5 the image can be deconvolved to increase the sharpness. Usually this makes only sense if the image is free of noise. Contrast and the color curves are adjusted in step 6.

The parameters can be specified in a configuration script named This configuration file must be located in the current directory. An example configuration script with a lot of detailed information about the processing and parameter adjustment can be found below.

The usage of the script is simple: <input file1> [<input file2> [<input file 2> [...]]]

For each input file a PPM output file is produced.

Here is the script

hc="-l 0.1% -u 99.9%"
if [ -f ]; then
debugout () {
    $debug && simg convert "$tmpfile" -16 "$base.$1.sif"
for i; do
  if [ "$base.ppm" = "$i" ]; then
  echo "$i --> $out"
  if [ "$SImg_cameraWB" = "" ] && [ "$zero" != "" -o "$one" != "" ]; then
  export SImg_cameraWB
  export SImg_RedCorrection
  export SImg_BlueCorrection
  simg convert "$i" "$tmpfile"
# step 1: darkfield correction
  if [ "$zero" != "" -a -f "$zero" ]; then
    simg add -f1 -1 -f2 1 "$zero" "$tmpfile" "$tmpfile"
    debugout 1
# step 2: flatfield correction
  if [ "$one" != "" -a -f "$one" ]; then
    simg mul -e1 -1 -e2 1 "$one" "$tmpfile" "$tmpfile"
    debugout 2
#  step 3: crop the image
  if [ "$cut" != "" ]; then
    simg cut $cut "$tmpfile" "$tmpfile"
    debugout 3
# step 4: color interpolation, bad pixel correction
  simg bayer $bayeralgo "$tmpfile" "$tmpfile"
  debugout 4
# step 5: deconvolution using the wiener method
  if [ "$dc" != "" ]; then
    simg wiener $dc "$tmpfile" "$tmpfile"
    debugout 5
# step 6: cropping of the dynamic range, adjustment of the color curves, ...
  simg histcut $hc "$tmpfile" "$tmpfile"
  simg convert -8 "$tmpfile" "$out"
  rm -f $tmpfile

Here is an example configuration script, which was used to process the example image below.

# Enable output for each step.
# Default: debug=false
# Syntax: debug=false|true
# A darkfield can be specified here. The darkfield is subtracted from
# the input image in the first step to eliminate the dark current 
# visible in long time exposures.
# Default: zero="" (no darkfield correction)
# A flatfield can be specified here. The image is divide by that
# flatfield in order to remove vignetting and dust on the sensor. May
# also be used for whit balance.
# Default: one="" (no flatfield correction)
# Cropping parameters (see "simg cut -h") can be specified here.
# Default: cut="" (no cropping)
cut="-xc 1450 -x 1400 -y 800"
# Color interpolation algorithm and parameters can be specified here.
# See simg Bayer -h for available algorithm. Try bayeralgo="-sc -scs 5"
# and bayeralgo="-sc -scs 3" first. This algorithm produces sharp 
# images, but may generate artifacts under some conditions. If so, use
# bayeralgo="-li". Bad pixel corrections can be enabled with the "-bp" 
# option, try "-bp 10"
# Default: bayeralgo="-li"
bayeralgo="-sc -scs 3"
# Wiener deconvolution parameters. If you have no idea ho to chose the
# parameters, try this:
# 1. Start with dc="-snr 10 -s 0.8" or dc="-snr 5 -s 0.8" (depending on 
#    the noise)
# 2. Increase -s until artifacts are visible.
# 3. Decrease -s by 10%
# 4. Increase -snr until you found a compromise in sharpness and noise
# Default: dc="" (no deconvolution)
dc="-snr 5 -s 1.1"
# Cropping of the dynamic range and adjustment, adjustment of the color 
# curves, ... . See "simg histcut -h". Suitable parameter can be found
# easily using the visualisation utility "vsimg visual"
# default: hc="-l 0.1% -u 99.9%"
hc="-l 0.1% -u 99.9% -mq 15% -mv 0.55"
# Use camera white balance. If your camera determines the white balance
# automatically darkfield and flatfield corrections may cause strange
# results.
# Default: SImg_cameraWB="" (camera white balance is used, if no 
#   darkfield and no flatfield is given)
# Syntax: SImg_cameraWB=0|1
# Additional white balance adjustment can be made here. Owners of pocket
# calculators find out the right values by searching a white reference 
# with the visualisation utility "vsimg visual" :-)

With this configuration script the Minolta raw image pict0830.mrw was processed by calling pict0830.mrw

This generates 4 output images, the final result pict0830.ppm and the results of the steps 3–5, namely pict0830.3.ppm, pict0830.4.ppm and pict0830.5.ppm. (The output of the step results can be disabled by setting debug=false in the configuration file).

Here comes pict0830.4.ppm, the result of the color interpolation step (click on the image to see a full resolution version)
Helicopter (step 4)

And this is the final result, after a weak deconvolution and adjustment of the brightness and color curves (click on the image to see a full resolution version):
Helicopter (final)

examples/bayer-processing.txt · Last modified: 2010/03/08 09:51 (external edit)
Recent changes RSS feed Creative Commons License Powered by PHP Debian Driven by DokuWiki