File Exchange

image thumbnail

Niblack local thresholding

version (256 KB) by Jan Motl
A traditional local image thresholding.


Updated 10 May 2013

View Version History

View License

This is an implementation of a traditional Niblack local image thresholding with an integral image method, which guarantees constant computation time regardless of the neighborhood size.

The method can be described like:
if pixel > (mean + k * standard_deviation)
pixel = object;
pixel = background;

Cite As

Jan Motl (2020). Niblack local thresholding (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (10)

azima ariff

Jan Motl

@JorgeRivé, the integral image method is used only in `averagefilter`. The integral image is calculated with:
% Matrix 't' is the sum of numbers on the left and above the current cell.
t = cumsum(cumsum(imageD),2);
The principle of the method is nicely illustrated at:
in "Algorithms" section (Back in time, I was using an older version of MATLAB that did not include `integralimage`. Hence, I implemented it).

Jorge Rivé

@JanMotl, I'm trying to understand the concept of the integral image method. It seems to me the image must be converted to an integral image first --but I don't see where that is done. I assumed it is done by the averagefilter function, but I can't tell for sure. can you speak to that a little? Thank you.

Jan Motl

Only 2-dimensional matrices in uint8 are supported. If you load a jpg file with IMREAD command then you get a 3-dimensional uint8 matrix (even if the image is grayscale). Hence you have to first perform conversion into a grayscale image, for example with RGB2GRAY.

The value of the weight 'k' is used to control and adjust the effect of standard deviation due to objects features. Niblack suggests the value of 'k' to be -0.2. However, the algorithm is sensitive to the 'k' value and the optimal value varies from image to image. Common values are between -0.2 and -0.1 but it makes sense to try values from -0.3 to 0.3. Nevertheless, an equation to estimate the optimal 'k' based on the image properties is given in:

If you don't get satisfying results with this method, try Sauvola local thresholding function (also published at File Exchange), which improves this method.

Dridi Maroua

Ho to define k?

Dridi Maroua

Hello,thank you for this code.But, It generaes an error for me :The input image must be a two-dimensional array.

Nat anunta


Thank you so much!
All your set of adaptive thresholding scripts is amazing.
Great work. It's very appreciated.

abd raha


MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!