Search code examples
pythonimage-processingscikit-imageimage-thresholding

Scikit image otsu thresholding producing zero threshold


I am doing some preprocessing on an image, After applying Otsu thresholding on the image I am getting zero threshold while on another image it’s working fine

from skimage import filters
from skimage.io import imread

img = imread(img_path, as_gray=True)

threshold = filters.threshold_otsu(img)
print(threshold)

H-S-1-G-01.tif enter image description here

Threshold : 0

original_1_1.png enter image description here

Threshold : 204


Solution

  • That is the correct result. Your image is bi-level, it only has values of 0 and 255, so 0 is a threshold that will split the image into two values correctly when you do the next step:

    threshold = filters.threshold_otsu(img)
    binary = im > threshold
    

    Try it for yourself with some dummy "images":

    filters.threshold_otsu(np.array([0,255],dtype=np.uint8))
    0
    
    filters.threshold_otsu(np.array([7,12],dtype=np.uint8)) 
    7
    
    filters.threshold_otsu(np.array([7,8,11,12],dtype=np.uint8))
    8