Apply standard brightness/saturation/contrast

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
deniswsrosa
Posts: 4
Joined: 2017-06-07T01:20:12-07:00
Authentication code: 1151

Apply standard brightness/saturation/contrast

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Apply standard brightness/saturation/contrast

Post 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.
snibgo's IM pages: im.snibgo.com
deniswsrosa
Posts: 4
Joined: 2017-06-07T01:20:12-07:00
Authentication code: 1151

Re: Apply standard brightness/saturation/contrast

Post by deniswsrosa »

Hi!
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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Apply standard brightness/saturation/contrast

Post by snibgo »

Useful links for getting started:
http://www.imagemagick.org/Usage/basics/
http://www.imagemagick.org/Usage/
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/script/comma ... 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 dropbox.com and paste links here.
snibgo's IM pages: im.snibgo.com
deniswsrosa
Posts: 4
Joined: 2017-06-07T01:20:12-07:00
Authentication code: 1151

Re: Apply standard brightness/saturation/contrast

Post 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
http://3.bp.blogspot.com/-awVv7mV3lTs/T ... 00/RG2.JPG

But most of images are like:
http://3.bp.blogspot.com/-EX_Xy-BhIlk/U ... 600/oi.png
https://1.bp.blogspot.com/-mf33JuJWkT0/ ... 1600/2.jpg
http://1.bp.blogspot.com/-0bEWp7P6oIo/U ... M00763.jpg
http://1.bp.blogspot.com/-wnHQ0JrnwPY/T ... C04750.JPG
https://1.bp.blogspot.com/-YIiNIO1NJwI/ ... 4460_n.jpg
https://4.bp.blogspot.com/-Z3KCM6yIT-Q/ ... 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Apply standard brightness/saturation/contrast

Post 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.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Apply standard brightness/saturation/contrast

Post 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 imagemagick.org/script/…. Also -auto-level and -auto-gamma combined are effective and simple. – fmw42 yesterday
deniswsrosa
Posts: 4
Joined: 2017-06-07T01:20:12-07:00
Authentication code: 1151

Re: Apply standard brightness/saturation/contrast

Post 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:
http://imgur.com/a/Mav3q

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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Apply standard brightness/saturation/contrast

Post 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
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Apply standard brightness/saturation/contrast

Post by glennrp »

@fred, thanks for autotone. I just updated an old answer of mine on StackExchange to add a link to it:
https://graphicdesign.stackexchange.com ... 7593#77593
Almost immediately I got a downvote. Go figure. I guess they didn't like it being noncommercial use only.
Post Reply