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.

ADAPTIVEGAMMA


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

Download Script

last modified: December 15, 2018



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, hsl,
.................... hsv, hsi, 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
-T ... thresh ...... threshold to determine if image is bright or dark; 0<=float<=1;
.................... default=0.5. If bright, the image will be negated before and
.................... after processing

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, hsl, hsv, hsi, 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.

-T thresh ... THRESH is a threshold to determine if the image is bright or dark. Values are 0<=float<=1. The default=0.5. If bright, the image will be negated before and after processing.

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


Original Image
(http://3.bp.blogspot.com/-iOOnD5rmkxk/Ta7Vi7ePTsI/AAAAAAAAANM/EgDnXlR0HHU/s1600/Chloe%2BBell%2B045.JPG)

 

Arguments:
(defaults)


Original Image
(provided by Anthony Thyssen)

 

Arguments:
(defaults)


Original Image
(http://www.asknlearn.com/EduWiz/help/Chapter9/Chapter9.htm)

 

Arguments:
(defaults)


Original Image
(http://enhance.pho.to/)

 

Arguments:
(defaults)


Original Image
(http://enhance.pho.to/)

 

Arguments:
(defaults)


Original Image
(http://warrencampdesign.com/warren/images/photoEdits/basics/focusContrast/lions_before.jpg)

 

Arguments:
(defaults)


Original Image
(http://digital-photography-school.com/turn-ho-hum-color-into-wow-with-photoshop)

 

Arguments:
(defaults)


Original Image
(http://dragon.larc.nasa.gov/retinex/757/)

 

Arguments:
(defaults)


Original Image
(http://dragon.larc.nasa.gov/retinex/757/)

 

Arguments:
(defaults)


Original Image
(http://dragon.larc.nasa.gov/retinex/757/)

 

Arguments:
(defaults)


Original Image
(http://sipi.usc.edu/database/database.php?volume=misc&image=1#top)

 

Arguments:
-gt 0.55 -dg 1
(defaults)


Original Image
(http://sipi.usc.edu/database/database.php?volume=misc&image=1#top)

 

Arguments:
-gt 0.55 -dg 1
(defaults)


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


Variation in Colormode

Original Image
(source)

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


Variation in Autolevel

Original Image
(source)

Arguments:
-a together
Arguments:
-a separate

 

Original Image
(source)

Arguments:
-a together
Arguments:
-a separate


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


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