Histogram equalization¶
| Fact | Value |
|---|---|
| Also called | Global histogram equalization, Equalize |
| Status | Implemented and tested |
| Family | Enhancement, illumination and color |
| Engine | Browser |
| Type | Nonlinear, point, cumulative histogram transfer |
| Output | Image |
| Operator shown as | point-transfer (exact) |
| Support radius | 0 |
| Code | web/src/core/filters/enhancement/histogram.ts, histogramEqFilter |
| Operator code | web/src/core/filters/enhancement/histogram.ts, equalizeTable |
| Channels | Brightness only, RGB |
Overview¶
Histogram equalization remaps brightness so that each level is used about equally often. Where many pixels share a narrow range of values, that range spreads out, and the contrast there goes up. It needs no parameters.
How it works¶
- Count how many pixels have each level.
- Add the counts up from level 0, the cumulative histogram.
- Divide by the number of pixels and multiply by 255.
- Use the result as the new value for each level.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(h_j\): The number of pixels at level j
- \(N\): The number of pixels
- \(T\): The transfer curve
h_j is the number of pixels at level j and N the number of pixels. T is the cumulative histogram scaled to 0 to 255, the histogram equalization of Gonzalez and Woods. A level shared by many pixels gets a big step up the curve, so crowded ranges spread out. The values stay unrounded until the end. The darkest level that occurs maps to 255 times its share of the pixels, not always to 0. OpenCV's equalizeHist shifts the curve so that it does. Values between two levels take the output interpolated between them.
If the values were continuous, the cumulative distribution function would map them to a uniform distribution. With 8-bit levels, all pixels at one level must move together, so the output histogram is only roughly flat. A level held by many pixels gets a big step up the curve. Levels that no pixel uses get no step at all.
Kernel or operator¶
The curve depends on the image, so the Inspector shows its formula. The Point-op curve in the Learn panel draws the curve for the open image, with the histograms before and after.
Algorithm¶
- Count the histogram of the levels of the whole image.
- Add it up from level 0 to get the cumulative histogram.
- Scale it to 0 to 255 and apply it to every pixel.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Channels channelScope |
Brightness only | RGB, Brightness only | Which part of the color image the filter changes. |
Implementation¶
histogramEqFilter in web/src/core/filters/enhancement/histogram.ts builds the curve with equalizeTable. histogram256 counts the levels of the whole plane, and applyTable applies the curve, interpolating between levels. The filter is global, so it always sees the whole image.
Worked example¶
| Level k | 50 | 100 | 150 | 200 |
|---|---|---|---|---|
| Pixels h_k | 4 | 2 | 1 | 1 |
| Running total | 4 | 6 | 7 | 8 |
| T(k) | 127.50 | 191.25 | 223.13 | 255 |
An image of 8 pixels. Half of them sit at level 50, so the curve jumps to 255 times 4 / 8 = 127.5 there. Each level after that adds its share of the pixels. The most crowded level gets the biggest step.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(1)\)
N is the number of pixels. One pass counts the histogram, a second applies the 256-entry curve.
Artifacts and limitations¶
Limitations
- One curve for the whole image, so it can overdo large flat areas. CLAHE works per region.
- Raises noise in flat areas along with detail.
Artifacts
- Harsh, unnatural contrast and amplified noise in smooth areas.
- Gaps in the output histogram, since 8-bit levels cannot be split.
Tests¶
The reference builds the cumulative histogram of the test plane with NumPy and applies it.
References¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 3.
- OpenCV documentation, cv::equalizeHist. https://docs.opencv.org/4.x/d6/dc7/group__imgproc__hist.html