Page 1 of 2

3 Implementations for Adaptive Gamma Correction

Posted: 2018-11-08T15:11:01-07:00
by chani
Hi there,

I had some very interesting and headache-full days trying to implement the proposed methods of three papers I've found. I decided to use PHP using \Imagick. I believe that those could be easily implemented with pure imagemagick / shell scripts. I'd like to share my results in the hope that my results and my work on this is helpful or at least interesting.

First of all. Why? ImageMagicks -auto-gamma did not satisfy me. My photos range from bright landscapes over buildings on a sunny day with lots of detail hidden in the shadows up to dark/dimmed images of the sea. I also do have pictures with little detail - like a bird in the upper-right of the photo. It's easy to guess that -auto-gamma does have trouble with some of these. Hence I was looking for something.. adaptive.

I am going to use the following (non official) abbreviations to allow to distinguishing between the three methods: agcwd / iagcwd / agcie.

A hopefully correct (and most likely not full) description of the three methods: AGCWD is an adaptive gamma correction with weighting distribution - probably known by most people because it is around for quite some years now. iagcwd is an improved version of that which (basically) tries to enhance brightness-distorted photos and tries to get around the problems that arise using agcwd. agcie is a completely different implementation which tries to minimize the negative effects of prior adaptive gamma correction methods.

Pictures are always in this order: original / agcwd / iagcwd / agcie.

ImageImageImageImage

ImageImageImageImage

ImageImageImageImage

ImageImageImageImage

I wouldn't say that any of these methods is best or worse - they do all play very good for specific photos and not so good for other photos. AGCWD looks the most pleasant to me - it does however loose detail/contrast in bright areas. You can push such a photo a lot by 1. agcwd, 2. levels, 3. unsharp mask. The third method can be improved by feeding it with a denoised-image for the transformation and by using a trim-to-detail image for classification & transformation. I also played around with averaging AGCWD & IAGCWD which lead to good results.

My implementation is available at Github: https://github.com/chani/AdaptiveGammaCorrection
More pictures / results with the full-size photos can be seen at my blog: https://jeanbruenn.info/2018/11/06/anot ... mentation/

The papers:
  • AGCWD: S. Huang, F. Cheng and Y. Chiu, „Efficient Contrast Enhancement Using Adaptive Gamma Correction With Weighting Distribution,“ in IEEE Transactions on Image Processing, vol. 22, no. 3, pp. 1032-1041, March 2013. doi: 10.1109/TIP.2012.2226047
  • IAGCWD: Cao, Gang & Huang, Lihui & Tian, Huawei & Huang, Xianglin & Wang, Yongbin & Zhi, Ruicong. (2017). Contrast Enhancement of Brightness-Distorted Images by Improved Adaptive Gamma Correction. CAEE. 10.1016/j.compeleceng.2017.09.012.
  • AGCIE: Rahman, Shanto & Rahman, Md. Mostafijur & Abdullah-Al-Wadud, M & Al-Quaderi, Golam Dastegir & Shoyaib, Mohammad. (2016). An adaptive gamma correction for image enhancement. EURASIP Journal on Image and Video Processing. 35. 10.1186/s13640-016-0138-1.
Jean

P.S: I'm just a hobbyist :^)

Re: 3 Implementations for Adaptive Gamma Correction

Posted: 2018-11-16T13:02:19-07:00
by chani
Hm... Nobody? :D I would have expected some answer. Not sure what exactly... :D

Re: 3 Implementations for Adaptive Gamma Correction

Posted: 2018-11-16T13:43:22-07:00
by snibgo
Thank you for the contribution and the references.

I've only had a brief look at them, but they seem interesting, and potentially useful.

To me, there are two different uses for such techniques:

1. To present an overview of where my photo has detail, and where it doesn't. This is for information, not aesthetics, and the result image may look horrible. But it tells me, for example, that the shadows have no useful detail, but the highlights do.

2. To make an aesthetically pleasing image, or a series of images that might be pleasing from which I can choose the best. I'm interested in how far this can be automated, or how to make intuitive human guidance, eg "Give me the top right from image #2, the bottom-left from image #3, and the rest from image #1".

Re: 3 Implementations for Adaptive Gamma Correction

Posted: 2018-11-16T14:11:18-07:00
by chani
Hey snibgo,

thanks for answering. Now that I worked myself through those (and a few more) papers, I do believe the most difficult and at the same time most important part in automating it is working with classification. i.e. Transforming bright and dark areas differently, Transforming low and high contrast differently. When I was playing around with those implementations I noticed that some can be fooled (Take a look at my first picture, especially note the dark results). The classification part fails and hence the picture is transformed wrongly. Doing a trim-to-detail just for the classification part sort of solves that.

