.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/features_detection/plot_holes_and_peaks.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 or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_features_detection_plot_holes_and_peaks.py: =============================== Filling holes and finding peaks =============================== We fill holes (i.e. isolated, dark spots) in an image using morphological reconstruction by erosion. Erosion expands the minimal values of the seed image until it encounters a mask image. Thus, the seed image and mask image represent the maximum and minimum possible values of the reconstructed image. We start with an image containing both peaks and holes: .. GENERATED FROM PYTHON SOURCE LINES 14-24 .. code-block:: Python import matplotlib.pyplot as plt from skimage import data from skimage.exposure import rescale_intensity image = data.moon() # Rescale image intensity so that we can see dim features. image = rescale_intensity(image, in_range=(50, 200)) .. GENERATED FROM PYTHON SOURCE LINES 25-31 Now we need to create the seed image, where the minima represent the starting points for erosion. To fill holes, we initialize the seed image to the maximum value of the original image. Along the borders, however, we use the original values of the image. These border pixels will be the starting points for the erosion process. We then limit the erosion by setting the mask to the values of the original image. .. GENERATED FROM PYTHON SOURCE LINES 31-41 .. code-block:: Python import numpy as np from skimage.morphology import reconstruction seed = np.copy(image) seed[1:-1, 1:-1] = image.max() mask = image filled = reconstruction(seed, mask, method='erosion') .. GENERATED FROM PYTHON SOURCE LINES 42-53 As shown above, eroding inward from the edges removes holes, since (by definition) holes are surrounded by pixels of brighter value. Finally, we can isolate the dark regions by subtracting the reconstructed image from the original image. Alternatively, we can find bright spots in an image using morphological reconstruction by dilation. Dilation is the inverse of erosion and expands the *maximal* values of the seed image until it encounters a mask image. Since this is an inverse operation, we initialize the seed image to the minimum image intensity instead of the maximum. The remainder of the process is the same. .. GENERATED FROM PYTHON SOURCE LINES 53-77 .. code-block:: Python seed = np.copy(image) seed[1:-1, 1:-1] = image.min() rec = reconstruction(seed, mask, method='dilation') fig, ax = plt.subplots(2, 2, figsize=(5, 4), sharex=True, sharey=True) ax = ax.ravel() ax[0].imshow(image, cmap='gray') ax[0].set_title('Original image') ax[0].axis('off') ax[1].imshow(filled, cmap='gray') ax[1].set_title('after filling holes') ax[1].axis('off') ax[2].imshow(image - filled, cmap='gray') ax[2].set_title('holes') ax[2].axis('off') ax[3].imshow(image - rec, cmap='gray') ax[3].set_title('peaks') ax[3].axis('off') plt.show() .. image-sg:: /auto_examples/features_detection/images/sphx_glr_plot_holes_and_peaks_001.png :alt: Original image, after filling holes, holes, peaks :srcset: /auto_examples/features_detection/images/sphx_glr_plot_holes_and_peaks_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.331 seconds) .. _sphx_glr_download_auto_examples_features_detection_plot_holes_and_peaks.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-image/scikit-image/v0.23.2?filepath=notebooks/auto_examples/features_detection/plot_holes_and_peaks.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_holes_and_peaks.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_holes_and_peaks.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_