{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Thresholding\n\nThresholding is used to create a binary image from a grayscale image [1]_.\n\n.. [1] https://en.wikipedia.org/wiki/Thresholding_%28image_processing%29\n\n.. seealso::\n    A more comprehensive presentation on\n    `sphx_glr_auto_examples_applications_plot_thresholding_guide.py`\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nfrom skimage import data\nfrom skimage.filters import threshold_otsu"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We illustrate how to apply one of these thresholding algorithms.\nOtsu's method [2]_ calculates an \"optimal\" threshold (marked by a red line in the\nhistogram below) by maximizing the variance between two classes of pixels,\nwhich are separated by the threshold. Equivalently, this threshold minimizes\nthe intra-class variance.\n\n.. [2] https://en.wikipedia.org/wiki/Otsu's_method\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "image = data.camera()\nthresh = threshold_otsu(image)\nbinary = image > thresh\n\nfig, axes = plt.subplots(ncols=3, figsize=(8, 2.5))\nax = axes.ravel()\nax[0] = plt.subplot(1, 3, 1)\nax[1] = plt.subplot(1, 3, 2)\nax[2] = plt.subplot(1, 3, 3, sharex=ax[0], sharey=ax[0])\n\nax[0].imshow(image, cmap=plt.cm.gray)\nax[0].set_title('Original')\nax[0].axis('off')\n\nax[1].hist(image.ravel(), bins=256)\nax[1].set_title('Histogram')\nax[1].axvline(thresh, color='r')\n\nax[2].imshow(binary, cmap=plt.cm.gray)\nax[2].set_title('Thresholded')\nax[2].axis('off')\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "If you are not familiar with the details of the different algorithms and the\nunderlying assumptions, it is often difficult to know which algorithm will give\nthe best results. Therefore, Scikit-image includes a function to evaluate\nthresholding algorithms provided by the library. At a glance, you can select\nthe best algorithm for your data without a deep understanding of their\nmechanisms.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from skimage.filters import try_all_threshold\n\nimg = data.page()\n\nfig, ax = try_all_threshold(img, figsize=(10, 8), verbose=False)\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
}