{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Approximate and subdivide polygons\n\nThis example shows how to approximate (Douglas-Peucker algorithm) and subdivide\n(B-Splines) polygonal chains.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\n\nfrom skimage.draw import ellipse\nfrom skimage.measure import find_contours, approximate_polygon, subdivide_polygon\n\n\nhand = np.array(\n    [\n        [1.64516129, 1.16145833],\n        [1.64516129, 1.59375],\n        [1.35080645, 1.921875],\n        [1.375, 2.18229167],\n        [1.68548387, 1.9375],\n        [1.60887097, 2.55208333],\n        [1.68548387, 2.69791667],\n        [1.76209677, 2.56770833],\n        [1.83064516, 1.97395833],\n        [1.89516129, 2.75],\n        [1.9516129, 2.84895833],\n        [2.01209677, 2.76041667],\n        [1.99193548, 1.99479167],\n        [2.11290323, 2.63020833],\n        [2.2016129, 2.734375],\n        [2.25403226, 2.60416667],\n        [2.14919355, 1.953125],\n        [2.30645161, 2.36979167],\n        [2.39112903, 2.36979167],\n        [2.41532258, 2.1875],\n        [2.1733871, 1.703125],\n        [2.07782258, 1.16666667],\n    ]\n)\n\n# subdivide polygon using 2nd degree B-Splines\nnew_hand = hand.copy()\nfor _ in range(5):\n    new_hand = subdivide_polygon(new_hand, degree=2, preserve_ends=True)\n\n# approximate subdivided polygon with Douglas-Peucker algorithm\nappr_hand = approximate_polygon(new_hand, tolerance=0.02)\n\nprint(\"Number of coordinates:\", len(hand), len(new_hand), len(appr_hand))\n\nfig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(9, 4))\n\nax1.plot(hand[:, 0], hand[:, 1])\nax1.plot(new_hand[:, 0], new_hand[:, 1])\nax1.plot(appr_hand[:, 0], appr_hand[:, 1])\n\n\n# create two ellipses in image\nimg = np.zeros((800, 800), 'int32')\nrr, cc = ellipse(250, 250, 180, 230, img.shape)\nimg[rr, cc] = 1\nrr, cc = ellipse(600, 600, 150, 90, img.shape)\nimg[rr, cc] = 1\n\nplt.gray()\nax2.imshow(img)\n\n# approximate / simplify coordinates of the two ellipses\nfor contour in find_contours(img, 0):\n    coords = approximate_polygon(contour, tolerance=2.5)\n    ax2.plot(coords[:, 1], coords[:, 0], '-r', linewidth=2)\n    coords2 = approximate_polygon(contour, tolerance=39.5)\n    ax2.plot(coords2[:, 1], coords2[:, 0], '-g', linewidth=2)\n    print(\"Number of coordinates:\", len(contour), len(coords), len(coords2))\n\nax2.axis((0, 800, 0, 800))\n\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
}