{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Normalized Cut\n\nThis example constructs a Region Adjacency Graph (RAG) and recursively performs\na Normalized Cut on it [1]_.\n\n## References\n.. [1] Shi, J.; Malik, J., \"Normalized cuts and image segmentation\",\n       Pattern Analysis and Machine Intelligence,\n       IEEE Transactions on, vol. 22, no. 8, pp. 888-905, August 2000.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from skimage import data, segmentation, color\nfrom skimage import graph\nfrom matplotlib import pyplot as plt\n\n\nimg = data.coffee()\n\nlabels1 = segmentation.slic(img, compactness=30, n_segments=400, start_label=1)\nout1 = color.label2rgb(labels1, img, kind='avg', bg_label=0)\n\ng = graph.rag_mean_color(img, labels1, mode='similarity')\nlabels2 = graph.cut_normalized(labels1, g)\nout2 = color.label2rgb(labels2, img, kind='avg', bg_label=0)\n\nfig, ax = plt.subplots(nrows=2, sharex=True, sharey=True, figsize=(6, 8))\n\nax[0].imshow(out1)\nax[1].imshow(out2)\n\nfor a in ax:\n    a.axis('off')\n\nplt.tight_layout()"
      ]
    }
  ],
  "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
}