Global threshold¶
| Fact | Value |
|---|---|
| Also called | Binary threshold, Fixed threshold |
| Status | Implemented and tested |
| Family | Segmentation and regions |
| Engine | Browser |
| Type | Nonlinear, statistical, fixed threshold |
| Output | Mask |
| Operator shown as | algorithm (exact) |
| Support radius | 0 |
| Code | web/src/core/filters/segmentation/thresholds.ts, thresholdGlobalFilter |
| Operator code | web/src/core/filters/segmentation/thresholds.ts, thresholdGlobalFilter |
| Channels | Brightness only, RGB |
Overview¶
A global threshold turns the image black and white with one level for every pixel. Pixels above the level turn white, the others black. It is the simplest segmentation, and it works only when the lighting is even.
How it works¶
- Compare each value with the threshold.
- Write 255 above it and 0 at or below it.
- With Invert, swap the two.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(I\): The input value from 0 to 255
- \(t\): The threshold
Every pixel is compared with the same level t. The result is 255 where the value is above the threshold and 0 elsewhere, or the reverse with Invert. This is the basic global thresholding of Gonzalez and Woods. It works when objects and background have clearly different levels everywhere in the image.
A threshold splits the histogram in two. When objects and background form two separate peaks, any level between them separates them. Uneven lighting shifts the peaks from place to place, so no single level works everywhere, as the figure shows.
Kernel or operator¶
The Inspector shows the threshold rule as formulas with the current settings.
Algorithm¶
- Compare each value with the threshold.
- Write 255 above it and 0 at or below it, or the reverse with Invert.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Threshold threshold |
128 | 0 to 255 | The level t. Values above it become white. |
Invert invert |
off | On or off | Make the pixels at or below the threshold white instead. |
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¶
thresholdGlobalFilter in web/src/core/filters/segmentation/thresholds.ts.
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 "Global, t = 128" is this filter.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(1)\)
N is the number of pixels.
Artifacts and limitations¶
Limitations
- One level for the whole image fails under uneven lighting.
- The threshold must be chosen by hand. Otsu chooses it from the histogram.
Artifacts
- Broken or merged objects where the lighting changes across the image.
Tests¶
The fixture cases come from a NumPy comparison 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¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 10, thresholding.