{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Convex Hull\n\nThe convex hull of a binary image is the set of pixels included in the\nsmallest convex polygon that surround all white pixels in the input.\n\nA 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/)_.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nfrom skimage.morphology import convex_hull_image\nfrom skimage import data, img_as_float\nfrom skimage.util import invert\n\n# The original image is inverted as the object must be white.\nimage = invert(data.horse())\n\nchull = convex_hull_image(image)\n\nfig, axes = plt.subplots(1, 2, figsize=(8, 4))\nax = axes.ravel()\n\nax[0].set_title('Original picture')\nax[0].imshow(image, cmap=plt.cm.gray)\nax[0].set_axis_off()\n\nax[1].set_title('Transformed picture')\nax[1].imshow(chull, cmap=plt.cm.gray)\nax[1].set_axis_off()\n\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We prepare a second plot to show the difference.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "chull_diff = img_as_float(chull.copy())\nchull_diff[image] = 2\n\nfig, ax = plt.subplots()\nax.imshow(chull_diff, cmap=plt.cm.gray)\nax.set_title('Difference')\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}