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

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

.. _sphx_glr_auto_examples_segmentation_plot_perimeters.py:


============================================
Measure perimeters with different estimators
============================================

In this example, we show the error on measuring perimeters, comparing classic
approximations and Crofton ones. For that, we estimate the perimeter of an
object (either a square or a disk) and its rotated version, as we increase the
rotation angle.

.. GENERATED FROM PYTHON SOURCE LINES 12-77



.. image-sg:: /auto_examples/segmentation/images/sphx_glr_plot_perimeters_001.png
   :alt: Square, Disk
   :srcset: /auto_examples/segmentation/images/sphx_glr_plot_perimeters_001.png
   :class: sphx-glr-single-img





.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from skimage.measure import perimeter, perimeter_crofton
    from skimage.transform import rotate


    # scale parameter can be used to increase the grid size. The resulting curves
    # should be smoothed with higher scales
    scale = 10

    # Construct two objects, a square and a disk
    square = np.zeros((100 * scale, 100 * scale))
    square[40 * scale : 60 * scale, 40 * scale : 60 * scale] = 1

    [X, Y] = np.meshgrid(np.linspace(0, 100 * scale), np.linspace(0, 100 * scale))
    R = 20 * scale
    disk = (X - 50 * scale) ** 2 + (Y - 50 * scale) ** 2 <= R**2

    fig, axes = plt.subplots(1, 2, figsize=(8, 5))
    ax = axes.flatten()

    dX = X[0, 1] - X[0, 0]
    true_perimeters = [80 * scale, 2 * np.pi * R / dX]

    # For each type of object, estimate its perimeter as the object is rotated,
    # according to different approximations
    for index, obj in enumerate([square, disk]):
        # `neighborhood` value can be 4 or 8 for the classic perimeter estimator
        for n in [4, 8]:
            p = []
            angles = range(90)
            for i in angles:
                rotated = rotate(obj, i, order=0)
                p.append(perimeter(rotated, n))
            ax[index].plot(angles, p)

        # `directions` value can be 2 or 4 for the Crofton estimator
        for d in [2, 4]:
            p = []
            angles = np.arange(0, 90, 2)
            for i in angles:
                rotated = rotate(obj, i, order=0)
                p.append(perimeter_crofton(rotated, d))
            ax[index].plot(angles, p)

        ax[index].axhline(true_perimeters[index], linestyle='--', color='k')
        ax[index].set_xlabel('Rotation angle')
        ax[index].legend(
            [
                'N4 perimeter',
                'N8 perimeter',
                'Crofton 2 directions',
                'Crofton 4 directions',
                'Ground truth',
            ],
            loc='best',
        )
        ax[index].set_ylabel('Perimeter of the rotated object')

    ax[0].set_title('Square')
    ax[1].set_title('Disk')
    plt.tight_layout()
    plt.show()


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

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


.. _sphx_glr_download_auto_examples_segmentation_plot_perimeters.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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