{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Find Regular Segments Using Compact Watershed\n\nThe watershed transform is commonly used as a starting point for many\nsegmentation algorithms. However, without a judicious choice of seeds, it\ncan produce very uneven fragment sizes, which can be difficult to deal with\nin downstream analyses.\n\nThe *compact* watershed transform remedies this by favoring seeds that are\nclose to the pixel being considered.\n\nBoth algorithms are implemented in the :func:`skimage.segmentation.watershed`\nfunction. To use the compact form, simply pass a ``compactness`` value greater\nthan 0.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nfrom skimage import data, util, filters, color\nfrom skimage.segmentation import watershed\nimport matplotlib.pyplot as plt\n\ncoins = data.coins()\nedges = filters.sobel(coins)\n\ngrid = util.regular_grid(coins.shape, n_points=468)\n\nseeds = np.zeros(coins.shape, dtype=int)\nseeds[grid] = np.arange(seeds[grid].size).reshape(seeds[grid].shape) + 1\n\nw0 = watershed(edges, seeds)\nw1 = watershed(edges, seeds, compactness=0.01)\n\nfig, (ax0, ax1) = plt.subplots(1, 2)\n\nax0.imshow(color.label2rgb(w0, coins, bg_label=-1))\nax0.set_title('Classical watershed')\n\nax1.imshow(color.label2rgb(w1, coins, bg_label=-1))\nax1.set_title('Compact watershed')\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
}