
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/edges/plot_convex_hull.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_edges_plot_convex_hull.py>`
        to download the full example code.

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

.. _sphx_glr_auto_examples_edges_plot_convex_hull.py:


===========
Convex Hull
===========

The convex hull of a binary image is the set of pixels included in the
smallest convex polygon that surround all white pixels in the input.

A good overview of the algorithm is given on `Steve Eddin's blog
<https://blogs.mathworks.com/steve/2011/10/04/binary-image-convex-hull-algorithm-notes/>`__.

.. GENERATED FROM PYTHON SOURCE LINES 13-39

.. code-block:: Python


    import matplotlib.pyplot as plt

    from skimage.morphology import convex_hull_image
    from skimage import data, img_as_float
    from skimage.util import invert

    # The original image is inverted as the object must be white.
    image = invert(data.horse())

    chull = convex_hull_image(image)

    fig, axes = plt.subplots(1, 2, figsize=(8, 4))
    ax = axes.ravel()

    ax[0].set_title('Original picture')
    ax[0].imshow(image, cmap=plt.cm.gray)
    ax[0].set_axis_off()

    ax[1].set_title('Transformed picture')
    ax[1].imshow(chull, cmap=plt.cm.gray)
    ax[1].set_axis_off()

    plt.tight_layout()
    plt.show()




.. image-sg:: /auto_examples/edges/images/sphx_glr_plot_convex_hull_001.png
   :alt: Original picture, Transformed picture
   :srcset: /auto_examples/edges/images/sphx_glr_plot_convex_hull_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 40-42

We prepare a second plot to show the difference.


.. GENERATED FROM PYTHON SOURCE LINES 42-50

.. code-block:: Python


    chull_diff = img_as_float(chull.copy())
    chull_diff[image] = 2

    fig, ax = plt.subplots()
    ax.imshow(chull_diff, cmap=plt.cm.gray)
    ax.set_title('Difference')
    plt.show()



.. image-sg:: /auto_examples/edges/images/sphx_glr_plot_convex_hull_002.png
   :alt: Difference
   :srcset: /auto_examples/edges/images/sphx_glr_plot_convex_hull_002.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_auto_examples_edges_plot_convex_hull.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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