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;
Jan Motl (2020). Niblack local thresholding (https://www.mathworks.com/matlabcentral/fileexchange/40849-niblack-local-thresholding), MATLAB Central File Exchange. Retrieved .
@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).
@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.
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: https://doi.org/10.1109/INMIC.2004.1492847
If you don't get satisfying results with this method, try Sauvola local thresholding function (also published at File Exchange), which improves this method.
Ho to define k?
Hello,thank you for this code.But, It generaes an error for me :The input image must be a two-dimensional array.
Thank you so much!
All your set of adaptive thresholding scripts is amazing.
Great work. It's very appreciated.
Better code illustration.