Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial (non-profit) use, ONLY.

    For use of my scripts in commercial (for-profit) environments or non-free applications, please contact me (Fred Weinhaus) for licensing arrangements. My email address is fmw at alink dot net.

    If you: 1) redistribute, 2) incorporate any of these scripts into other free applications or 3) reprogram them in another scripting language, then you must contact me for permission, especially if the result might be used in a commercial or for-profit environment.

    Usage, whether stated or not in the script, is restricted to the above licensing arrangements. It is also subject, in a subordinate manner, to the ImageMagick license, which can be found at: http://www.imagemagick.org/script/license.php

    Please read the Pointers For Use on my home page to properly install and customize my scripts.

RETINEX


Enhances detail and color in an image using the multiscale retinex algorithm

Download Script

last modified: December 15, 2018



USAGE: retinex [-m colormodel] [-f fact] [-c contrast] [-b bright] [-s sat] [-r res1,res2,res3] infile outfile
USAGE: retinex [-h or -help]

-m .... colormodel ........ processing colorspace; HSL or RGB; default=HSL
-f .... fact .............. color boost blending factor; 0 - 100; default=0
........................... 0 is no color boost; 100 is full color boost
-c .... contrast .......... contrast gamma; contrast>0; default=1 (no change)
-b .... bright ............ brightness gain; bright>=0; default=100 (no change)
-s .... sat ............... saturation gain; sat>=0; default=100 (no change)
-r .... res1,res2,res3 .... resolution levels; default=5,20,240
-h or -help ............... get help

PURPOSE: To enhance detail and color in an image using the multiscale retinex algorithm.

DESCRIPTION: RETINEX converts the intensity values in the image to reflectance values at three resolution scales. This is achieved by computing the log of the ratio of the image to each of three gaussian blurred version of the image; one slightly blurred, one moderately blurred and one severely blurred. The three results are then averaged together. As this process sometimes desaturates the image an optional color boost is provided. The color boost process creates a new image which is the product of the resulting image with the log of the ratio of the original image to its grayscale version. The color boosted image is then blended with the previous result. The basic processing operation prior to the color boost can be performed in RGB colorspace in order to process each channel separately or it can be performed only on the lightness channel in HSL colorspace. The latter may be prefered as it tends to maintain the color balance from the original image. The implementation of this script adapts the retinex approach developed by NASA/Truview. See the following references:
http://dragon.larc.nasa.gov/retinex/background/pubabs/papers/ret40.pdf
http://visl.technion.ac.il/1999/99-07/www/
http://dragon.larc.nasa.gov/retinex/
http://www.truview.com/

ARGUMENTS:

-m colormodel ... COLORMODEL can be either RGB or HSL. If RGB is selected, then each of the R, G and B channels will be processed independently (possibly causing a color shift). If HSL is selected, then only the Lightness channel will be processed and then recombined with the Hue and Saturation channels and converted back to RGB. This option may be preferred as it will better maintain the relative color balance from the original image. The default is HSL.

-f fact ... FACT specifies the color boost blending percentage. Values for fact may be an integer between 0 and 100. A value of 0 indicates no color boost. A value of 100 indicates full color boost. The default=0

-c contrast ... CONTRAST specifies a gamma control for contrast. Values for contrast must be positive floats or integers. A value of 1 indicates no contrast change. Larger/smaller values indicate lower/higher contrast.

-b bright ... BRIGHT specifies a gain in brightness. Values for bright must be non-negative integers. A value of 100 indicates no brightness change. Larger/smaller values indicate brighter/darker results.

-s sat ... SAT specifies a gain in saturation. Values for sat must be non-negative integers. A value of 100 indicates no saturation change. Larger/smaller values indicate more/less saturation.

-r res1,res2,res3 ... RES1,RES2,RES3 specifies the three resolution levels in the retinex processing. They correspond to the pixel equivalent values to the sigma in the Gaussian blurring. These resolution values do not seem to be very sensitive such that variations to do not seem to make much change in the result. The default values are 5,20,240.

NOTE: This script will be slow due the use of -fx, unless hdri is enabled.

CAVEAT: No guarantee that this script will work on all platforms, nor that trapping of inconsistent parameters is complete and foolproof. Use At Your Own Risk.


EXAMPLES

