.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/segmentation/plot_multiotsu.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_multiotsu.py: ======================= Multi-Otsu Thresholding ======================= The multi-Otsu threshold [1]_ is a thresholding algorithm that is used to separate the pixels of an input image into several different classes, each one obtained according to the intensity of the gray levels within the image. Multi-Otsu calculates several thresholds, determined by the number of desired classes. The default number of classes is 3: for obtaining three classes, the algorithm returns two threshold values. They are represented by a red line in the histogram below. .. [1] Liao, P-S., Chen, T-S. and Chung, P-C., "A fast algorithm for multilevel thresholding", Journal of Information Science and Engineering 17 (5): 713-727, 2001. Available at: . .. GENERATED FROM PYTHON SOURCE LINES 20-63 .. image-sg:: /auto_examples/segmentation/images/sphx_glr_plot_multiotsu_001.png :alt: Original, Histogram, Multi-Otsu result :srcset: /auto_examples/segmentation/images/sphx_glr_plot_multiotsu_001.png :class: sphx-glr-single-img .. code-block:: Python import matplotlib import matplotlib.pyplot as plt import numpy as np from skimage import data from skimage.filters import threshold_multiotsu # Setting the font size for all plots. matplotlib.rcParams['font.size'] = 9 # The input image. image = data.camera() # Applying multi-Otsu threshold for the default value, generating # three classes. thresholds = threshold_multiotsu(image) # Using the threshold values, we generate the three regions. regions = np.digitize(image, bins=thresholds) fig, ax = plt.subplots(nrows=1, ncols=3, figsize=(10, 3.5)) # Plotting the original image. ax[0].imshow(image, cmap='gray') ax[0].set_title('Original') ax[0].axis('off') # Plotting the histogram and the two thresholds obtained from # multi-Otsu. ax[1].hist(image.ravel(), bins=255) ax[1].set_title('Histogram') for thresh in thresholds: ax[1].axvline(thresh, color='r') # Plotting the Multi Otsu result. ax[2].imshow(regions, cmap='jet') ax[2].set_title('Multi-Otsu result') ax[2].axis('off') plt.subplots_adjust() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.140 seconds) .. _sphx_glr_download_auto_examples_segmentation_plot_multiotsu.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_multiotsu.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_multiotsu.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_multiotsu.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_multiotsu.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_