
.. 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

        Click :ref:`here <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:


=========================
Different perimeters
=========================

In this example we show the uncertainty on calculating perimeters, comparing
classic and Crofton ones. For that, we evaluate the perimeters of a square and
its rotated version.

.. GENERATED FROM PYTHON SOURCE LINES 11-71



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





.. code-block:: default

    from skimage.measure import perimeter
    from skimage.measure import perimeter_crofton
    from skimage.transform import rotate
    import matplotlib.pyplot as plt
    import numpy as np


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

    # Construct 2 figures, square and disks
    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 objects, the different perimeters are evaluated
    for index, obj in enumerate([square, disk]):

        # 2 neighbourhoud configurations for measure.perimeter
        for n in [4, 6]:
            p = []
            angles = range(90)
            for i in angles:
                # rotation and perimeter evaluation
                rotated = rotate(obj, i, order=0)
                p.append(perimeter(rotated, n))
            ax[index].plot(angles, p)

        # 2 or 4 directions can be used by measure.perimeter_crofton
        for d in [2, 4]:
            p = []
            angles = np.arange(0, 90, 2)
            for i in angles:
                # rotation and perimeter evaluation
                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  7.642 seconds)


.. _sphx_glr_download_auto_examples_segmentation_plot_perimeters.py:


.. only :: html

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



  .. 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-jupyter

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


.. only:: html

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

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