.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/features_detection/plot_remove_objects.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_remove_objects.py: ================ Removing objects ================ scikit-image has several ways of removing objects from N-dimensional images. Here, "objects" (and "holes") are defined as groups of samples with the same label value which distinct from the background and other objects. This example shows how to remove objects based on their size, or their distances from other objects. .. GENERATED FROM PYTHON SOURCE LINES 13-41 .. image-sg:: /auto_examples/features_detection/images/sphx_glr_plot_remove_objects_001.png :alt: labeled objects, large objects, spaced objects (nearest removed), small objects :srcset: /auto_examples/features_detection/images/sphx_glr_plot_remove_objects_001.png :class: sphx-glr-single-img .. code-block:: Python import matplotlib.pyplot as plt import skimage as ski # Extract foreground by thresholding an image taken by the Hubble Telescope image = ski.color.rgb2gray(ski.data.hubble_deep_field()) foreground = image > ski.filters.threshold_li(image) objects = ski.measure.label(foreground) # Separate objects into regions larger and smaller than 100 pixels large_objects = ski.morphology.remove_small_objects(objects, min_size=100) small_objects = objects ^ large_objects # Remove objects until remaining ones are at least 100 pixels apart. # By default, larger ones take precedence. spaced_objects = ski.morphology.remove_objects_by_distance(objects, min_distance=100) # Plot the results fig, ax = plt.subplots(2, 2, figsize=(10, 10), sharex=True, sharey=True) ax[0, 0].set_title("labeled objects") ax[0, 0].imshow(ski.color.label2rgb(objects, image=image, bg_label=0)) ax[0, 1].set_title("large objects") ax[0, 1].imshow(ski.color.label2rgb(large_objects, bg_label=0)) ax[1, 1].set_title("small objects") ax[1, 1].imshow(ski.color.label2rgb(small_objects, bg_label=0)) ax[1, 0].set_title("spaced objects (nearest removed)") ax[1, 0].imshow(ski.color.label2rgb(spaced_objects, bg_label=0)) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.192 seconds) .. _sphx_glr_download_auto_examples_features_detection_plot_remove_objects.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/features_detection/plot_remove_objects.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_remove_objects.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_remove_objects.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_