Thresholding is used to create a binary image from a grayscale image .
A more comprehensive presentation on Thresholding
We illustrate how to apply one of these thresholding algorithms. Otsu’s method  calculates an “optimal” threshold (marked by a red line in the histogram below) by maximizing the variance between two classes of pixels, which are separated by the threshold. Equivalently, this threshold minimizes the intra-class variance.
import matplotlib.pyplot as plt from skimage import data from skimage.filters import threshold_otsu image = data.camera() thresh = threshold_otsu(image) binary = image > thresh fig, axes = plt.subplots(ncols=3, figsize=(8, 2.5)) ax = axes.ravel() ax = plt.subplot(1, 3, 1, adjustable='box-forced') ax = plt.subplot(1, 3, 2) ax = plt.subplot(1, 3, 3, sharex=ax, sharey=ax, adjustable='box-forced') ax.imshow(image, cmap=plt.cm.gray) ax.set_title('Original') ax.axis('off') ax.hist(image.ravel(), bins=256) ax.set_title('Histogram') ax.axvline(thresh, color='r') ax.imshow(binary, cmap=plt.cm.gray) ax.set_title('Thresholded') ax.axis('off') plt.show()
If you are not familiar with the details of the different algorithms and the underlying assumptions, it is often difficult to know which algorithm will give the best results. Therefore, Scikit-image includes a function to evaluate thresholding algorithms provided by the library. At a glance, you can select the best algorithm for you data without a deep understanding of their mechanisms.
Total running time of the script: ( 0 minutes 1.521 seconds)