Skip to content

Bradley threshold

Fact Value
Also called Bradley-Roth threshold, Integral image threshold
Status Implemented and tested
Family Segmentation and regions
Engine Browser
Type Nonlinear, statistical, local mean ratio threshold
Output Mask
Operator shown as algorithm (exact)
Support radius floor(size / 2)
Code web/src/core/filters/segmentation/thresholds.ts, bradleyFilter
Operator code web/src/core/filters/segmentation/thresholds.ts, bradleyFilter
Channels Brightness only, RGB

Overview

Bradley and Roth compare each pixel with the mean of the window around it. A pixel turns black when it is at least t percent darker than that mean. An integral image gives every window sum in constant time, so the method is fast enough for live video.

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

How it works

  1. Find the mean of the window.
  2. Lower it by t percent.
  3. Make the pixel black if it is not brighter than the result, and white otherwise.

Mathematical foundation

\[ T = m\, \frac{100 - t}{100}, \quad I_{\text{out}} = \begin{cases} 255 & I > T \\ 0 & \text{otherwise} \end{cases}, \qquad s = 15,\ t = 15 \]

The formula shows the default parameter values.

where

  • \(m\): The mean of the s by s window
  • \(s\): The window size
  • \(t\): The percent below the mean at which pixels turn black

m is the mean of the s by s window around the pixel. A pixel turns black when it is at least t percent darker than m, and white otherwise. Bradley and Roth sum each window with an integral image, so the cost per pixel does not depend on s. PixelNexus uses running sums, which give the same sums. Their method improves on Wellner's, which compares each pixel with a moving average of the last s pixels of the scan. They cite Wellner's choices of s as one eighth of the image width and t = 15. Since T is a fixed fraction of the mean, making the whole image darker or brighter by a factor gives the same result. The result is 255 where the value is above the threshold and 0 elsewhere, or the reverse with Invert.

The threshold is a fixed fraction of the mean. If a shadow makes a region darker by a factor, the mean and the pixel change by the same factor, so the result stays the same. The adaptive mean threshold subtracts a fixed offset instead, which suits noise of a fixed size better than changes of lighting.

Kernel or operator

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

Algorithm

  1. Sum every s by s window with running sums, as an integral image does.
  2. Divide by the window area to get the mean m.
  3. Make the pixel black if it is at or below m (100 - t) / 100, and white otherwise.

Parameters

Parameter Default Values Meaning
Window size size 15 3 to 301, odd px The side s of the square window around each pixel.
Percent t percent 15 0 to 100 % How many percent darker than its window mean a pixel must be to turn black.
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

bradleyFilter in web/src/core/filters/segmentation/thresholds.ts sums the windows with windowSumOf, shared with the rank and 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 "Bradley, 15 px, t = 15" is this filter.

Complexity

  • Time: \(O(N)\)
  • Memory: \(O(N)\)

N is the number of pixels. The cost does not depend on the window size.

Artifacts and limitations

Limitations

  • Assumes most pixels are background. A dark object larger than the window turns white in its middle, as Bradley and Roth note.
  • In very dark areas the percent rule leaves only a small margin, so noise can turn black.

Artifacts

  • Hollow dark objects when the window is too small.
  • Speckle in dark, noisy areas.

Tests

The fixture cases come from a NumPy reference that follows the paper: an integral image of the plane padded with the border mode, and the comparison with the lowered mean. Before the cases are written, the reference with t = 0 is checked against the mean of scikit-image's threshold_local for all five border modes. Unit tests check that t = 0 thresholds at the local mean and that a darker copy of an image gives the same result. One more test puts a dark pixel on each side of the threshold.

References

  • Bradley, D. and Roth, G. Adaptive Thresholding using the Integral Image. Journal of Graphics Tools 12(2), 13 to 21, 2007. DOI 10.1080/2151237X.2007.10129236.
  • Wellner, P. D. Adaptive thresholding for the DigitalDesk. Technical report EPC-93-110, EuroPARC, 1993.