.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/morpho/example_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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_morpho_example_watershed.py: Watershed Segmentation Example ============================== This example shows how to use the watershed on an image to make a segmentation. The watershed segmentation is based on the usage of an image as a landscape, where each minimum is a basin of a lake. It simulates the filling of these basins and where two basins meet, it make a watershed line. Meyer's watershed from local minima *********************************** First, we import the modules needed to use it .. GENERATED FROM PYTHON SOURCE LINES 16-22 .. code-block:: Python from pylena import morpho from skimage.data import camera import matplotlib.pyplot as plt import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 23-24 We first get an image .. GENERATED FROM PYTHON SOURCE LINES 24-26 .. code-block:: Python img = camera() .. GENERATED FROM PYTHON SOURCE LINES 27-28 We compute the gradient .. GENERATED FROM PYTHON SOURCE LINES 28-38 .. code-block:: Python se = morpho.make_structuring_element_2d("disc", 2) grad = morpho.gradient(img, se) plt.figure(figsize=(8, 8)) plt.imshow(grad, cmap="gray") plt.xticks([]) plt.yticks([]) plt.show() .. image-sg:: /examples/morpho/images/sphx_glr_example_watershed_001.png :alt: example watershed :srcset: /examples/morpho/images/sphx_glr_example_watershed_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 39-40 Then, we compute the watershed on this gradient. .. GENERATED FROM PYTHON SOURCE LINES 40-44 .. code-block:: Python ws, nlabel = morpho.watershed(img, 8) print(f"Number of label: {nlabel}") .. rst-class:: sphx-glr-script-out .. code-block:: none Number of label: 13563 .. GENERATED FROM PYTHON SOURCE LINES 45-46 We finally illustrate the resulting segmentation .. GENERATED FROM PYTHON SOURCE LINES 46-58 .. code-block:: Python np.random.seed(0) color = np.random.randint(low=0, high=255, size=(np.max(ws) + 1, 3)) color[0, :] = 0 # Watershed lines are illustrated in black colorized = color[ws] plt.figure(figsize=(8, 8)) plt.imshow(colorized) plt.xticks([]) plt.yticks([]) plt.show() .. image-sg:: /examples/morpho/images/sphx_glr_example_watershed_002.png :alt: example watershed :srcset: /examples/morpho/images/sphx_glr_example_watershed_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 59-67 The disadvantage of the watershed from local minima is the fact it over-segments the image. To solve this issue, it may be useful to provide some markers from which the watershed will start the flooding Watershed segmentation from user markers **************************************** First, we provide the input markers. .. GENERATED FROM PYTHON SOURCE LINES 68-74 .. code-block:: Python markers = np.zeros(img.shape, dtype=np.int16) markers[200:400, 100] = 1 markers[250:480, 450] = 1 markers[50, 50:450] = 1 .. GENERATED FROM PYTHON SOURCE LINES 75-76 Then, we compute the watershed from markers. .. GENERATED FROM PYTHON SOURCE LINES 76-79 .. code-block:: Python ws2, nlabel2 = morpho.watershed(img, 8, markers=markers) .. GENERATED FROM PYTHON SOURCE LINES 80-81 Finally we illustrate .. GENERATED FROM PYTHON SOURCE LINES 81-99 .. code-block:: Python color2 = np.random.randint(low=0, high=255, size=(np.max(ws2) + 1, 3)) color2[0, :] = 0 # Watershed lines are illustrated in black colorized2 = color2[ws2] markers = morpho.dilation(markers.astype(np.uint8), se) markers_illustration = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8) markers_illustration[markers > 0, 0] = 255 plt.figure(figsize=(12, 6)) plt.subplot(121) plt.title("Markers") plt.imshow(markers_illustration) plt.subplot(122) plt.title("Watershed by markers") plt.imshow(colorized2) plt.tight_layout() plt.show() .. image-sg:: /examples/morpho/images/sphx_glr_example_watershed_003.png :alt: Markers, Watershed by markers :srcset: /examples/morpho/images/sphx_glr_example_watershed_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.565 seconds) .. _sphx_glr_download_examples_morpho_example_watershed.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_watershed.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_watershed.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_watershed.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_