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.

ADAPTIVEGAMMA2


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

Download Script

last modified: November 17, 2023



USAGE: adaptivegamma2 [-t type][-m meanthresh] [-f fraction] [-c cumthresh] [-bg brightgain] [-dg darkgain] [-ba brightalpha] [-da darkalpha] [-C colormode] infile outfile

USAGE: adaptivegamma2 [-h or -help]

-t .... type ............ type of mean threshold to use; choices are: orginial or simple;
......................... default=simple
-m .... meanthresh ...... threshold of the mean used to test if the image is bright
......................... or dark; 0<=integer<=255; default=128 (use 112 for original)
-f .... fraction ........ fraction to use with the meanthresh to test if the image
......................... is bright or dark for type=original; ignored if type=simple;
......................... 0<=float<=1; default=0.3
-c .... cumthresh ....... threshold used to clip the cumulative distribution for dark
......................... images; 0<=float<=1; default=0.55
-bg .... brightgain ..... gain on cumulative distribution for bright images;
......................... 0<=float<=1; default=1 (no change)
-dg .... darkgain ....... gain on cumulative distribution for dark images;
......................... 0<=float<=1; default=1 (no change)
-ba .... brightalpha .... alpha exponent for weighting the normalize histogram for
......................... bright images; 0<=float<=1; default=0.25
-da .... darkalpha ...... alpha exponent for weighting the normalize histogram for
......................... dark images; 0<=float<=1; default=0.75
-C .... colormode ....... colorspace for processing; choices are srgb, lab, hcl, hsl,
......................... hsv, hsi, ycbcr and ohta; default=hsv

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

DESCRIPTION: ADAPTIVEGAMMA2 is an adaptive gamma technique used to enhance an image's brightness and contrast. It is based upon an exponentiation of the the image's values using an exponent, gamma, that is evaluated differently for each graylevel in the image based upon values in the cumulative normalized and weight histogram in the form, gamma(value)=1-cumulative_histogram(value). The fraction along with the meanthresh value are used to categorize the image as bright, dark (or neutral if type=original). Neutral images will have no processing applied. For dark images the cumulative distribution is clipped not to exceed cumthresh. For bright images, no clipping is used, but the input is negated before gamma processing and then negated back after processing.

ARGUMENTS:

-t type ... TYPE of mean threshold to use. The choices are: orginial or simple. The default=simple.

-m meanthresh ... MEANTHRESH is a threshold of the mean that is used to test if the image is bright or dark. Values are 0<=integer<=255. The default=128. (Use 112 for original).

-f fraction ... FRACTION to use with the meanthresh to test if the image is bright or dark. Values are 0<=float<=1. The default=0.3.

-c cumthresh ... CUMTHRESH is a threshold used to clip the cumulative distribution for dark images. Values are 0<=float<=1. The default=0.55.

-bg brightgain ... BRIGHTGAIN ia a gain on cumulative distribution for bright images. Values are 0<=float<=1. The default=1 (no change).

-dg darkgain ... DARKGAIN is a gain on cumulative distribution for dark images. Values are 0<=float<=1. The default=1 (no change).

-ba brightalpha ... BRIGHTALPHA is an alpha exponent for weighting the normalize histogram for bright images. Values are 0<=float<=1. The default=0.25.

-da darkalpha ... DARKALPHA is an alpha exponent for weighting the normalize histogram for dark images. Values are 0<=float<=1. The default=0.75.

-C colormode ... COLORMODE is the colorspace for processing; choices are srgb, lab, hcl, hsl, hsv, hsi, ycbcr and ohta; default=hsv

REFERENCES:
https://arxiv.org/pdf/1709.04427.pdf
Contrast Enhancement of Brightness-Distorted Images by Improved Adaptive Gamma Correction
Gang Cao1*, Lihui Huang1, Huawei Tian2, Xianglin Huang3, Yongbin Wang1, Ruicong Zhi4

https://in.mathworks.com/matlabcentral/answers/uploaded_files/4242/AGCWD.pdf
Efficient Contrast Enhancement Using Adaptive Gamma Correction With Weighting Distribution
Shih-Chia Huang, Fan-Chieh Cheng, and Yi-Sheng Chiu
IEEE TRANSACTIONS ON IMAGE PROCESSING, VOL. 22, NO. 3, MARCH 2013

NOTE: This script may be a bit slow due for non-HDRI compile Imagemagick 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 ColorMode

Original Image
(source)

Arguments:
-C hsv (default)
Arguments:
-C hsl
Arguments:
-C hsi
Arguments:
-C hcl
Arguments:
-C srgb
Arguments:
-C lab
Arguments:
-C YCbCr
Arguments:
-C OHTA
Arguments:
 
 


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)
Arguments:
-gt 0.55 -dg 0.7
Arguments:
-gt 0 -dg 1
Arguments:
-gt 0 --dg 0.7


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

 

Arguments:
-gt 0.55 -dg 1
(defaults)
Arguments:
-gt 0.55 -bg 0.8


What the script does is as follows:

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