.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "intro/scipy/auto_examples/plot_mathematical_morpho.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_intro_scipy_auto_examples_plot_mathematical_morpho.py: ============================= Demo mathematical morphology ============================= A basic demo of binary opening and closing. .. GENERATED FROM PYTHON SOURCE LINES 8-49 .. image-sg:: /intro/scipy/auto_examples/images/sphx_glr_plot_mathematical_morpho_001.png :alt: a, mask, opened_mask, closed_mask :srcset: /intro/scipy/auto_examples/images/sphx_glr_plot_mathematical_morpho_001.png :class: sphx-glr-single-img .. code-block:: default # Generate some binary data import numpy as np np.random.seed(0) a = np.zeros((50, 50)) a[10:-10, 10:-10] = 1 a += 0.25 * np.random.standard_normal(a.shape) mask = a>=0.5 # Apply mathematical morphology import scipy as sp opened_mask = sp.ndimage.binary_opening(mask) closed_mask = sp.ndimage.binary_closing(opened_mask) # Plot import matplotlib.pyplot as plt plt.figure(figsize=(12, 3.5)) plt.subplot(141) plt.imshow(a, cmap=plt.cm.gray) plt.axis('off') plt.title('a') plt.subplot(142) plt.imshow(mask, cmap=plt.cm.gray) plt.axis('off') plt.title('mask') plt.subplot(143) plt.imshow(opened_mask, cmap=plt.cm.gray) plt.axis('off') plt.title('opened_mask') plt.subplot(144) plt.imshow(closed_mask, cmap=plt.cm.gray) plt.title('closed_mask') plt.axis('off') plt.subplots_adjust(wspace=.05, left=.01, bottom=.01, right=.99, top=.99) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.098 seconds) .. _sphx_glr_download_intro_scipy_auto_examples_plot_mathematical_morpho.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_mathematical_morpho.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_mathematical_morpho.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_