CLAHE¶
| Fact | Value |
|---|---|
| Also called | Contrast limited adaptive histogram equalization, Local histogram equalization |
| Status | Implemented and tested |
| Family | Enhancement, illumination and color |
| Engine | Browser |
| Type | Nonlinear, point, tiled histogram equalization with clipping |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | web/src/core/filters/enhancement/clahe.ts, claheFilter |
| Operator code | web/src/core/filters/enhancement/clahe.ts, tileCurves |
| Channels | Brightness only, RGB |
Overview¶
CLAHE, contrast limited adaptive histogram equalization, equalizes each tile of a grid on its own. A clip limit caps how much any tile may raise the contrast, so flat areas and noise are not stretched too far. The tile curves are blended smoothly, so no tile edges show. It is the usual choice for local contrast in medical images and in photos with deep shadows.
How it works¶
- Cut the image into a grid of tiles.
- Count the histogram of each tile.
- Clip the tall bins at the limit and spread the clipped pixels over all bins.
- Turn each histogram into an equalization curve.
- For each pixel, blend the curves of the four nearest tile centers.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(h^{(t)}_k\): The number of pixels of tile t at level k
- \(M\): The number of pixels in one tile
- \(c, \beta\): The clip limit and the largest count a bin may keep
- \(E^{(t)}\): The number of clipped pixels of tile t
- \(T^{(t)}\): The curve of tile t
- \(w_t(x, y)\): The bilinear weight of tile t at the pixel
h^(t) is the histogram of tile t and M the number of pixels in a tile. No bin may hold more than beta pixels. The E^(t) pixels above the limit are spread over all 256 bins, the same share to each. The remainder r goes out one pixel at a time from bin 0, with a step of max(1, floor(256 / remainder)). The tile curve T^(t) is the scaled running total, rounded to whole levels. The slope of a tile curve at level k is 255 / M times the count of that bin, so the limit caps the contrast gain at about c. Each pixel blends the curves of the four nearest tile centers with bilinear weights w_t. Tile centers sit at the middle of each tile. Outside the outermost centers, the nearest curves are used. These are the steps of cv::CLAHE in OpenCV, which follow Zuiderveld. Values between two levels take each curve interpolated between them.
The slope of an equalization curve at a level is proportional to the number of pixels at that level. A flat tile has one tall bin, so its curve is very steep there and small differences such as noise become large. Clipping the histogram at beta caps the slope. With the clip limit c of OpenCV, beta is about c times the average bin count, so the slope stays near c or below. Pizer and colleagues and Zuiderveld describe this contrast limit and the bilinear blend between tile centers.
Kernel or operator¶
The Inspector shows the stages as formulas: the tile grid, the tile histograms, the tile curves and the bilinear blend. The curves depend on the image. The Point-op curve in the Learn panel draws the blended curve at the probe pixel, and the histograms of the whole image before and after.
Algorithm¶
- Extend the image at the right and bottom so that its size is a multiple of the grid.
- Count the histogram of the levels in each tile.
- Clip each histogram at beta and spread the clipped pixels over all bins.
- Turn each tile histogram into a curve, 255 / M times its running total, rounded.
- For each pixel, blend the curves of the four nearest tile centers bilinearly and apply the blend to the pixel's value.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Tiles across tilesX |
8 | 1 to 64 | How many tiles the grid has from left to right. Never more than the image width. |
Tiles down tilesY |
8 | 1 to 64 | How many tiles the grid has from top to bottom. Never more than the image height. |
Clip limit clipLimit |
2 | 0.1 to 40 | The clip limit c of OpenCV. No histogram bin of a tile may hold more than c times the average bin count. |
Channels channelScope |
Brightness only | RGB, Brightness only | Which part of the color image the filter changes. |
Implementation¶
claheFilter in web/src/core/filters/enhancement/clahe.ts. The function tileCurves builds the curve of every tile, and applyCurves blends them per pixel. The function curveAt gives the blended curve at one pixel for the Point-op curve. The filter is global, since the tile grid covers the whole image.
Worked example¶
| Clip limit c | 1 | 2 | 4 | 40 | none (AHE) |
|---|---|---|---|---|---|
| beta | 1 | 3 | 6 | 62 | none |
| Steepest step | 2 | 4 | 6 | 41 | 78 |
The image of the figure is 160 by 160 pixels, so 8 x 8 tiles hold M = 400 pixels each. beta is the largest count a bin may keep. The steepest step is the largest jump between neighboring levels in any tile curve, in output levels. A jump of 1 keeps contrast as it is. Without a limit, a crowded level can jump by dozens of levels.
Complexity¶
- Time: \(O(N + 256 T)\)
- Memory: \(O(256 T)\)
N is the number of pixels and T the number of tiles. Each pixel blends four table lookups.
Artifacts and limitations¶
Limitations
- Can look unnatural on photos, with halos at the borders of large bright or dark regions.
- The tile size must suit the size of the detail.
Artifacts
- Halos and dark rims near large bright objects.
- Some noise is still raised in flat areas at high clip limits.
Tests¶
The reference follows the steps of cv::CLAHE with NumPy. Before the cases are written, a version that blends in 32-bit floats, as OpenCV does, must equal cv2.createCLAHE exactly on a test image, for four grids and clip limits. PixelNexus blends in 64-bit floats, which moves a rounded result by at most one level, on under 1 percent of the pixels in that test. More tests check that clipping keeps the pixel count, that one tile without a limit is rounded global equalization, and that each pixel gets the curve that curveAt reports.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/enhancement/enhancement.test.ts
References¶
- Zuiderveld, K. Contrast Limited Adaptive Histogram Equalization. In Heckbert, P. S. (editor), Graphics Gems IV, 474 to 485. Academic Press, 1994. DOI 10.1016/B978-0-12-336156-1.50061-6.
- Pizer, S. M., Amburn, E. P., Austin, J. D., Cromartie, R., Geselowitz, A., Greer, T., ter Haar Romeny, B., Zimmerman, J. B. and Zuiderveld, K. Adaptive histogram equalization and its variations. Computer Vision, Graphics, and Image Processing 39(3), 355 to 368, 1987. DOI 10.1016/S0734-189X(87)80186-X.
- OpenCV documentation, cv::CLAHE and cv::createCLAHE. https://docs.opencv.org/4.x/d6/db6/classcv_1_1CLAHE.html