The localized thresholding algorithm attributed to Wayne Niblack appeared in his 1985 book on image processing [1]. The threshold is varied across the image as a function of the local intensity average and standard deviation.
Algorithm
The threshold value at each pixel is calculated as the sum of the mean and the standard deviation (times a constant, k) of the neighbourhood surrounding the pixel. It is based on applying the following formula to each pixel centred on a n×n
neighbourhood surrounding the pixel to derive a local threshold.
t = mN + (k × stdN)
Parameters:
- mN = mean of the neighbourhood
- stdN = standard deviation of the neighbourhood
- k = constant value (default = -0.2)
The default values are based on the experiments performed by Niblack [1]. The value of w
used depends on the content of the particular image being binarized, i.e. chosen empirically.
Implementation (in Julia)
Here is the algorithm implement in Julia. Note that because this algorithm processes the neighbourhood surrounding a pixel, the image will need to be padded before the algorithm is applied (preferably with zeros) to allow edge pixels to be processed. Works on 8-bit grayscale images.
function niblackTH(img, n=5, k=-0.2)
dx,dy = size(img)
imgSize = dx * dy
imgN = copy(img)
# Calculate the radius of the neighbourhood
w = div((n-1),2)
# Process the image
for i = w+1:dx-w, j = w+1:dy-w
# Extract the neighbourhood area
block = img[i-w:i+w, j-w:j+w]
# Calculate the mean and standard deviation of the neighbourhood region
wBmn = mean(block)
wBstd = std(block)
# Calculate the threshold value
wBTH = (wBmn + k * wBstd)
# Threshold the pixel
if (img[i,j] < wBTH)
imgN[i,j] = 0
else
imgN[i,j] = 255
end
end
return imgN
end
Example Use
imgB = niblackTH(img,15) |
Testing
The algorithm is illustrated using the two images of text shown below. For both algorithms, the default values are used, and the size of the neighbourhood is n=15
.


The results are not good. In Example 1 the background produces a lot of noise, which is largely because of discontinuities in the grayscale values, even though it looks quite smooth. A similar situation occurs in Example 2, with the thresholding clearly indicating the background texture attributable to the


Caveats
The algorithm tends to produce a lot of noise, particularly when the image background has a light texture.
REFS:
- Niblack, W., An Introduction to Digital Image Processing, Prentice Hall pp.115-116 (1986)