Skip to content

Niblack threshold

Fact Value
Also called Niblack binarization, Local mean and deviation threshold
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, niblackFilter
Operator code web/src/core/filters/segmentation/thresholds.ts, niblackFilter
Channels Brightness only, RGB

Overview

Niblack's method sets the threshold of each pixel at the local mean plus k times the local standard deviation. The deviation lets the threshold follow the local contrast as well as the local brightness.

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 some speckle between them. 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 some speckle between them.
Niblack 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 + k s.
  3. Make the pixel white if it is brighter than the threshold.

Mathematical foundation

\[ T = m + k\, s, \quad I_{\text{out}} = \begin{cases} 255 & I > T \\ 0 & \text{otherwise} \end{cases}, \qquad k = -0.2 \]

The formula shows the default parameter values.

where

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

m and s are the mean and the population standard deviation of the window around the pixel. Niblack sets the threshold at m + k s, with k = -0.2 for dark text on a light page. The deviation adapts the threshold to the local contrast. scikit-image's threshold_niblack writes it as m - k s, so its k has the opposite sign. The result is 255 where the value is above the threshold and 0 elsewhere, or the reverse with Invert.

In a flat area the deviation is small, so the threshold sits close to the mean and noise decides each pixel. That is the speckle Niblack's method is known for. Running sums of the values and of their squares give the mean and the variance at the same cost for any window size.

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 + k s.
  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.2 -1 to 1 The weight k of the local 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

niblackFilter 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 "Niblack, 15 px, k = -0.2" 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

  • Turns noise in empty areas into speckle, since s is small there and T is close to m.

Artifacts

  • Heavy speckle in blank regions. Sauvola was designed to fix this.

Tests

The fixture cases come from scikit-image's threshold_niblack, with its k of the opposite sign on the random test plane, and compare every output pixel.

References