Adaptive Gaussian threshold¶
| Fact | Value |
|---|---|
| Also called | Local Gaussian threshold, Gaussian adaptive thresholding |
| Status | Implemented and tested |
| Family | Segmentation and regions |
| Engine | Browser |
| Type | Nonlinear, statistical, local mean threshold |
| Output | Mask |
| Operator shown as | algorithm (exact) |
| Support radius | floor(4 * (size - 1) / 6 + 0.5) |
| Code | web/src/core/filters/segmentation/thresholds.ts, adaptiveGaussianFilter |
| Operator code | web/src/core/filters/segmentation/thresholds.ts, adaptiveGaussianFilter |
| Channels | Brightness only, RGB |
Overview¶
The adaptive Gaussian threshold compares each pixel with a Gaussian weighted mean of its neighborhood, lowered by an offset. Near pixels count more than far ones, which follows the background more closely than a plain mean.
How it works¶
- Blur the image with a Gaussian of sigma (size - 1) / 6.
- Subtract the offset.
- Make the pixel white if it is brighter than the result.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(T(x, y)\): The local threshold
- \(s\): The window size
- \(C\): The offset
The threshold at each pixel is a Gaussian weighted mean with sigma = (s - 1) / 6, cut at 4 sigma, minus the offset C. This follows scikit-image's threshold_local. So a pixel turns white when it is brighter than its surroundings by more than C. Slow changes of lighting move the mean and the pixel together, so they do not change the result. The result is 255 where the value is above the threshold and 0 elsewhere, or the reverse with Invert. OpenCV's adaptiveThreshold is similar, but rounds the mean to whole levels and sizes its Gaussian differently.
The Gaussian is the same local mean with smooth weights. scikit-image derives its sigma from the block size as (size - 1) / 6. So the window holds about plus and minus 3 sigma. The kernel is cut at 4 sigma.
Kernel or operator¶
The Inspector shows the threshold rule as formulas with the current settings.
Algorithm¶
- Blur the window around each pixel with a Gaussian.
- Subtract the offset to get the local threshold.
- 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. |
Offset offset |
5 | -50 to 50 | The amount C subtracted from the local mean to get the threshold. |
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¶
adaptiveGaussianFilter in web/src/core/filters/segmentation/thresholds.ts blurs with gaussianMean, two 1D passes of the Gaussian.
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 "Adaptive Gaussian, 15 px, C = 5" is this filter.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels. The mean uses running sums or two 1D passes.
Artifacts and limitations¶
Limitations
- Flat areas larger than the window turn into noise unless the offset is large enough.
- Objects much larger than the window get hollow.
Artifacts
- Speckle in large flat areas.
- Thick strokes break into outlines.
Tests¶
The fixture cases come from scikit-image's threshold_local with the Gaussian method 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¶
- scikit-image documentation, skimage.filters. https://scikit-image.org/docs/stable/api/skimage.filters.html
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 10, thresholding.