In order to test scikit-image, we are going to threshold a test image using Otsu's binarization algorithm. In order to try this method, the first step is to import the required packages. In this case, in connection with scikit-image as follows:
from skimage.filters import threshold_otsu
from skimage import img_as_ubyte
The key code to apply Otsu's binarization algorithm with scikit-image is the following:
thresh = threshold_otsu(gray_image)
binary = gray_image > thresh
binary = img_as_ubyte(binary)
The threshold_otsu(gray_image) function returns the threshold value based on Otsu's binarization algorithm. Afterwards, with this value, the binary image is constructed (dtype= bool), which should be converted to 8-bit unsigned integer format (dtype= uint8) for proper visualization. The img_as_ubyte() function is used for this purpose. The full code for this example can be seen in the thresholding_scikit_image_otsu.py script.
The output can be seen in the following screenshot:
We will now try out some thresholding techniques with scikit-image.