
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/filters/plot_tophat.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_filters_plot_tophat.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_filters_plot_tophat.py:


================================================================
Removing small objects in grayscale images with a top hat filter
================================================================

This example shows how to remove small objects from grayscale images.
The top-hat transform [1]_ is an operation that extracts small elements and
details from given images. Here we use a white top-hat transform, which is
defined as the difference between the input image and its
(mathematical morphology) opening.

.. [1] https://en.wikipedia.org/wiki/Top-hat_transform

.. GENERATED FROM PYTHON SOURCE LINES 15-35



.. image-sg:: /auto_examples/filters/images/sphx_glr_plot_tophat_001.png
   :alt: Original, White tophat, Complementary
   :srcset: /auto_examples/filters/images/sphx_glr_plot_tophat_001.png
   :class: sphx-glr-single-img





.. code-block:: Python


    import matplotlib.pyplot as plt

    from skimage import data
    from skimage import color, morphology

    image = color.rgb2gray(data.hubble_deep_field())[:500, :500]

    footprint = morphology.disk(1)
    res = morphology.white_tophat(image, footprint)

    fig, ax = plt.subplots(ncols=3, figsize=(20, 8))
    ax[0].set_title('Original')
    ax[0].imshow(image, cmap='gray')
    ax[1].set_title('White tophat')
    ax[1].imshow(res, cmap='gray')
    ax[2].set_title('Complementary')
    ax[2].imshow(image - res, cmap='gray')

    plt.show()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.810 seconds)


.. _sphx_glr_download_auto_examples_filters_plot_tophat.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_tophat.ipynb <plot_tophat.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_tophat.py <plot_tophat.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_tophat.zip <plot_tophat.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
