File Exchange

image thumbnail

Bernsen local image thresholding

version (258 KB) by Jan Motl
Implementation of Bernsen's thresholding method.


Updated 24 Oct 2014

View License

The method uses a user-provided contrast threshold. If the local contrast (max-min) is above or equal to the contrast threshold, the threshold is set at the local midgrey value (the mean of the minimum and maximum grey values in the local window). If the local contrast is below the contrast threshold the neighbourhood is considered to consist only of one class and the pixel is set to object or background depending on the value of the midgrey.

Cite As

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

Comments and Ratings (12)


Solved, thanks :)


Dear Jan,
thanks for your sharing
Could you please let me know how I can use it in a loop to segment a series of images?
Is it possible at all?

Jan Motl

All thresholding algorithms that I have submitted to MATLAB Central work at pixel level and return the results at the (same) pixel level.

If you want subpixel accuracy, you generally have to make some assumptions about the content of the image (e.g.: about its shape, color or structure). For example, if you know that the object in the image is a line or some known bitmap, it is easy to obtain subpixel accuracy. These methods are known as edge detection or object tracking respectively.

I am not familiar with general-purpose thresholding algorithms that would work at subpixel accuracy.

Lns K

I would like know if this method has a good results for subpixel accuracy ,

Shepelev Max

Excelent algorithm. It almost perfectly works with medical gray images and OCT scans. It can 'find' objects in a strong noisy environment.



Max Fedorchuck

Thanks for sharing!


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


great, thanks a lot dear jan Motl.

Jan Motl

I have retested Bersen thresholding on several operating systems and versions of MATLAB. In all configurations it worked.

But keep in mind that only 2-dimensinal matrices in uint8 are supported. However, 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 reduce dimensionality.

Also, it is generally advisable to test several sizes of the neighborhood to get good results.

For example, to get the illustration image from the attached jpg, use following command:
imshow(bernsen(rgb2gray(imread('image.jpg')), [93, 93]));

The description of the algorithm (and the parameters) is for example at:

If you are still having difficulties with the code, let me know more details (like version of MATALB, OS, example image, the minimal code reproducing the error,...) and I will do my best to help you.

Rohan Ghare

not working proper
what are different parameters of bernsen



Extended help text.

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