Instagram added a Lux button recently, which allows auto-contrasting / leveling for the pictures you take.
I have a bunch of pictures that I need to autolevel in a simi
A simple linear way of performing "auto-contrast" is to linearly stretch and offset the image intensities.
The idea is to find the stretch (contrast) and offset (intensity) correction parameters such that in the corrected image the 5th percentile will be mapped to 0, and the 95th percentile will be mapped to 255.
My example is for a grayscale image. For color images you can convert to any color space that has a single intensity channel and 2 color channels (e.g. Lab, HSV, YUV etc.) and perform this only on the intensity channel.
a
and b
in these 2 simple linear equations:a*p5+b=0
and a*p95+b=255
, where p5
and p95
are the 5th and 95 grey-value percentiles respectively.a
is the contrast, and b
is the intensity corrections.g'=a*g+b
for all g
=0..255. Of course, you might want to use different values for the percentile and the actual mappings. See what works for you.
For those who haven't solved a linear equation in a very long time (such as I) here the required parameters from Adi Shavit's answer in a formula (in pseudo code) - I renamed p5
and p95
to p1
and p2
(as 5 is just a suggestion):
p_div := float(p1) / p2;
b := -255 * p_div / (1 - p_div);
a := (255 - b) / p2;
I don't know if it can be done easier, but you can check that:
a * p1 + b -> 0
a * p2 + b -> 255