.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/morpho/example_morphological_operations.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_morphological_operations.py: Structural morphological operations =================================== This example shows how to use some structural morphological operations available in pylena. Right now, five operations are available: - Erosion (:py:func:`pylena.morpho.erosion`) - Dilation (:py:func:`pylena.morpho.dilation`) - Opening (:py:func:`pylena.morpho.opening`) - Closing (:py:func:`pylena.morpho.closing`) - Gradient (:py:func:`pylena.morpho.gradient`) Example ------- First, we import the different libraries needed for this example to run and we read the image. .. GENERATED FROM PYTHON SOURCE LINES 22-30 .. code-block:: Python import matplotlib.pyplot as plt from skimage.morphology import diamond from skimage.data import camera import pylena as pln img = camera() .. GENERATED FROM PYTHON SOURCE LINES 31-42 Then, we define the structuring element used for the morphological operations. Four type of structuring element are available: - A 2D rectangle - A disc - Periodic lines - A mask, represented as a 2D NumPy array. This makes possible to use the `scikit-image structuring elements `_. To make it easier to use a predifined structuring element, the function ``make_structuring_element_2d`` has been defined. .. GENERATED FROM PYTHON SOURCE LINES 42-56 .. code-block:: Python rect_se = pln.morpho.make_structuring_element_2d("rect", 10, 10) disc_se = pln.morpho.make_structuring_element_2d("disc", 5) pl_se = pln.morpho.make_structuring_element_2d("periodic_line", (5, 2), 5) diamond_se = diamond(5) operations = [ pln.morpho.erosion, pln.morpho.dilation, pln.morpho.opening, pln.morpho.closing, pln.morpho.gradient, ] .. GENERATED FROM PYTHON SOURCE LINES 57-60 Then, we use the morphological operations. They requires only two arguments: the input image, represented as a 2D NumPy arrays with a uint8 dtype, and the structuring element as explained above. .. GENERATED FROM PYTHON SOURCE LINES 60-81 .. code-block:: Python xtick_label = ["Rectangle", "Disc", "Periodic lines", "Diamond"] ytick_label = ["erosion", "dilation", "opening", "closing", "gradient"] fig, ax = plt.subplots(nrows=5, ncols=4, figsize=(13, 15)) for i in range(4): ax[0, i].set_xlabel(xtick_label[i], fontsize=20) ax[0, i].xaxis.set_label_position("top") for i in range(5): op = operations[i] ax[i, 0].set_ylabel(ytick_label[i], fontsize=20) ax[i, 0].imshow(op(img, rect_se), cmap="gray") ax[i, 1].imshow(op(img, disc_se), cmap="gray") ax[i, 2].imshow(op(img, pl_se), cmap="gray") ax[i, 3].imshow(op(img, diamond_se), cmap="gray") for a1 in ax: for a in a1: a.set_xticks([]) a.set_yticks([]) plt.tight_layout(pad=0, h_pad=0.5) plt.show() .. image-sg:: /examples/morpho/images/sphx_glr_example_morphological_operations_001.png :alt: example morphological operations :srcset: /examples/morpho/images/sphx_glr_example_morphological_operations_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.705 seconds) .. _sphx_glr_download_examples_morpho_example_morphological_operations.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_morphological_operations.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_morphological_operations.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_morphological_operations.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_