v7.1.2-34 Latest Stable Release

Contrast Limited Adaptive Histogram Equalization

ImageMagick supports contrast limited adaptive histogram equalization (CLAHE) to improve the contrast in images.

quickstart.sh
# Install via your favorite Package Manager
$ brew install imagemagick
# Convert PNG to WebP with compression
$ magick input.png -quality 85 output.webp
# Resize image maintaining aspect ratio
$ magick input.jpg -resize 800x600^ output.jpg

Use contrast limited adaptive histogram equalization (CLAHE) to improve contrast in images. Ordinary histogram equalization computes a global equalization, whereas an adaptive method computes several histograms—each corresponding to a distinct section of the image—to redistribute lightness values. This is ideal for enhancing local contrast and edge definitions. CLAHE introduces a variant that limits AHE's tendency to overamplify noise in homogeneous regions.

Syntax and Options

CLAHE is supported as of ImageMagick 7.0.8-24 via the -clahe option:

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

The image is divided into tiles of specified width and height pixels. Append % to define dimensions as percentages of the image size, or add ! to force exact tile dimensions. Tile size should exceed the features you want to preserve while respecting the image's aspect ratio.

number-bins specifies histogram bins per tile (ranging from 2 to 65536) and should be smaller than the number of pixels in a single tile. clip-limit controls the localized contrast limit, with values between 2 and 3 serving as an effective starting point (e.g., -clahe 50x50%+128+3). Very large values maximize local contrast, while a value of 1 yields the original image. Omitting bins and clip-limit defaults them to 128 and no clipping, respectively.

Visual Comparison

To visualize the benefits of CLAHE, examine this low-contrast image of mountains:

Original Mountains

Original Image

Global Histogram Equalization

Global Equalization

CLAHE Enhanced Mountains

CLAHE Enhanced

The enhanced image on the right was produced with the following command:

$ magick mountains.jpg -clahe 25x25%+128+3 mountains-clahe.jpg

The 25x25% argument divides the 1500x1000 image into 16 tiles measuring exactly 375x250 pixels each. You can also specify tile dimensions directly rather than using percentages.

Virtual Pixels & Tile Alignment

For tile sizes that do not divide evenly into the image dimensions, CLAHE relies on virtual pixels, meaning results can vary slightly depending on your chosen virtual pixel method. For example:

$ magick mountains.jpg -virtual-pixel mirror -clahe 300x300+128+3! mountains-clahe.jpg

Notice the exclamation point (!): it forces an exact tile size of 300x300 pixels, whereas omitting it preserves the image's aspect ratio and results in dimensions of 300x200.