{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Hausdorff Distance\n\nThis example shows how to calculate the Hausdorff distance between two sets of\npoints. The [Hausdorff distance](https://en.wikipedia.org/wiki/Hausdorff_distance)_ is the maximum distance\nbetween any point on the first set and its nearest point on the second set,\nand vice-versa.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom skimage import metrics\n\nshape = (60, 60)\nimage = np.zeros(shape)\n\n# Create a diamond-like shape where the four corners form the 1st set of points\nx_diamond = 30\ny_diamond = 30\nr = 10\n\nfig, ax = plt.subplots()\nplt_x = [0, 1, 0, -1]\nplt_y = [1, 0, -1, 0]\n\nset_ax = [(x_diamond + r * x) for x in plt_x]\nset_ay = [(y_diamond + r * y) for y in plt_y]\nplt.plot(set_ax, set_ay, 'or')\n\n# Create a kite-like shape where the four corners form the 2nd set of points\nx_kite = 30\ny_kite = 30\nx_r = 15\ny_r = 20\n\nset_bx = [(x_kite + x_r * x) for x in plt_x]\nset_by = [(y_kite + y_r * y) for y in plt_y]\nplt.plot(set_bx, set_by, 'og')\n\n# Set up the data to compute the Hausdorff distance\ncoords_a = np.zeros(shape, dtype=bool)\ncoords_b = np.zeros(shape, dtype=bool)\nfor x, y in zip(set_ax, set_ay):\n    coords_a[(x, y)] = True\n\nfor x, y in zip(set_bx, set_by):\n    coords_b[(x, y)] = True\n\n# Call the Hausdorff function on the coordinates\nmetrics.hausdorff_distance(coords_a, coords_b)\nhausdorff_point_a, hausdorff_point_b = metrics.hausdorff_pair(coords_a, coords_b)\n\n# Plot the lines that shows the length of the Hausdorff distance\nx_line = [30, 30]\ny_line = [20, 10]\nplt.plot(x_line, y_line, 'y')\n\nx_line = [30, 30]\ny_line = [40, 50]\nplt.plot(x_line, y_line, 'y')\n\n# Plot circles to show that at this distance, the Hausdorff distance can\n# travel to its nearest neighbor (in this case, from the kite to diamond)\nax.add_artist(plt.Circle((30, 10), 10, color='y', fill=None))\nax.add_artist(plt.Circle((30, 50), 10, color='y', fill=None))\nax.add_artist(plt.Circle((15, 30), 10, color='y', fill=None))\nax.add_artist(plt.Circle((45, 30), 10, color='y', fill=None))\n\n# Annotate the returned pair of points that are Hausdorff distance apart\nax.annotate(\n    'a',\n    xy=hausdorff_point_a,\n    xytext=(35, 15),\n    arrowprops=dict(facecolor='red', shrink=0.005),\n)\nax.annotate(\n    'b',\n    xy=hausdorff_point_b,\n    xytext=(35, 5),\n    arrowprops=dict(facecolor='green', shrink=0.005),\n)\n\nax.imshow(image, cmap=plt.cm.gray)\nax.axis((0, 60, 60, 0))\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
}