.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "packages/scikit-image/auto_examples/plot_labels.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_packages_scikit-image_auto_examples_plot_labels.py: Labelling connected components of an image =========================================== This example shows how to label connected components of a binary image, using the dedicated skimage.measure.label function. .. GENERATED FROM PYTHON SOURCE LINES 8-39 .. image-sg:: /packages/scikit-image/auto_examples/images/sphx_glr_plot_labels_001.png :alt: plot labels :srcset: /packages/scikit-image/auto_examples/images/sphx_glr_plot_labels_001.png :class: sphx-glr-single-img .. code-block:: default from skimage import measure from skimage import filters import matplotlib.pyplot as plt import numpy as np n = 12 l = 256 np.random.seed(1) im = np.zeros((l, l)) points = l * np.random.random((2, n ** 2)) im[(points[0]).astype(int), (points[1]).astype(int)] = 1 im = filters.gaussian(im, sigma= l / (4. * n)) blobs = im > 0.7 * im.mean() all_labels = measure.label(blobs) blobs_labels = measure.label(blobs, background=0) plt.figure(figsize=(9, 3.5)) plt.subplot(131) plt.imshow(blobs, cmap='gray') plt.axis('off') plt.subplot(132) plt.imshow(all_labels, cmap='nipy_spectral') plt.axis('off') plt.subplot(133) plt.imshow(blobs_labels, cmap='nipy_spectral') plt.axis('off') plt.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.073 seconds) .. _sphx_glr_download_packages_scikit-image_auto_examples_plot_labels.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_labels.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_labels.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_