A few more ideas involved denoising to make the classification more accurate. However, with and without denoising I got nearly the same results - the difference is not visible.

A good way to go might be to classify according to those papers and choose different algorithms for different classification results. That would however mean to create something new based on those papers - I just wrote the implementation as it is explained in the paper (to be as much as possible at the paper).

Probably one might - if you trust exif - also use exif information to determine how to transform; e.g. you would transform a night-picture differently than a daylight picture, as well as a cloudy-weather picture.

By the way: An implementation for AGCWD in addition to things like -auto-gamma and sigmodial contrast, would be pretty cool (feature request!). However, I'm still searching the holy-grail in adaptive gamma correction.

Jean

Re: 3 Implementations for Adaptive Gamma Correction

Posted: 2018-11-16T17:46:14-07:00
by fmw42
I have bash unix implementations of histogram based AGC (without weighting), AGCWD (with weighting), and an entropy method similar to AGC but substitutes the cumulative entropy for the cumulative pdf. They are all just in testing phases. They all seem to give similar results. But one or another method works best for different images. So no one good solution. I also have another approach that I did make into a format script some while ago on my web site below called, adaptivegamma. It looks at the local mean and std about each pixel.

The entropy reference is:

Entropy-Based Adaptive Gamma Correction for Content Preserving Contrast Enhancement
Anil Singh Parihar, Department of Information Technology, Delhi Technological University Delhi, India
International Journal of Pure and Applied Mathematics Volume 117 No. 20 2017, 887-893

https://acadpubl.eu/jsi/2017-117-20-22/ ... /20/79.pdf


My adaptivegamma script is based upon:

http://www.ipol.im/pub/art/2011/gl_lcc/article_lr.pdf
https://asp-eurasipjournals.springerope ... eropen.com



P.S. chani and I have been exchanging ideas and code.

Re: 3 Implementations for Adaptive Gamma Correction

Posted: 2018-11-17T02:07:18-07:00
by snibgo
A non-weighted AGC, as given in the "Contrast Enhancement of Brightness-Distorted Images by Improved Adaptive Gamma Correction, Gang Cao" paper (Windows BAT syntax):

Code: Select all

%IM7DEV%magick ^
  %SRC% ^
  +write mpr:INP ^
  -process 'mkhisto cumul norm verbose' ^
  -negate ^
  ( +clone ^
    -sparse-color bilinear 0,0,Black,%%[fx:w-1],0,White ^
  ) ^
  -fx pow(v,u) ^
  mpr:INP ^
  +swap ^
  -clut ^
  out.png
I hate using "-fx", but it is on a Nx1 image where N=255 or 65535, so not a big performance problem. IM seems to have a "-evaluate-sequence Pow", but I've never understood how to use it.

mkhisto is Process modules: mkhisto

Input:
Image

Output:
Image

The verbose output from mkhisto is:

Code: Select all

mkhisto options: capnumbuckets 65536  multiply 1  cumul norm verbose
mkhisto: Input image [1709-003-000.jpg] depth is 8
  NumBuckets 256  BucketSize 1.67772e+07
  counts: min_value 1, max_value 24082
  sum_red 171366, sum_green 171366, sum_blue 171366
  Cumulating and normalising...
    max_sum=171366 mult_fact=25063.1
If I understand correctly, "min_value 1, max_value 24082" would be used to calculate alpha, the weighting function.

Re: 3 Implementations for Adaptive Gamma Correction

Posted: 2018-11-17T03:13:12-07:00
by snibgo
My result above reduces contrast in the light helmets and mudguards, burning them out. The Gang Cao paper also has this, even with different alphas. That result is unacceptable (to me).

Following Gang Cao, in my command we can negate the input and output, which gives a stretching in the highlights (which has little effect in this image). Then we can take the mean of the two results, or we can use that mean as a mask to compose the negated result over the non-negated result:

Mean:
Image

Masked composite:
Image

Another possibility comes from blending the two cluts:

Image

Re: 3 Implementations for Adaptive Gamma Correction

Posted: 2018-11-18T10:30:11-07:00
by snibgo
snibgo wrote:IM seems to have a "-evaluate-sequence Pow", but I've never understood how to use it.
Okay, I've figured that out. If we have:

Code: Select all

magick in1 in2 -evaluate-sequence out
then out becomes in2 to the power in1 (not the other way round), where the inputs and output are not normalised to or from 0.0 to 1.0. So we can write my previous command as:

Code: Select all

%IM7DEV%magick ^
  %SRC% ^
  +write mpr:INP ^
  -process 'mkhisto cumul norm verbose' ^
  -negate ^
  -evaluate Divide %%[fx:QuantumRange] ^
  ( +clone ^
    -sparse-color bilinear 0,0,Black,%%[fx:w-1],0,White ^
  ) ^
  -evaluate-sequence Pow ^
  mpr:INP ^
  +swap ^
  -clut ^
  out.png

