{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Multi-Otsu Thresholding\n\nThe multi-Otsu threshold [1]_ is a thresholding algorithm that is used to separate\nthe pixels of an input image into several different classes, each one obtained\naccording to the intensity of the gray levels within the image.\n\nMulti-Otsu calculates several thresholds, determined by the number of desired\nclasses. The default number of classes is 3: for obtaining three classes, the\nalgorithm returns two threshold values. They are represented by a red line in\nthe histogram below.\n\n.. [1] Liao, P-S., Chen, T-S. and Chung, P-C., \"A fast algorithm for multilevel\n       thresholding\", Journal of Information Science and Engineering 17 (5):\n       713-727, 2001. Available at:\n       <https://ftp.iis.sinica.edu.tw/JISE/2001/200109_01.pdf>.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom skimage import data\nfrom skimage.filters import threshold_multiotsu\n\n# Setting the font size for all plots.\nmatplotlib.rcParams['font.size'] = 9\n\n# The input image.\nimage = data.camera()\n\n# Applying multi-Otsu threshold for the default value, generating\n# three classes.\nthresholds = threshold_multiotsu(image)\n\n# Using the threshold values, we generate the three regions.\nregions = np.digitize(image, bins=thresholds)\n\nfig, ax = plt.subplots(nrows=1, ncols=3, figsize=(10, 3.5))\n\n# Plotting the original image.\nax[0].imshow(image, cmap='gray')\nax[0].set_title('Original')\nax[0].axis('off')\n\n# Plotting the histogram and the two thresholds obtained from\n# multi-Otsu.\nax[1].hist(image.ravel(), bins=255)\nax[1].set_title('Histogram')\nfor thresh in thresholds:\n    ax[1].axvline(thresh, color='r')\n\n# Plotting the Multi Otsu result.\nax[2].imshow(regions, cmap='jet')\nax[2].set_title('Multi-Otsu result')\nax[2].axis('off')\n\nplt.subplots_adjust()\n\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}