
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/segmentation/plot_compact_watershed.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_segmentation_plot_compact_watershed.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_segmentation_plot_compact_watershed.py:


=============================================
Find Regular Segments Using Compact Watershed
=============================================

The watershed transform is commonly used as a starting point for many
segmentation algorithms. However, without a judicious choice of seeds, it
can produce very uneven fragment sizes, which can be difficult to deal with
in downstream analyses.

The *compact* watershed transform remedies this by favoring seeds that are
close to the pixel being considered.

Both algorithms are implemented in the :func:`skimage.segmentation.watershed`
function. To use the compact form, simply pass a ``compactness`` value greater
than 0.

.. GENERATED FROM PYTHON SOURCE LINES 18-44



.. image-sg:: /auto_examples/segmentation/images/sphx_glr_plot_compact_watershed_001.png
   :alt: Classical watershed, Compact watershed
   :srcset: /auto_examples/segmentation/images/sphx_glr_plot_compact_watershed_001.png
   :class: sphx-glr-single-img





.. code-block:: Python


    import numpy as np
    from skimage import data, util, filters, color
    from skimage.segmentation import watershed
    import matplotlib.pyplot as plt

    coins = data.coins()
    edges = filters.sobel(coins)

    grid = util.regular_grid(coins.shape, n_points=468)

    seeds = np.zeros(coins.shape, dtype=int)
    seeds[grid] = np.arange(seeds[grid].size).reshape(seeds[grid].shape) + 1

    w0 = watershed(edges, seeds)
    w1 = watershed(edges, seeds, compactness=0.01)

    fig, (ax0, ax1) = plt.subplots(1, 2)

    ax0.imshow(color.label2rgb(w0, coins, bg_label=-1))
    ax0.set_title('Classical watershed')

    ax1.imshow(color.label2rgb(w1, coins, bg_label=-1))
    ax1.set_title('Compact watershed')

    plt.show()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.442 seconds)


.. _sphx_glr_download_auto_examples_segmentation_plot_compact_watershed.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_compact_watershed.ipynb <plot_compact_watershed.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_compact_watershed.py <plot_compact_watershed.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_compact_watershed.zip <plot_compact_watershed.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
