Skip to content

Sauvola threshold

Fact Value
Also called Sauvola binarization
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, sauvolaFilter
Operator code web/src/core/filters/segmentation/thresholds.ts, sauvolaFilter
Channels Brightness only, RGB

Overview

Sauvola's method refines Niblack's for documents. Where the neighborhood is flat, the threshold drops well below the mean, so blank paper stays clean. Where there is text, it rises toward the mean.

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, with clean strokes and a clean background. 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, with clean strokes and a clean background.
Sauvola threshold on a page lit from the right.

How it works

  1. Find the mean and the standard deviation of the window.
  2. Set the threshold to m (1 + k (s / R - 1)).
  3. Make the pixel white if it is brighter than the threshold.

Mathematical foundation

\[ T = m \left(1 + k \left(\frac{s}{R} - 1\right)\right), \quad I_{\text{out}} = \begin{cases} 255 & I > T \\ 0 & \text{otherwise} \end{cases}, \qquad k = 0.5,\ R = 128 \]

The formula shows the default parameter values.

where

  • \(m, s\): The local mean and standard deviation
  • \(k\): The weight of the deviation term
  • \(R\): The dynamic range of the standard deviation

m and s are the mean and the population standard deviation of the window around the pixel. Sauvola and Pietikainen use k = 0.5 and R = 128 for 8-bit images. Where the window is flat, s is small and T falls to about m (1 - k), well below the mean, so noise stays black. Where there is text, s is large and T moves toward m. The same formula is scikit-image's threshold_sauvola. For dark text on a light page, use Invert so the text becomes white. The result is 255 where the value is above the threshold and 0 elsewhere, or the reverse with Invert.

With no contrast, s = 0 and the threshold is m (1 - k), half the mean for k = 0.5. Noise on blank paper cannot cross that. As s grows toward R, the threshold rises toward m. The figure shows dark strokes on light paper as black on white, without Invert.

Kernel or operator

The Inspector shows the threshold rule as formulas with the current settings.

Algorithm

  1. Compute the local mean and standard deviation with running sums of the values and their squares.
  2. Set the threshold to m (1 + k (s / R - 1)).
  3. 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.5 0 to 1 How strongly the local deviation lowers the threshold.
R r 128 1 to 255 The dynamic range R of the 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

sauvolaFilter 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 "Sauvola, 15 px, k = 0.5, R = 128" 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

  • Low-contrast text on a dark background can vanish, since the threshold drops with the mean.

Artifacts

  • Thin strokes can break where the contrast is weak.

Tests

The fixture cases come from scikit-image's threshold_sauvola on the random test plane, and compare every output pixel.

References