Otsu threshold¶
| Fact | Value |
|---|---|
| Also called | Otsu's method, Otsu binarization |
| Status | Implemented and tested |
| Family | Segmentation and regions |
| Engine | Browser |
| Type | Nonlinear, statistical, between-class variance maximization |
| Output | Mask |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | web/src/core/filters/segmentation/thresholds.ts, otsuFilter |
| Operator code | web/src/core/filters/segmentation/thresholds.ts, otsuThreshold |
| Channels | Brightness only, RGB |
Overview¶
Otsu's method picks the global threshold from the histogram. It tries every level and keeps the one that makes the dark and the bright groups as different as possible. It needs no parameters.
How it works¶
- Count the histogram of the levels.
- For each level, measure how far apart the two groups are.
- Threshold at the level where they are furthest apart.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(\omega_0, \omega_1\): The pixel counts at or below t, and above t
- \(\mu_0, \mu_1\): The mean levels of the two classes
- \(t^*\): The chosen threshold
Here omega_0 and mu_0 are the number and the mean of the pixels at or below level t. The values omega_1 and mu_1 are those of the pixels above it. Otsu showed that maximizing the between-class variance sigma_B^2 is the same as minimizing the variance within the two classes. Only the levels from the lowest to the highest one present are tried. Ties go to the smallest t, as in scikit-image and OpenCV. A flat image gets its own level as threshold and turns all 0. The result is 255 where the value is above the threshold and 0 elsewhere, or the reverse with Invert.
The total variance of the levels splits into a part within the two classes and a part between them. The total does not depend on t, so maximizing the between-class part minimizes the within-class part. Running sums of the counts and of the levels give each candidate in constant time.
Kernel or operator¶
The Inspector shows the threshold rule as formulas with the current settings.
Algorithm¶
- Count the histogram of the levels of the whole image.
- For each level t, compute the between-class variance from running sums.
- Keep the level with the largest variance and threshold the image there.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
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¶
otsuFilter in web/src/core/filters/segmentation/thresholds.ts picks the level with otsuThreshold. It is global, since the histogram covers the whole image.
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 "Otsu" is this filter.
Complexity¶
- Time: \(O(N + 256)\)
- Memory: \(O(1)\)
N is the number of pixels. Running sums make each candidate threshold cost the same.
Artifacts and limitations¶
Limitations
- Assumes two classes. Small objects on a large background can be missed.
- One threshold for the whole image, so uneven lighting still fails.
Artifacts
- Noise in flat regions when the histogram has only one clear peak.
Tests¶
The fixture cases come from scikit-image's threshold_otsu, which agrees with OpenCV's THRESH_OTSU on three test images 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¶
- Otsu, N. A threshold selection method from gray-level histograms. IEEE Transactions on Systems, Man, and Cybernetics 9(1), 62 to 66, 1979. DOI 10.1109/TSMC.1979.4310076.
- scikit-image documentation, skimage.filters. https://scikit-image.org/docs/stable/api/skimage.filters.html