Skip to content

Wolf threshold

Fact Value
Also called Wolf-Jolion binarization, Wolf binarization
Status Implemented and tested
Family Segmentation and regions
Engine Browser
Type Nonlinear, statistical, normalized local mean and deviation threshold
Output Mask
Operator shown as algorithm (exact)
Support radius global
Code web/src/core/filters/segmentation/thresholds.ts, wolfFilter
Operator code web/src/core/filters/segmentation/thresholds.ts, wolfThresholds
Channels Brightness only, RGB

Overview

Wolf, Jolion and Chassaing changed Sauvola's rule so that it fits images with low contrast, such as text in video frames. The threshold is measured from the darkest value of the image, and the range of contrast comes from the image itself.

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.
Wolf threshold on a page lit from the right.

How it works

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

Mathematical foundation

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

The formula shows the default parameter values.

where

  • \(m, s\): The local mean and standard deviation
  • \(M\): The smallest value of the image
  • \(R\): The largest local standard deviation in the image
  • \(k\): The weight of the contrast term

m and s are the mean and the population standard deviation of the window around the pixel. M is the smallest value of the image, and R is the largest s over the image. The same rule can be written T = m - k (1 - s / R) (m - M). Sauvola's rule is T = m - k (1 - s / R) m with a fixed R. So Wolf's method measures from the darkest value M instead of 0, and takes R from the image. Where the window has the highest contrast in the image, s = R and T = m. Where it is flat, T falls from m toward M by the fraction k. This is the rule of Wolf's own program, which uses k = 0.5 by default, and of OpenCV's niBlackThreshold with BINARIZATION_WOLF. If the whole image is flat, R = 0. Then s / R counts as 0 and T equals the value, so every pixel turns 0. The result is 255 where the value is above the threshold and 0 elsewhere, or the reverse with Invert.

Written as T = m - k (1 - s / R) (m - M), the rule shows how it relates to Sauvola's, T = m - k (1 - s / R) m. Sauvola measures from 0 with a fixed range R. Wolf measures from the darkest value M and takes R from the image. In the busiest window of the image, s = R and the threshold is the mean. In a flat window, the threshold moves from the mean toward M by the fraction k.

M and R depend on the whole image, so this is a global filter. It runs on the whole image even when a selection is active, and a selection only limits where the result is blended in.

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. Find the smallest value M of the image and the largest standard deviation R.
  3. Set the threshold to (1 - k) m + k M + k (s / R) (m - M).
  4. 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 far the threshold moves from the local mean toward the darkest value where the contrast is low.
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

wolfFilter in web/src/core/filters/segmentation/thresholds.ts calls wolfThresholds, which 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 "Wolf, 15 px, k = 0.5" 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

  • One very dark pixel or one very busy window changes M or R, and so the threshold everywhere.
  • Wolf's own program copies the threshold of the nearest full window at the image edges and finds R over full windows only. PixelNexus fills windows with the border mode, as OpenCV does with replicate, so results near the edges can differ.

Artifacts

  • Thin strokes can break where the contrast is weak.

Tests

The fixture cases come from a NumPy reference that sums windows with an integral image of the padded plane. Before the cases are written, the reference is checked against OpenCV's niBlackThreshold with BINARIZATION_WOLF and the replicate border on two images. Unit tests check that k = 0 thresholds at the local mean, that k = 1 gives M + (s / R) (m - M), and that a flat image turns all 0.

References