.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/segmentation/plot_extrema.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_segmentation_plot_extrema.py: =============================== Extrema =============================== We detect local maxima in a galaxy image. The image is corrupted by noise, generating many local maxima. To keep only those maxima with sufficient local contrast, we use h-maxima. .. GENERATED FROM PYTHON SOURCE LINES 10-36 .. code-block:: Python import matplotlib.pyplot as plt from skimage.measure import label from skimage import data from skimage import color from skimage.morphology import extrema from skimage import exposure color_image = data.hubble_deep_field() # for illustration purposes, we work on a crop of the image. x_0 = 70 y_0 = 354 width = 100 height = 100 img = color.rgb2gray(color_image)[y_0 : (y_0 + height), x_0 : (x_0 + width)] # the rescaling is done only for visualization purpose. # the algorithms would work identically in an unscaled version of the # image. However, the parameter h needs to be adapted to the scale. img = exposure.rescale_intensity(img) .. GENERATED FROM PYTHON SOURCE LINES 37-38 MAXIMA DETECTION .. GENERATED FROM PYTHON SOURCE LINES 38-66 .. code-block:: Python # Maxima in the galaxy image are detected by mathematical morphology. # There is no a priori constraint on the density. # We find all local maxima local_maxima = extrema.local_maxima(img) label_maxima = label(local_maxima) overlay = color.label2rgb( label_maxima, img, alpha=0.7, bg_label=0, bg_color=None, colors=[(1, 0, 0)] ) # We observed in the previous image, that there are many local maxima # that are caused by the noise in the image. # For this, we find all local maxima with a height of h. # This height is the gray level value by which we need to descent # in order to reach a higher maximum and it can be seen as a local # contrast measurement. # The value of h scales with the dynamic range of the image, i.e. # if we multiply the image with a constant, we need to multiply # the value of h with the same constant in order to achieve the same result. h = 0.05 h_maxima = extrema.h_maxima(img, h) label_h_maxima = label(h_maxima) overlay_h = color.label2rgb( label_h_maxima, img, alpha=0.7, bg_label=0, bg_color=None, colors=[(1, 0, 0)] ) .. GENERATED FROM PYTHON SOURCE LINES 67-68 GRAPHICAL OUTPUT .. GENERATED FROM PYTHON SOURCE LINES 68-84 .. code-block:: Python # a new figure with 3 subplots fig, ax = plt.subplots(1, 3, figsize=(15, 5)) ax[0].imshow(img, cmap='gray') ax[0].set_title('Original image') ax[0].axis('off') ax[1].imshow(overlay) ax[1].set_title('Local Maxima') ax[1].axis('off') ax[2].imshow(overlay_h) ax[2].set_title(f'h maxima for h = {h:.2f}') ax[2].axis('off') plt.show() .. image-sg:: /auto_examples/segmentation/images/sphx_glr_plot_extrema_001.png :alt: Original image, Local Maxima, h maxima for h = 0.05 :srcset: /auto_examples/segmentation/images/sphx_glr_plot_extrema_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.251 seconds) .. _sphx_glr_download_auto_examples_segmentation_plot_extrema.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.24.0?filepath=notebooks/auto_examples/segmentation/plot_extrema.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_extrema.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_extrema.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_