Page 1 of 1

Apply standard brightness/saturation/contrast

Posted: 2017-06-07T01:30:23-07:00
by deniswsrosa
Hi Guys,

I have a dataset of similar images with different levels of saturation, brightness, and contrasts. Is there some way to "normalize" those three factors to force the images to look alike? I mean, I would like to have a standard brightness/saturation and make for example dark images lighter and light images darker

Re: Apply standard brightness/saturation/contrast

Posted: 2017-06-07T02:11:30-07:00
by snibgo
What version of IM? I'll assume v7. On what platform?

The first step is to define your measurements.

Assuming sRGB images, brightness and contrast might be the mean and standard deviation (SD) of (a) all the channels or (b) the lightness channel of Lab.

Saturation might be the mean value in the C channel of HCL.

Then define what you want those values to be, on a scale of 0.0 to 1.0. For example: mean=0.5, SD=0.16, saturation=0.15.

Then decide how to adjust the images. For example, changing the mean (for all channels, or just one channel) is easily done by raising values to a power:

Code: Select all

magick in.ext -evaluate Pow %[fx:log(0.5)/log(mean)] out.ext
... which will change the mean value to 0.5.

Changing the SD is harder. I do it by "-sigmoidal-contrast" with trial and error.

Re: Apply standard brightness/saturation/contrast

Posted: 2017-06-07T02:41:21-07:00
by deniswsrosa
I am using v7 on Mac.
Thanks a lot for your answer! I am quite new on ImageMagick, so barely understood what you said. I will try to find out how to get all those values and them I will come back to this thread.

Re: Apply standard brightness/saturation/contrast

Posted: 2017-06-07T03:03:19-07:00
by snibgo
Useful links for getting started: ... essing.php ... ptions.php

At the risk of confusing you, I'll mention a couple of points.

1. Brightness, contrast and saturation are not independent. Changing any one usually also somewhat changes the others. So if you want a "perfect" result, you may need to iterate.

2. There are simple methods for making one image "look like" another. For example, see my Gain and bias page.

3. If you can show sample images, we can give more specific guidance. You can upload to somewhere like and paste links here.

Re: Apply standard brightness/saturation/contrast

Posted: 2017-06-07T03:34:23-07:00
by deniswsrosa
Wow, thanks a lot!
I am reading all references that you posted.
To summarize my problem, the ideal image would be something like ... 00/RG2.JPG

But most of images are like: ... 600/oi.png ... 1600/2.jpg ... M00763.jpg ... C04750.JPG ... 4460_n.jpg ... 1%255D.JPG

I am doing some OCR, so I pre-process those images to make my OCR engine be able to recognize it, I have some filters that are able to remove the green background for example, but in order to do it accurately, the images should have a similar background color.

Re: Apply standard brightness/saturation/contrast

Posted: 2017-06-07T10:18:18-07:00
by fmw42
You have multiple problems.

1) images with glare (blown out regions that are all white from too much light)
2) blurred images
3) faded images
4) images with uneven illumination

There is not one universal solution. I doubt there is anything you can do about glare. Some sharpening can help if the blur is not too extreme. Faded images could be helped simply by increasing the contrast and/or saturation. Images with uneven illumination may be helped by my unix bash shell script, textcleaner. Some of the sharpening and contrast improvement may be helped by my script, autotune (or simply -auto-level, -auto-gamma, -modulate -- the latter for saturation).

All my scripts at my link below run on Unix-like systems. So that includes Mac OSX. I developed them on Mac OSX.

Re: Apply standard brightness/saturation/contrast

Posted: 2017-06-07T12:22:57-07:00
by glennrp
Here are comments by me and fmw42 on the StackOverflow instance of this question:

Have you tried the "-normalize" option of ImageMagick? There are more complex methods that produce better results, but you might like the result as a first cut at fixing them. – Glenn Randers-Pehrson yesterday

Just to add a little to Glenn's command, a simple approach is just to stretch the histogram. You can use -contrast-stretch 0% to stretch the histogram to full dynamic range or to clip a little off the dark and light ends of the histogram to stretch it even more. This increases contrast/brightness. -normalize is just -contrast-stretch 2%x1%. See…. Also -auto-level and -auto-gamma combined are effective and simple. – fmw42 yesterday

Re: Apply standard brightness/saturation/contrast

Posted: 2017-06-08T05:41:55-07:00
by deniswsrosa
Thanks a lot guys, all comments here were really helpful

I am really impressed how autotone just works out of the box, this simple command increased the number of characters recognized in 8%:

Code: Select all

./autotone  -P 80 in.jpg out.jpg
Textcleaner however did not work quite well for the type of images that I have.

I still have some corner cases where the background pattern is interfering in the OCR, like this one:

Is there any way get rid of this background pattern without messing up with the letters?

I have tried to force the letters to be more visible, like:

Code: Select all

convert rgfix-7.jpg -fuzz 30% -fill black -opaque '#172126' result.png
And also some noise reduction, but no success so far.

Re: Apply standard brightness/saturation/contrast

Posted: 2017-06-08T10:23:35-07:00
by fmw42
Try something like either of these. Adjust the values as desired

Code: Select all

convert fK1Mz6L.jpg -fuzz 15% -fill black -opaque "#2E303A" -fuzz 0 -fill white +opaque black result.png

Code: Select all

convert fK1Mz6L.jpg -threshold 35% result.png

Re: Apply standard brightness/saturation/contrast

Posted: 2017-06-10T11:34:55-07:00
by glennrp
@fred, thanks for autotone. I just updated an old answer of mine on StackExchange to add a link to it: ... 7593#77593
Almost immediately I got a downvote. Go figure. I guess they didn't like it being noncommercial use only.