.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/segmentation/plot_marked_watershed.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_segmentation_plot_marked_watershed.py: =============================== Markers for watershed transform =============================== The watershed is a classical algorithm used for **segmentation**, that is, for separating different objects in an image. Here a marker image is built from the region of low gradient inside the image. In a gradient image, the areas of high values provide barriers that help to segment the image. Using markers on the lower values will ensure that the segmented objects are found. See Wikipedia_ for more details on the algorithm. .. _Wikipedia: https://en.wikipedia.org/wiki/Watershed_(image_processing) .. GENERATED FROM PYTHON SOURCE LINES 20-70 .. image-sg:: /auto_examples/segmentation/images/sphx_glr_plot_marked_watershed_001.png :alt: Original, Local Gradient, Markers, Segmented :srcset: /auto_examples/segmentation/images/sphx_glr_plot_marked_watershed_001.png :class: sphx-glr-single-img .. code-block:: Python from scipy import ndimage as ndi import matplotlib.pyplot as plt from skimage.morphology import disk from skimage.segmentation import watershed from skimage import data from skimage.filters import rank from skimage.util import img_as_ubyte image = img_as_ubyte(data.eagle()) # denoise image denoised = rank.median(image, disk(2)) # find continuous region (low gradient - # where less than 10 for this image) --> markers # disk(5) is used here to get a more smooth image markers = rank.gradient(denoised, disk(5)) < 10 markers = ndi.label(markers)[0] # local gradient (disk(2) is used to keep edges thin) gradient = rank.gradient(denoised, disk(2)) # process the watershed labels = watershed(gradient, markers) # display results fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(8, 8), sharex=True, sharey=True) ax = axes.ravel() ax[0].imshow(image, cmap=plt.cm.gray) ax[0].set_title("Original") ax[1].imshow(gradient, cmap=plt.cm.nipy_spectral) ax[1].set_title("Local Gradient") ax[2].imshow(markers, cmap=plt.cm.nipy_spectral) ax[2].set_title("Markers") ax[3].imshow(image, cmap=plt.cm.gray) ax[3].imshow(labels, cmap=plt.cm.nipy_spectral, alpha=0.5) ax[3].set_title("Segmented") for a in ax: a.axis('off') fig.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.574 seconds) .. _sphx_glr_download_auto_examples_segmentation_plot_marked_watershed.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-image/scikit-image/v0.23.2?filepath=notebooks/auto_examples/segmentation/plot_marked_watershed.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_marked_watershed.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_marked_watershed.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_