Re: 3 Implementations for Adaptive Gamma Correction

Posted: 2018-11-18T15:23:25-07:00
by snibgo
The weighting shown in "Efficient Contrast Enhancement Using Adaptive Gamma Correction and Cumulative Intensity Distribution" by Yi-Sheng Chiu et al has the effect of raising (when 0 < alpha < 1) or lowering (when alpha > 1) the values in the Probability Density Function before it is cumulated. Hence when 0 < alpha < 1, the effect is reduced, to a minimum when alpha=0.

AGCWD (Adaptive Gamma Correction with Weighting Distribution) can be implemented with HDRI IM as (Windows BAT syntax):

Code: Select all

%IM7DEV%magick ^
  %SRC% ^
  +write mpr:INP ^
  -process 'mkhisto verbose' ^
  -auto-level ^
  -evaluate Pow %alpha% ^
  -process 'cumulhisto norm verbose' ^
  -negate ^
  -evaluate Divide %%[fx:QuantumRange] ^
  ( +clone ^
    -sparse-color bilinear 0,0,Black,%%[fx:w-1],0,White ^
  ) ^
  -evaluate-sequence Pow ^
  mpr:INP ^
  +swap ^
  -clut ^
  out3.png
As the papers say, it will "improve the brightness of dimmed images". It expands shadows (increasing contrast there) at the expense of highlights (which it wrecks). If the image has no highlights or we don't care about them, that's fair enough.

A more common situation (for me) is a photo that is "dimmed" because I've taken care not to clip highlights in the camera. AGCWD would wreck the highlights, so it's no good for me.

NOTE: My commands above apply the technique to RGB colour channels independently. However, the Gang Cao paper says the techniques should enhance colour images "by applying them to the luminance channel and preserving chrominance channels in a certain color space."

The Gang Cao paper deals with highlights by truncating the CDF. I haven't implemented this (yet).

Re: 3 Implementations for Adaptive Gamma Correction

Posted: 2018-11-18T16:36:45-07:00
by fmw42
Is there a link to "Efficient Contrast Enhancement Using Adaptive Gamma Correction and Cumulative Intensity Distribution" by Yi-Sheng Chiu where one can download the paper and not just a citation?

Re: 3 Implementations for Adaptive Gamma Correction

Posted: 2018-11-18T17:24:35-07:00
by snibgo

Re: 3 Implementations for Adaptive Gamma Correction

Posted: 2018-11-18T18:42:59-07:00
by fmw42
Seems to be pretty much the same as ACGWD paper by the same authors but in a different order of the authors.

Re: 3 Implementations for Adaptive Gamma Correction

Posted: 2018-11-18T19:11:11-07:00
by snibgo
Truncating the CDF to reduce the damage to highlights is easy, with "-evaluate Max 50%" between "-negate" and "-evaluate Divide". The Gang Cao paper suggests alpha=0.25 and tau=0.5 (ie 50%, in IM terms).

Well, hmm, yeah, it works -- it expands the shadows without wrecking the highlights. But now we have two controls, alpha and tau, with no particular justification for their values, and without obvious intuitive meanings. They are "fiddle factors".

The process is complex, and has limited use. I can't see a great advantage over much simpler forms, eg:

Code: Select all

magick 1709\1709-003-000.jpg -auto-level -evaluate Pow %[fx:log(0.3)/log(mean)] 1709-alp.png
This stretches to use the full range 0 to 100%, then adjusts gamma so what was the mean becomes 30%. (Which puts the new mean at roughly 30%.) There is one control. Lower values make it darker, higher values make it lighter. 30% looks good to me.

Image

Re: 3 Implementations for Adaptive Gamma Correction

Posted: 2018-11-18T20:16:02-07:00
by fmw42
That is pretty much the same as -auto-level -auto-gamma, but where 0.3 is replaced by 0.5. I tried to get Anthony to include an argument or define for the desired value (0.5), but he did not want to do it. But I have it on my enhancement list to recommend whenever there is time for that to be added. My script autogamma allows the 0.5 (default value) to be adjusted.

Re: 3 Implementations for Adaptive Gamma Correction

Posted: 2018-11-19T05:50:54-07:00
by snibgo
fmw42 wrote:That is pretty much the same as -auto-level -auto-gamma, but where 0.3 is replaced by 0.5.
Yes. "-auto-gamma" works fine on this image, and the result is acceptable (to me). Aesthetically I prefer a darker more gritty version, which we can get with "-evaluate Pow" after "-auto-gamma", or we can combine both operations into one as I show here.