{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Measure perimeters with different estimators\n\nIn this example, we show the error on measuring perimeters, comparing classic\napproximations and Crofton ones. For that, we estimate the perimeter of an\nobject (either a square or a disk) and its rotated version, as we increase the\nrotation angle.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom skimage.measure import perimeter, perimeter_crofton\nfrom skimage.transform import rotate\n\n\n# scale parameter can be used to increase the grid size. The resulting curves\n# should be smoothed with higher scales\nscale = 10\n\n# Construct two objects, a square and a disk\nsquare = np.zeros((100 * scale, 100 * scale))\nsquare[40 * scale : 60 * scale, 40 * scale : 60 * scale] = 1\n\n[X, Y] = np.meshgrid(np.linspace(0, 100 * scale), np.linspace(0, 100 * scale))\nR = 20 * scale\ndisk = (X - 50 * scale) ** 2 + (Y - 50 * scale) ** 2 <= R**2\n\nfig, axes = plt.subplots(1, 2, figsize=(8, 5))\nax = axes.flatten()\n\ndX = X[0, 1] - X[0, 0]\ntrue_perimeters = [80 * scale, 2 * np.pi * R / dX]\n\n# For each type of object, estimate its perimeter as the object is rotated,\n# according to different approximations\nfor index, obj in enumerate([square, disk]):\n    # `neighborhood` value can be 4 or 8 for the classic perimeter estimator\n    for n in [4, 8]:\n        p = []\n        angles = range(90)\n        for i in angles:\n            rotated = rotate(obj, i, order=0)\n            p.append(perimeter(rotated, n))\n        ax[index].plot(angles, p)\n\n    # `directions` value can be 2 or 4 for the Crofton estimator\n    for d in [2, 4]:\n        p = []\n        angles = np.arange(0, 90, 2)\n        for i in angles:\n            rotated = rotate(obj, i, order=0)\n            p.append(perimeter_crofton(rotated, d))\n        ax[index].plot(angles, p)\n\n    ax[index].axhline(true_perimeters[index], linestyle='--', color='k')\n    ax[index].set_xlabel('Rotation angle')\n    ax[index].legend(\n        [\n            'N4 perimeter',\n            'N8 perimeter',\n            'Crofton 2 directions',\n            'Crofton 4 directions',\n            'Ground truth',\n        ],\n        loc='best',\n    )\n    ax[index].set_ylabel('Perimeter of the rotated object')\n\nax[0].set_title('Square')\nax[1].set_title('Disk')\nplt.tight_layout()\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
}