.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "advanced/image_processing/auto_examples/plot_watershed_segmentation.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end <sphx_glr_download_advanced_image_processing_auto_examples_plot_watershed_segmentation.py>` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_advanced_image_processing_auto_examples_plot_watershed_segmentation.py: Watershed segmentation ======================= This example shows how to do segmentation with watershed. .. GENERATED FROM PYTHON SOURCE LINES 7-44 .. image-sg:: /advanced/image_processing/auto_examples/images/sphx_glr_plot_watershed_segmentation_001.png :alt: plot watershed segmentation :srcset: /advanced/image_processing/auto_examples/images/sphx_glr_plot_watershed_segmentation_001.png :class: sphx-glr-single-img .. code-block:: Python import numpy as np from skimage.segmentation import watershed from skimage.feature import peak_local_max import matplotlib.pyplot as plt import scipy as sp # Generate an initial image with two overlapping circles x, y = np.indices((80, 80)) x1, y1, x2, y2 = 28, 28, 44, 52 r1, r2 = 16, 20 mask_circle1 = (x - x1) ** 2 + (y - y1) ** 2 < r1**2 mask_circle2 = (x - x2) ** 2 + (y - y2) ** 2 < r2**2 image = np.logical_or(mask_circle1, mask_circle2) # Now we want to separate the two objects in image # Generate the markers as local maxima of the distance # to the background distance = sp.ndimage.distance_transform_edt(image) peak_idx = peak_local_max(distance, footprint=np.ones((3, 3)), labels=image) peak_mask = np.zeros_like(distance, dtype=bool) peak_mask[tuple(peak_idx.T)] = True markers = sp.ndimage.label(peak_mask)[0] labels = watershed(-distance, markers, mask=image) plt.figure(figsize=(9, 3.5)) plt.subplot(131) plt.imshow(image, cmap="gray", interpolation="nearest") plt.axis("off") plt.subplot(132) plt.imshow(-distance, interpolation="nearest") plt.axis("off") plt.subplot(133) plt.imshow(labels, cmap="nipy_spectral", interpolation="nearest") plt.axis("off") plt.subplots_adjust(hspace=0.01, wspace=0.01, top=1, bottom=0, left=0, right=1) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.060 seconds) .. _sphx_glr_download_advanced_image_processing_auto_examples_plot_watershed_segmentation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_watershed_segmentation.ipynb <plot_watershed_segmentation.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_watershed_segmentation.py <plot_watershed_segmentation.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_watershed_segmentation.zip <plot_watershed_segmentation.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_