How to estimate optimal gamma parameter for gamma correction?

Viewed 558

Is it possible to estimate optimal gamma parameter for gamma correction by algorithm using some image statistics? By 'optimal' I mean that image should 'look good' for human on average after correction.

2 Answers

If your image pixels are scaled on the range 0..255, you could use:

gamma = log(mean)/log(128)

where mean is the mean of your image pixels. If they are scaled on the range 0..1:

gamma = log(mean)/log(0.5)

Note that this is the technique that ImageMagick uses, documentation here, and you can test it yourself on the command line with:

magick input.jpg -auto-gamma result.jpg

Nothing is perfect though and that may not look good if there are heavy shadows or bright areas in your images.

The so-called gamma correction is a weird beast, which exists for historical reasons. It was initially implemented by TV broadcasters to deal with the fact that cathode ray tubes did not have a linear response to the signal amplitude. And rather than compensate in every TV set, i.e. in the receiver, they decided to compensate in the emitter. This also had a nice dynamic compression effect.

As time passed, the pre-compensation remained in the standards, and for modern devices that have a linear response, the pre-compensation must be cancelled by applying a gamma correction with the inverse exponent. So when you get an image from an unknown source, it is unsure if it needs to be gamma de-corrected, and with which exponent.

This said, a gamma exponent is also used in a complete empirical way to strengthen or weaken the dark tones, and conversely. A priori, the concept of an "optimal" gamma exponents is quite subjective and will differ depending on the atmosphere you want to give to your picture, and depending on the particular subject.

I don't know of any technique to choose a gamma value automatically. If I had to, I would choose some feature drawn from the image histogram (such as mean, deviation, coefficient of variation...) and adjust the gamma until that criterion reached a particular value. As the histogram does not have an analytic form, a trial and error process (such as a dichotomic search) is necessary.

Also have a look at the "histogram specification" technique.

Related