Copyright © Fred Weinhaus My scripts are available free of charge for noncommercial (nonprofit) use, ONLY. For use of my scripts in commercial (forprofit) environments or nonfree 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 forprofit 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 
Enhances the contrast/brightness in an image using a locally adaptive gamma method. 
last modified: October 10, 2017
USAGE: adaptivegamma [t type] [c colormode] [a autolevel] [s sigma] [m mix ]
[b base] [g gain] infile outfile
t ... type ........ type of adaptive gamma enhancement; choices are lcc, pnae, hybrid; 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 intensitylike 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 intensitylike channel achieve by blurring the image and base is nominally 2. In the pnae method, exponent=const*[M(x,y)epsilon]/[1M(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 intensitylike 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 intensitylike channel of the image. The nominal value is around 0.5.
REFERENCES: 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. 
Example 1  Variation in Type 

Original Image 

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


Original Image 

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


Original Image 

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


Original Image 

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


Original Image 

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


Original Image 

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


Original Image 

Arguments: t lcc 
Arguments: t hybrid 
Arguments: t pnae 
Example 2  Variation in Colormode 

Original Image 

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 

Arguments: a together 
Arguments: a separate 



Original Image 

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 

Arguments: s 5 
Arguments: s 20 
Arguments: s 80 
What the script does is as follows:
