Niblack threshold¶
| Fact | Value |
|---|---|
| Also called | Niblack binarization, Local mean and deviation threshold |
| Status | Implemented and tested |
| Family | Segmentation and regions |
| Engine | Browser |
| Type | Nonlinear, statistical, local mean and deviation threshold |
| Output | Mask |
| Operator shown as | algorithm (exact) |
| Support radius | floor(size / 2) |
| Code | web/src/core/filters/segmentation/thresholds.ts, niblackFilter |
| Operator code | web/src/core/filters/segmentation/thresholds.ts, niblackFilter |
| Channels | Brightness only, RGB |
Overview¶
Niblack's method sets the threshold of each pixel at the local mean plus k times the local standard deviation. The deviation lets the threshold follow the local contrast as well as the local brightness.
How it works¶
- Find the mean and the standard deviation of the window.
- Set the threshold to m + k s.
- Make the pixel white if it is brighter than the threshold.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(m, s\): The local mean and standard deviation
- \(k\): The weight of the deviation
m and s are the mean and the population standard deviation of the window around the pixel. Niblack sets the threshold at m + k s, with k = -0.2 for dark text on a light page. The deviation adapts the threshold to the local contrast. scikit-image's threshold_niblack writes it as m - k s, so its k has the opposite sign. The result is 255 where the value is above the threshold and 0 elsewhere, or the reverse with Invert.
In a flat area the deviation is small, so the threshold sits close to the mean and noise decides each pixel. That is the speckle Niblack's method is known for. Running sums of the values and of their squares give the mean and the variance at the same cost for any window size.
Kernel or operator¶
The Inspector shows the threshold rule as formulas with the current settings.
Algorithm¶
- Compute the local mean and standard deviation with running sums of the values and their squares.
- Set the threshold to m + k s.
- Compare each pixel with its own threshold.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Window size size |
15 | 3 to 151, odd px | The side of the square window around each pixel. |
k k |
-0.2 | -1 to 1 | The weight k of the local standard deviation. |
Invert invert |
off | On or off | Make the pixels at or below the threshold white instead. |
Border mode border |
Reflect 101 (default) | Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap | How pixels outside the image are filled when the window reaches past the edge. |
Output mapping outputMapping |
Clip to 0 to 255 | Absolute value, Offset by 128, Stretch to full range, Clip to 0 to 255 | How signed results, such as gradients, become 8-bit pixel values. |
Channels channelScope |
Brightness only | RGB, Brightness only | Which part of the color image the filter changes. |
Implementation¶
niblackFilter in web/src/core/filters/segmentation/thresholds.ts uses localMoments, shared with the local statistics filters.
Worked example¶
| Method | Strokes black (%) | Background white (%) |
|---|---|---|
| Global, t = 128 | 100 | 57.2 |
| Otsu | 100 | 55.2 |
| Adaptive mean, 15 px, C = 5 | 100 | 99.8 |
| Adaptive Gaussian, 15 px, C = 5 | 100 | 99.6 |
| Niblack, 15 px, k = -0.2 | 100 | 96.4 |
| Sauvola, 15 px, k = 0.5, R = 128 | 97.5 | 100 |
| Wolf, 15 px, k = 0.5 | 100 | 100 |
| Bradley, 15 px, t = 15 | 100 | 100 |
All eight methods with their default settings on the page of the figure, 160 by 120 pixels. The background brightness rises from 60 at the left to 220 at the right, and the strokes are 45 percent of it. A stroke pixel is right when it is black, and a background pixel is right when it is white. The row "Niblack, 15 px, k = -0.2" is this filter.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels. Running sums make the cost independent of the window size.
Artifacts and limitations¶
Limitations
- Turns noise in empty areas into speckle, since s is small there and T is close to m.
Artifacts
- Heavy speckle in blank regions. Sauvola was designed to fix this.
Tests¶
The fixture cases come from scikit-image's threshold_niblack, with its k of the opposite sign on the random test plane, and compare every output pixel.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/segmentation/segmentation.test.ts
References¶
- Niblack, W. An Introduction to Digital Image Processing. Prentice Hall, 1986.
- scikit-image documentation, skimage.filters. https://scikit-image.org/docs/stable/api/skimage.filters.html