.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` 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. .. image:: /intro/scipy/auto_examples/images/sphx_glr_plot_mathematical_morpho_001.png :class: sphx-glr-single-img .. code-block:: python # 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 from scipy import ndimage opened_mask = ndimage.binary_opening(mask) closed_mask = ndimage.binary_closing(opened_mask) # Plot from matplotlib import 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() **Total running time of the script:** ( 0 minutes 0.041 seconds) .. _sphx_glr_download_intro_scipy_auto_examples_plot_mathematical_morpho.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: plot_mathematical_morpho.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: plot_mathematical_morpho.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_