Trying out more thresholding techniques with scikit-image

We are going to threshold a test image comparing Otsu's, triangle, Niblack's, and Sauvola's thresholding techniques. Otsu and triangle are global thresholding techniques, while Niblack and Sauvola are local thresholding techniques. Local thresholding techniques are considered a better approach when the background is not uniform. For more information about Niblack's and Sauvola's thresholding algorithms, see An Introduction to Digital Image Processing (1986) and Adaptive document image binarization (2000), respectively. The full code for this example can be seen in the thresholding_scikit_image_techniques.py script. In order to try these methods, 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, threshold_triangle, threshold_niblack, threshold_sauvola)
from skimage import img_as_ubyte

In order to perform the thresholding operations with scikit-image, we call each thresholding method (threshold_otsu()threshold_niblack()threshold_sauvola(), and threshold_triangle()):

# Trying Otsu's scikit-image algorithm:
thresh_otsu = threshold_otsu(gray_image)
binary_otsu = gray_image > thresh_otsu
binary_otsu = img_as_ubyte(binary_otsu)

# Trying Niblack's scikit-image algorithm:
thresh_niblack = threshold_niblack(gray_image, window_size=25, k=0.8)
binary_niblack = gray_image > thresh_niblack
binary_niblack = img_as_ubyte(binary_niblack)

# Trying Sauvola's scikit-image algorithm:
thresh_sauvola = threshold_sauvola(gray_image, window_size=25)
binary_sauvola = gray_image > thresh_sauvola
binary_sauvola = img_as_ubyte(binary_sauvola)

# Trying triangle scikit-image algorithm:
thresh_triangle = threshold_triangle(gray_image)
binary_triangle = gray_image > thresh_triangle
binary_triangle = img_as_ubyte(binary_triangle)

The output can be seen in the next screenshot:

As you can see, local thresholding methods can provide better results when the background is not uniform. Indeed, these methods can be applied for text recognition. Finally, scikit-image comes with more thresholding techniques that you can try. If necessary, consult the API documentation to see all available methods at http://scikit-image.org/docs/dev/api/api.html.