.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/segmentation/plot_thresholding.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_segmentation_plot_thresholding.py: ============ Thresholding ============ Thresholding is used to create a binary image from a grayscale image [1]_. .. [1] https://en.wikipedia.org/wiki/Thresholding_%28image_processing%29 .. seealso:: A more comprehensive presentation on :ref:`sphx_glr_auto_examples_applications_plot_thresholding_guide.py` .. GENERATED FROM PYTHON SOURCE LINES 15-20 .. code-block:: Python import matplotlib.pyplot as plt from skimage import data from skimage.filters import threshold_otsu .. GENERATED FROM PYTHON SOURCE LINES 21-29 We illustrate how to apply one of these thresholding algorithms. Otsu's method [2]_ 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. .. [2] https://en.wikipedia.org/wiki/Otsu's_method .. GENERATED FROM PYTHON SOURCE LINES 29-55 .. code-block:: Python image = data.camera() thresh = threshold_otsu(image) binary = image > thresh fig, axes = plt.subplots(ncols=3, figsize=(8, 2.5)) ax = axes.ravel() ax[0] = plt.subplot(1, 3, 1) ax[1] = plt.subplot(1, 3, 2) ax[2] = plt.subplot(1, 3, 3, sharex=ax[0], sharey=ax[0]) ax[0].imshow(image, cmap=plt.cm.gray) ax[0].set_title('Original') ax[0].axis('off') ax[1].hist(image.ravel(), bins=256) ax[1].set_title('Histogram') ax[1].axvline(thresh, color='r') ax[2].imshow(binary, cmap=plt.cm.gray) ax[2].set_title('Thresholded') ax[2].axis('off') plt.show() .. image-sg:: /auto_examples/segmentation/images/sphx_glr_plot_thresholding_001.png :alt: Original, Histogram, Thresholded :srcset: /auto_examples/segmentation/images/sphx_glr_plot_thresholding_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 56-63 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 your data without a deep understanding of their mechanisms. .. GENERATED FROM PYTHON SOURCE LINES 63-70 .. code-block:: Python from skimage.filters import try_all_threshold img = data.page() fig, ax = try_all_threshold(img, figsize=(10, 8), verbose=False) plt.show() .. image-sg:: /auto_examples/segmentation/images/sphx_glr_plot_thresholding_002.png :alt: Original, Isodata, Li, Mean, Minimum, Otsu, Triangle, Yen :srcset: /auto_examples/segmentation/images/sphx_glr_plot_thresholding_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.053 seconds) .. _sphx_glr_download_auto_examples_segmentation_plot_thresholding.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-image/scikit-image/v0.24.0?filepath=notebooks/auto_examples/segmentation/plot_thresholding.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_thresholding.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_thresholding.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_