Make an Adaptive Histogram Equalization

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?".
MathieuC
Posts: 6
Joined: 2013-06-05T02:56:34-07:00
Authentication code: 6789

Make an Adaptive Histogram Equalization

Post by MathieuC »

Hello,

I'm trying to convert this image :
Image

to this image :
Image

I read the page about Color Modification but i don't find the good args to apply. I would like to try the redist script from Fred's ImageMagick Scripts but i'm using the windows version of ImageMagick and I cannot install cygwin.

Does somebody knows the way to convert the first image to the second ? Or to use the redist script on windows ?

Best regards
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Make an Adaptive Histogram Equalization

Post by snibgo »

This is quite close:

Code: Select all

convert originalnew.jpg ( +clone -equalize ) -average o.jpg
Instead of taking the average, you could blend.
snibgo's IM pages: im.snibgo.com
MathieuC
Posts: 6
Joined: 2013-06-05T02:56:34-07:00
Authentication code: 6789

Re: Make an Adaptive Histogram Equalization

Post by MathieuC »

Thank a lot, it's working
MathieuC
Posts: 6
Joined: 2013-06-05T02:56:34-07:00
Authentication code: 6789

Re: Make an Adaptive Histogram Equalization

Post by MathieuC »

I spoke too fast, with this image is do not work :

Original :
Image

After convert originalnew.jpg ( +clone -equalize ) -average o.jpg :
Image

The same image process with the software Color Factory of Fotoware, it's using the adaptive historgram equalization technique (very powerful) : Image

I'm trying to do the same image process with ImageMagick.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Make an Adaptive Histogram Equalization

Post by fmw42 »

What version of IM are you using. If old it may be equalizing differently for each channel. If new the default is to equalize the same for all channels.

If you are on a recent version of IM, you can try both

-equalize

or

-channel rgb -equalize

see if either give you a good result.

The only way to use my redist, is if you install Cygwin on Windows.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Make an Adaptive Histogram Equalization

Post by snibgo »

"Equalize" is an extreme effect that can benefit from other options. For example (in v6.8.5-7):

Code: Select all

convert room.jpg ( +clone -channel RGB -equalize ) -average r.jpg
... seems a good result to me.

A huge number of histogram equalization effects are available using just ImageMagick. In addition, IM can generate a histogram for processing by an external program, to create a clut which is then applied to the image.

I use the external program technique for various processing, including what Wiki calls "contrast limited adaptive histogram equalization (CLAHE)", and various others including my current favourite, which involves finding the first and last peaks in the histogram, and increasing the contrast between the peaks at the expense of contrast outside the peaks. (Hence, the darkest tones will become darker, while the lightest tones will become lighter.)

Your sample result from Fotoware appears to do something similar: the dark doorway becomes very dark, while the middle tones of the right and middle walls have become spread further apart. This is very similar to the result from my script in this post, but slightly more extreme. So a blend instead of average would probably nail it.
snibgo's IM pages: im.snibgo.com
MathieuC
Posts: 6
Joined: 2013-06-05T02:56:34-07:00
Authentication code: 6789

Re: Make an Adaptive Histogram Equalization

Post by MathieuC »

@fmw42
I'm using ImageMagick 6.8.5-4 2013-04-28 Q16

@snibgo
It's better thank to you. What is the software/process do you use to make the CLAHE process ?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Make an Adaptive Histogram Equalization

Post by fmw42 »

Did you try my suggestion of testing

-equalize

or

-channel rgb -equalize
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Make an Adaptive Histogram Equalization

Post by snibgo »

MathieuC wrote:What is the software/process do you use to make the CLAHE process ?
My own software. The contrast limiting is very simple: after reading the histogram, it can cap each value before calculating the cumulative. For the cap value, the code calculates the mean and standard deviation of the histogram counts. The user specifies a multiple of the standard deviation. The cap is the mean, plus this multiple times the StdDev.

This comes after other process such as filling null counts. (A 14-bit camera can only generate 2^14 values, so a 16-bit histogram has counts in only about 1/4 of the values. The histogram is shaped like a comb, which confuses some processes. I fill in null counts by spreading non-zero counts.)
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Make an Adaptive Histogram Equalization

Post by fmw42 »

There is code available at http://tog.acm.org/resources/GraphicsGe ... iv/clahe.c if anyone wants to implement it. It is a reference in http://en.wikipedia.org/wiki/Adaptive_h ... ualization

snibgo wrote:My own software. The contrast limiting is very simple: after reading the histogram, it can cap each value before calculating the cumulative. For the cap value, the code calculates the mean and standard deviation of the histogram counts. The user specifies a multiple of the standard deviation. The cap is the mean, plus this multiple times the StdDev.
Just curious if you redistributed the capped values on the histogram as described in the Wikipedia page?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Make an Adaptive Histogram Equalization

Post by snibgo »

fmw42 wrote:Just curious if you redistributed the capped values on the histogram as described in the Wikipedia page?
I didn't. I wasn't sure of the value of doing so. Redistributing seemed to be the equivalent of simply lowering the cap slightly. The magic (or the art) is in selecting the best cap value.

But I could be wrong about redistribution; when I get some time, I intend to experiment.
snibgo's IM pages: im.snibgo.com
MathieuC
Posts: 6
Joined: 2013-06-05T02:56:34-07:00
Authentication code: 6789

Re: Make an Adaptive Histogram Equalization

Post by MathieuC »

Does someone knows a software who implements the CLAHE process (freeware/shareware) and who can be call with a DOS command line ?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Make an Adaptive Histogram Equalization

Post by fmw42 »

MathieuC wrote:Does someone knows a software who implements the CLAHE process (freeware/shareware) and who can be call with a DOS command line ?
Not DOS, but you can download CLAHE plugin for ImageJ for all platforms

see
http://rsb.info.nih.gov/ij/plugins/clahe/index.html
http://rsb.info.nih.gov/ij/download.html

It is also in OpenCV library,

see
http://opencv.org/
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Make an Adaptive Histogram Equalization

Post by snibgo »

If you are playing with adaptive histogram equalisation, you might look into the strongly related technique of pseudo-HDR. (HDR = High Dynamic Range.)

Here's a quick and easy pseudo-HDR. Windows script, but fairly easily translatable. It merges two equalised images: the darkest 30% and lightest 70%. (Adjust the boundary, CUTPC, as desired.) The mask used for the blend is a heavily blurred version of the original, which thus replicates the Adaptive effect of Adaptive Histogram Equalisation.

This script doesn't limit the contrast introduced by equalisation, so noise in areas of fairly constant tone becomes exagerated.

Code: Select all

set SRC=sm.jpg

set CUTPC=30

FOR /F "usebackq" %%L IN (`%IM%identify -format "WW=%%w\nHH=%%h" %SRC%`) DO set %%L

"convert" %SRC% ^
  -alpha off ^
  -colorspace HSL -channel B ^
  -equalize ^
  -colorspace sRGB -channel RGB,sync ^
  ( -clone 0 -level 0%%,%CUTPC%%% ) ^
  ( -clone 0 -level %CUTPC%%%,100%% ) ^
  ( -clone 0 -resize 2%%x2%% -resize "%WW%x%HH%^!" ) ^
  -delete 0 ^
  -compose Over -composite ^
  pseudo.jpg
snibgo's IM pages: im.snibgo.com
MathieuC
Posts: 6
Joined: 2013-06-05T02:56:34-07:00
Authentication code: 6789

Re: Make an Adaptive Histogram Equalization

Post by MathieuC »

Thank you for your help, the results is better than -equalize.
Post Reply