Contrast Limited Adaptive Histogram Equalization (CLAHE)

Announcements pertaining to ImageMagick, or ImageMagick related software. This list is moderated. No discussions here, instead post to the users group instead.
Post Reply
User avatar
fmw42
Posts: 25664
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Contrast Limited Adaptive Histogram Equalization (CLAHE)

Post by fmw42 » 2019-01-05T15:41:18-07:00

As of IM 7.0.8.24 (currently in beta as of this post), Imagemagick has Contrast Limited Adaptive Histogram Equalization as -CLAHE. Some previous versions (back to 7.0.8.15) had -clahe available, but the functionality was still in development.

The documentation is as follows:

-clahe widthxheight{%}{+}number-bins{+}clip-limit{!}

contrast limited adaptive histogram equalization. The image is divided into tiles of width and height pixels. Add % to use the percentage of the image's width and height rather than number of pixels for the widthxheight argument.The tile size should be larger than the size of features to be preserved and respects the aspect ratio of the image. Add ! to force an exact tile width and height. number-bins is the number of histogram bins per tile (min 2, max 65536). The number of histogram bins should be smaller than the number of pixels in a single tile. clip-limit is the contrast limit for localised changes in contrast. A clip-limit of 2 to 3 is a good starting place (e.g. -clahe 50x50%+128+3). Very large values will let the histogram equalization do whatever it wants to do, that is result in maximal local contrast. The value 1 will result in the original image. Note if number of bins and the clip-limit are ommitted, they default to 128 and no clipping respectively.

A good reference is https://en.wikipedia.org/wiki/Adaptive_ ... tion#CLAHE

Here are a few examples.

Input Image (440x320 pixels) (source: https://docs.opencv.org/3.1.0/d5/daf/tu ... ation.html)
Image

Global histogram equalization:

Code: Select all

magick office.png -equalize office_equalize.png
Image


Contrast Limited Adaptive Histogram Equalization:

1) Variations in Width and Height where image dimensions are exactly divisible by the tile dimensions.

Code: Select all

magick office.png -clahe 220x160+128+3! office_220x160+128+3_exact.png
Image

Code: Select all

magick office.png -clahe 110x80+128+3! office_110x80+128+3_exact.png
Image

Code: Select all

magick office.png -clahe 55x40+128+3! office_55x40+128+3_exact.png
Image

Animation:
Image


2) The same image is generated using tile aspect sizes (default) and exact tile sizes (using !) by these commands since the 440x160 is evenly divisible by 220x160 (each is a factor of 2). The 220x220 in aspect mode, converts the tile size to 220x160 in order to preserve aspect ratio of the tile relative to the image's aspect ratio

Code: Select all

magick office.png -clahe 220x160+128+3! office_220x160+128+3_exact.png
magick office.png -clahe 220x160+128+3 office_220x160+128+3_aspect.png
magick office.png -clahe 220x220+128+3 office_220x220+128+3_aspect.png
Image

3) No variation in virtual-pixel when the tiles completely fill the image with no excess. That is the image is exactly divisible by the tile dimensions. So all images should look identical. 44x32 is 1/10 the size of the image at 440x320.

Code: Select all

magick office.png -virtual-pixel edge -clahe 44x32+128+3! office_44x32+128+3_exact_edge.png
magick office.png -virtual-pixel mirror -clahe 44x32+128+3! office_44x32+128+3_exact_mirror.png
magick office.png -virtual-pixel black -clahe 44x32+128+3! office_44x32+128+3_exact_black.png
magick office.png -virtual-pixel white -clahe 44x32+128+3! office_44x32+128+3_exact_white.png
Image

4) Variation in virtual-pixel method is visible when the image dimensions not exactly divisible by the tile dimensions. The excess pixels plus one full tile around the boundary of the image will be affected. Here the tiles are 42x30 which divides into the image dimensions of 440x320 leaving an excess partial tile of 20 pixels on each side. So the outer 62 pixels left and right and the outer 50 pixels top and bottom will show differences. This is most visible on the right side of the results.

Code: Select all

magick office.png -virtual-pixel edge -clahe 42x30+128+3! office_42x30+128+3_exact_edge.png
magick office.png -virtual-pixel mirror -clahe 42x30+128+3! office_42x30+128+3_exact_mirror.png
magick office.png -virtual-pixel black -clahe 42x30+128+3! office_42x30+128+3_exact_black.png
magick office.png -virtual-pixel white -clahe 42x30+128+3! office_42x30+128+3_exact_white.png
Image


5) Color Image: Equalize vs CLAHE (source: https://imagej.net/Enhance_Local_Contrast_(CLAHE))

Code: Select all

magick Photo1b.jpg -equalize Photo1b_equalize.jpg
magick Photo1b.jpg -clahe 387x174+128+3! Photo1b_387x174+128+3_exact.jpg
magick Photo1b.jpg -clahe 258x116+128+3! Photo1b_258x116+128+3_exact.jpg
magick Photo1b.jpg -clahe 129x58+128+3! Photo1b_129x58+128+3_exact.jpg
Image

Post Reply