Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial 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

ADAPTIVEGAMMA


Enhances the contrast/brightness in an image using a locally adaptive gamma method.

Download Script

last modified: October 10, 2017



USAGE: adaptivegamma [-t type] [-c colormode] [-a autolevel] [-s sigma] [-m mix ] [-b base] [-g gain] infile outfile
USAGE: adaptivegamma [-h or -help]

-t ... type ........ type of adaptive gamma enhancement; choices are lcc, pnae, hybrid;
.................... default=hybrid
-c ... colormode ... colorspace for processing; choices are gray, srgb, lab, hcl
.................... ycbcr and ohta; default=gray
-a ... autolevel ... autolevel as preprocessing; options are: off, together and
.................... separately; default=together
-s ... sigma ....... sigma value for blurring the intensity-like channel; integer>=0;
.................... default=20
-m ... mix ......... mixing percent for the hybrid blend of lcc and pnae;
.................... 0<=integer<= 100; default=50; 0 is pure lcc, 100 is pure pnae
-b ... base ........ base value for lcc method; float>0; default=2
-g ... gain ........ gain value for pnae method; float>=0; default is computed
.................... automatically from the mean and standard deviation of the image;
.................... nominal value is about 0.5

PURPOSE: To enhance the contrast/brightness in an image using an adaptive gamma method.

DESCRIPTION: ADAPTIVEGAMMA is a locally/spatially adaptive gamma technique to enhance an image's brightness and contrast. There are 3 types of adaptive gamma enhancement that can be used. The first is the local contrast correction (lcc) method. The second is the parallel nonlinear adaptive enhancement (pnae) method. The third is a hybrid blend of the other two.

These adaptive gamma methods are power law adjustments of the image as defined by OUT=pow(IN,exponent); where exponent = 1/gamma. However, here the exponent is a value that changes spatially according to the local mean M(x,y) of the intensity-like channel of the image.

In the lcc method, exponent=[base]*M(x,y)-1, where M is an image of the local mean of the intensity-like channel achieve by blurring the image and base is nominally 2.

In the pnae method, exponent=const*[M(x,y)-epsilon]/[1-M(x,y)+epsilon] + gain. The const and epsilon values are constants where const=0.1 and epsilon=0.01. The gain is essentially a global (inverse) gamma value and is nominally about 0.5. But here the default is computed automatically as (log(0.5)/log(M))*($maxstd/min($maxstd,S)), where M is the global mean of the image and S is the global standard deviation of the image and maxstd is a maximum value for the standard deviation above which the standard deviation term equals unity and so does not affect the first mean term.

ARGUMENTS:

-t type ... TYPE of adaptive gamma enhancement. The choices are lcc (l), pnae (p) and hybrid (h). The default=hybrid.

-c colormode ... COLORMODE is thecolorspace for processing. The choices are gray, srgb, lab, hcl, ycbcr and ohta. The default=gray.

-a autolevel ... AUTOLEVEL as preprocessing step. The choices are: off (o), together (t) and separately (s). The default=together

-s sigma ... SIGMA value for blurring the intensity-like channel. Values are integer>=0. The default=20. Larger values give slightly more contrast.

-m mix ... MIX is the mixing percent for the hybrid blend of the lcc and pnae types. Values are 0<=integers<= 100. The default=50. 0 is pure lcc and 100 is pure pnae.

-b base .. BASE value for lcc method. Values are floats>0. The default=2. Larger values reduce the contrast and smaller values increase the contrast.

-g gain ... GAIN value for pnae method. Values aare float>=0. The default is computed automatically from the global mean and standard deviation of the intensity-like channel of the image. The nominal value is around 0.5.

REFERENCES:
http://www.ipol.im/pub/art/2011/gl_lcc/article_lr.pdf
https://asp-eurasipjournals.springeropen.com/track/pdf/10.1186/1687-6180-2014-70?site=asp.eurasipjournals.springeropen.com
http://www.ivl.disco.unimib.it/download/capra2010contrast-image.pdf
https://en.wikipedia.org/wiki/Gamma_correction

NOTE: This script may be a bit slow due to the use of -fx.

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


Example 1 -- Variation in Type

Original Image
(source)

Arguments:
-t lcc
Arguments:
-t hybrid
Arguments:
-t pnae

 

Original Image

Arguments:
-t lcc
Arguments:
-t hybrid
Arguments:
-t pnae

 

Original Image
(source)

Arguments:
-t lcc
Arguments:
-t hybrid
Arguments:
-t pnae

 

Original Image
(source)

Arguments:
-t lcc
Arguments:
-t hybrid
Arguments:
-t pnae

 

Original Image
(source)

Arguments:
-t lcc
Arguments:
-t hybrid
Arguments:
-t pnae

 

Original Image
(source)

Arguments:
-t lcc
Arguments:
-t hybrid
Arguments:
-t pnae

 

Original Image
(source)

Arguments:
-t lcc
Arguments:
-t hybrid
Arguments:
-t pnae


Example 2 -- Variation in Colormode

Original Image
(source)

Arguments:
-t gray
Arguments:
-t srgb
Arguments:
-t lab
Arguments:
-t hcl
Arguments:
-t ycbcr
Arguments:
-t ohta


Example 3 -- Variation in Autolevel

Original Image
(source)

Arguments:
-a together
Arguments:
-a separate

 

Original Image
(source)

Arguments:
-a together
Arguments:
-a separate


Example 4 -- Variation in Bias and Gain

Original Image

Arguments:
-t lcc -b 2
Arguments:
-t lcc -b 3
Arguments:
-t lcc -b 4
Arguments:
-t pnae -g 0.4
Arguments:
-t pnae -g 0.5
Arguments:
-t pnae -g 0.6


Example 5 -- Variation in Sigma

Original Image
(source)

Arguments:
-s 5
Arguments:
-s 20
Arguments:
-s 80


What the script does is as follows:

  • Applies -auto-level to stretch the image to full dynamic range
  • Applies adaptive gamma enhancement