Some examples are taken from and compared to NASA's retinex technique.


Color Boost Variations For Partially Shadowed Image

NASA Original Image

Arguments:
-m RGB -f 0

Arguments:
-m RGB -f 50

Arguments:
-m RGB -f 100

NASA Original Image

Arguments:
-m HSL -f 0

Arguments:
-m HSL -f 50

Arguments:
-m HSL -f 100



Comparisons Of Enhancements For Overall Dark Image

Original Image

retinex
-m RGB -f 0

retinex
-m HSL -f 0

space
-m 0.5 -b 2 -c 2 -w 5

omnistretch
-acc 3

redist
-m RGB 50,60,60

redist
-m HSL 50,60,60

-gamma 3

-normalize

-equalize



Comparisons Of Enhancements For Partially Shadowed Image

NASA Original Image

NASA retinex

retinex
-m RGB -f 70 -c 1.5 -b 90

retinex
-m HSL -f 70 -c 1.5 -b 90

redist -m RGB 50,80,80
-modulate 100,120

space
-m 0.3 -b 1.5 -c 0.7 -w 5

omnistretch -acc 3

-gamma 2

bcimage 20,-15

-equalize



Comparisons Of Enhancements For Partially Shadowed Image

NASA Original Image

NASA retinex

retinex -m RGB -f 85

retinex -m HSL -f 15

space
-m 0.7 -b 1.8 -c 1.5 -s 1.5 -w 5

redist -m RGB 60,60,60

-gamma 2

-equalize



Comparisons Of Enhancements For Partially Shadowed Image

NASA Original Image

NASA Retinex

retinex
-m RGB -f 80 -s 70

retinex
-m HSL -f 50 -c 1.2

space -m 0.7 -b 2.3
-modulate 100,150

redist -m RGB 50,80,80
-modulate 100,200

-gamma 2

-equalize



Comparisons Of Enhancements For Hazy Image

NASA Original Image

NASA Retinex

retinex -m RGB -f 20

retinex
-m HSL -f 100 -s 200

space
-m 0.5 -b 1.2 -c 8 -w 5 -mg 10
-gamma 0.7

redist -m RGB 60,60,30

-normalize

-equalize



Comparisons Of Enhancements For Hazy Image

NASA Original Image

NASA Retinex

retinex
-m RGB -f 60 -b 120 -s 70

retinex
-m HSL -f 100 -s 500 -c 2

space
-m 0.3 -b 1.5 -c 0.7 -w 5
redist 50,60,40

omnistretch -acc 3

-normalize

-equalize



Comparisons Of Enhancements For Hazy Image

NASA Original Image

NASA Retinex

retinex
-m RGB -f 50 -c 1.5

retinex
-m HSL -f 100 -s 200

space
-m 0.25 -b 1.5 -c 4 -s 1.5
-gamma 0.6

-normalize

bcimage 10,40

redist -m RGB 50,40,40



What the script does is as follows:

  • Computes the log of the ratio of the image to each of three gaussian
    blurred version of the image; one slightly blurred, one moderately
    blurred and one severely blurred
  • Averages these 3 resulting images
  • Computes a color boost image from the log of the ratio of the
    original image to the graylevel versin of the image
  • Multiplies the two images together to create a more colorful
    image.
  • Blends the averaged result with the color boosted image.
  • Normalize and optionally change the brightness and/or
    saturation

This is equivalent to the following IM commands for
the case of -m RGB:

  • convert $infile \( +clone -blur 0x$res1 \) \
    -fx "log((u/max(v,.000001))+1)" $tmp1
  • convert $infile \( +clone -blur 0x$res2 \) \
    -fx "log((u/max(v,.000001))+1)" $tmp2
  • convert $infile \( +clone -blur 0x$res3 \) \
    -fx "log((u/max(v,.000001))+1)" $tmp3
  • convert $tmp1 $tmp2 $tmp3 -average -normalize $tmp1
  • convert $infile \( +clone -colorspace Gray \) \
    -fx "log((u/max(v,.000001))+1)" $tmp2
  • convert $tmp2 $tmp1 -compose multiply -composite -normalize $tmp3
  • composite -blend $fact% $tmp3 $tmp1 $tmp1
  • convert $tmp1 -gamma $contrast -modulate ${bright},${sat} $outfile