Skip to content

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.

Two panels: a gray test page with rows of dark strokes, dark at the left and bright at the right, and the black and white result, where Otsu's level also turns the dark left half black. Two panels: a gray test page with rows of dark strokes, dark at the left and bright at the right, and the black and white result, where Otsu's level also turns the dark left half black.
Otsu threshold on a page lit from the right.

How it works

  1. Count the histogram of the levels.
  2. For each level, measure how far apart the two groups are.
  3. Threshold at the level where they are furthest apart.

Mathematical foundation

\[ \sigma_B^2(t) = \omega_0(t)\, \omega_1(t)\, \left(\mu_0(t) - \mu_1(t)\right)^2, \quad t^* = \arg\max_t \sigma_B^2(t), \quad I_{\text{out}} = \begin{cases} 255 & I > t^* \\ 0 & \text{otherwise} \end{cases} \]

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

  1. Count the histogram of the levels of the whole image.
  2. For each level t, compute the between-class variance from running sums.
  3. 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.

References