Adaptive histogram equalization¶
| Fact | Value |
|---|---|
| Also called | AHE, Tiled histogram equalization |
| Status | Implemented and tested |
| Family | Enhancement, illumination and color |
| Engine | Browser |
| Type | Nonlinear, point, tiled histogram equalization |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | web/src/core/filters/enhancement/clahe.ts, aheFilter |
| Operator code | web/src/core/filters/enhancement/clahe.ts, tileCurves |
| Channels | Brightness only, RGB |
Overview¶
Adaptive histogram equalization (AHE) equalizes each tile of a grid on its own, so every region uses the full range of levels. It brings out the faintest local detail, and it also turns noise in flat areas into strong grain. CLAHE adds a clip limit to prevent that. This filter is CLAHE without the limit.
How it works¶
- Cut the image into a grid of tiles.
- Build the histogram equalization curve of each tile.
- 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
- \(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. Each tile gets the histogram equalization curve of its own pixels, rounded to whole levels. Each pixel blends the curves of the four nearest tile centers with bilinear weights w_t, as Pizer and colleagues describe for interpolated AHE. This is CLAHE without a clip limit, and it uses the same code. A flat tile has one tall bin, so its curve jumps steeply at that level. That stretches small differences, such as noise, to large ones.
Each tile curve is the scaled cumulative histogram of the tile. Pizer and colleagues compute it only at the tile centers and blend the curves bilinearly in between, which costs far less than one histogram per pixel. In a flat tile, nearly all pixels share a few levels, so the curve spreads those few levels over the whole range. That is why AHE raises noise in flat areas.
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.
- 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. |
Channels channelScope |
Brightness only | RGB, Brightness only | Which part of the color image the filter changes. |
Implementation¶
aheFilter in web/src/core/filters/enhancement/clahe.ts is the CLAHE code without a clip limit. 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¶
| Image | Input | Histogram equalization | CLAHE, clip 2 | AHE |
|---|---|---|---|---|
| Standard deviation | 1.49 | 52.16 | 4.22 | 72.06 |
The standard deviation of a flat patch of background, 30 by 20 pixels, in the image of the figure. Before the filters it is only noise. Global equalization stretches it a lot, since most pixels of the image are background. AHE stretches it the most, since the tiles in flat areas have one tall histogram bin and so a very steep curve. The clip limit of CLAHE caps that slope.
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
- Raises noise strongly in flat areas.
- The tile size must suit the size of the detail.
Artifacts
- Grainy, noisy flat areas, such as sky or background.
- Halos near large bright or dark objects.
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¶
- 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.
- 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.