.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "intro/scipy/auto_examples/plot_connect_measurements.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_connect_measurements.py: ============================= Demo connected components ============================= Extracting and labeling connected components in a 2D array .. GENERATED FROM PYTHON SOURCE LINES 8-12 .. code-block:: default import numpy as np import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 13-14 Generate some binary data .. GENERATED FROM PYTHON SOURCE LINES 14-32 .. code-block:: default np.random.seed(0) x, y = np.indices((100, 100)) sig = np.sin(2*np.pi*x/50.) * np.sin(2*np.pi*y/50.) * (1+x*y/50.**2)**2 mask = sig > 1 plt.figure(figsize=(7, 3.5)) plt.subplot(1, 2, 1) plt.imshow(sig) plt.axis('off') plt.title('sig') plt.subplot(1, 2, 2) plt.imshow(mask, cmap=plt.cm.gray) plt.axis('off') plt.title('mask') plt.subplots_adjust(wspace=.05, left=.01, bottom=.01, right=.99, top=.9) .. image-sg:: /intro/scipy/auto_examples/images/sphx_glr_plot_connect_measurements_001.png :alt: sig, mask :srcset: /intro/scipy/auto_examples/images/sphx_glr_plot_connect_measurements_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 33-34 Label connected components .. GENERATED FROM PYTHON SOURCE LINES 34-45 .. code-block:: default import scipy as sp labels, nb = sp.ndimage.label(mask) plt.figure(figsize=(3.5, 3.5)) plt.imshow(labels) plt.title('label') plt.axis('off') plt.subplots_adjust(wspace=.05, left=.01, bottom=.01, right=.99, top=.9) .. image-sg:: /intro/scipy/auto_examples/images/sphx_glr_plot_connect_measurements_002.png :alt: label :srcset: /intro/scipy/auto_examples/images/sphx_glr_plot_connect_measurements_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 46-47 Extract the 4th connected component, and crop the array around it .. GENERATED FROM PYTHON SOURCE LINES 47-56 .. code-block:: default sl = sp.ndimage.find_objects(labels==4) plt.figure(figsize=(3.5, 3.5)) plt.imshow(sig[sl[0]]) plt.title('Cropped connected component') plt.axis('off') plt.subplots_adjust(wspace=.05, left=.01, bottom=.01, right=.99, top=.9) plt.show() .. image-sg:: /intro/scipy/auto_examples/images/sphx_glr_plot_connect_measurements_003.png :alt: Cropped connected component :srcset: /intro/scipy/auto_examples/images/sphx_glr_plot_connect_measurements_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.112 seconds) .. _sphx_glr_download_intro_scipy_auto_examples_plot_connect_measurements.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_connect_measurements.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_connect_measurements.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_