Skip to content

Adaptive mean threshold

Fact Value
Also called Local mean threshold, 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(size / 2)
Code web/src/core/filters/segmentation/thresholds.ts, adaptiveMeanFilter
Operator code web/src/core/filters/segmentation/thresholds.ts, adaptiveMeanFilter
Channels Brightness only, RGB

Overview

The adaptive mean threshold compares each pixel with the mean of its own neighborhood, lowered by an offset. Slow changes of lighting move the mean along with the pixel, so they do not change the result.

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 across the whole page. 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 across the whole page.
Adaptive mean threshold on a page lit from the right.

How it works

  1. Average the window around each pixel.
  2. Subtract the offset.
  3. Make the pixel white if it is brighter than the result.

Mathematical foundation

\[ T(x, y) = \operatorname{mean}_{s \times s}(I) - C, \quad I_{\text{out}} = \begin{cases} 255 & I > T \\ 0 & \text{otherwise} \end{cases}, \qquad s = 15,\ C = 5 \]

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 the mean of the s by s window around the pixel, 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.

A pixel turns white when it is brighter than its surroundings by more than the offset. So the threshold follows the local background. The window must be larger than the strokes, or the strokes pull the mean down and break up.

Kernel or operator

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

Algorithm

  1. Average the window around each pixel.
  2. Subtract the offset to get the local threshold.
  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.
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

adaptiveMeanFilter in web/src/core/filters/segmentation/thresholds.ts uses the running box mean boxMean.

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 mean, 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 mean method on the random test plane, and compare every output pixel.

References