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

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

.. _sphx_glr_auto_examples_plot_splinebox_vs_scipy_coin.py:


Comparison splinebox and scipy: contour approximation
-----------------------------------------------------

This example compares splinebox and scipy when trying to approximate a contour/shape
with a closed spline with a fixe number of control points.

.. GENERATED FROM PYTHON SOURCE LINES 8-15

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np
    import scipy
    import skimage
    import splinebox








.. GENERATED FROM PYTHON SOURCE LINES 16-17

Let's load the astronaut example image from skimage and crop out a single coins using slicing.

.. GENERATED FROM PYTHON SOURCE LINES 17-21

.. code-block:: Python

    img = skimage.data.coins()[90:150, 240:300]
    plt.imshow(img, cmap="gray")
    plt.show()




.. image-sg:: /auto_examples/images/sphx_glr_plot_splinebox_vs_scipy_coin_001.png
   :alt: plot splinebox vs scipy coin
   :srcset: /auto_examples/images/sphx_glr_plot_splinebox_vs_scipy_coin_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 22-23

Next, we will segment the coin using Otsu's method and fix some holes in the mask.

.. GENERATED FROM PYTHON SOURCE LINES 23-29

.. code-block:: Python

    thresh = skimage.filters.threshold_otsu(img)
    mask = img > thresh
    mask = skimage.morphology.remove_small_holes(mask)
    plt.imshow(mask, cmap="gray")
    plt.show()




.. image-sg:: /auto_examples/images/sphx_glr_plot_splinebox_vs_scipy_coin_002.png
   :alt: plot splinebox vs scipy coin
   :srcset: /auto_examples/images/sphx_glr_plot_splinebox_vs_scipy_coin_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 30-33

We can find the contour of the coin using find contours.
The return value is a list of contours. Since our binary image only
has one contour we select the first element of the list.

.. GENERATED FROM PYTHON SOURCE LINES 33-39

.. code-block:: Python

    contours = skimage.measure.find_contours(mask)
    contour = contours[0]
    plt.imshow(mask, cmap="gray", alpha=0.5)
    plt.plot(contour[:, 1], contour[:, 0])
    plt.show()




.. image-sg:: /auto_examples/images/sphx_glr_plot_splinebox_vs_scipy_coin_003.png
   :alt: plot splinebox vs scipy coin
   :srcset: /auto_examples/images/sphx_glr_plot_splinebox_vs_scipy_coin_003.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 40-42

For closed contours, the last point is identical to the first one, so we will
chop it off.

.. GENERATED FROM PYTHON SOURCE LINES 42-44

.. code-block:: Python

    contour = contour[:-1]








.. GENERATED FROM PYTHON SOURCE LINES 45-48

Our goal is to fit a cubic B-spline with M control points to
the contour. We will first use splinbox to acchive this and then
use scipy.

.. GENERATED FROM PYTHON SOURCE LINES 48-64

.. code-block:: Python

    M = 9

    spline = splinebox.Spline(M=M, basis_function=splinebox.B3(), closed=True)
    spline.fit(contour)

    ts = np.linspace(0, M, 100)
    splinebox_vals = spline.eval(ts)
    splinebox_control_points = spline.control_points

    plt.imshow(mask, cmap="gray", alpha=0.5)
    # plt.scatter(contours[0][:, 1], contours[0][:, 0], label="contour")
    plt.plot(splinebox_vals[:, 1], splinebox_vals[:, 0], label="splinebox")
    plt.scatter(splinebox_control_points[:, 1], splinebox_control_points[:, 0])
    plt.legend()
    plt.show()




.. image-sg:: /auto_examples/images/sphx_glr_plot_splinebox_vs_scipy_coin_004.png
   :alt: plot splinebox vs scipy coin
   :srcset: /auto_examples/images/sphx_glr_plot_splinebox_vs_scipy_coin_004.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 65-66

Let see how this compares to scipy...

.. GENERATED FROM PYTHON SOURCE LINES 66-71

.. code-block:: Python


    # Number of data points
    N = len(contour)
    k = 3








.. GENERATED FROM PYTHON SOURCE LINES 72-75

To get a spline with a specific number of control points in scipy
we have to precalculate the parameters values `t` for the knots and the parameter values `u`
for the data points. It is important that we account for the periodicity and padding of the knots.

.. GENERATED FROM PYTHON SOURCE LINES 75-78

.. code-block:: Python

    t = np.arange(-k, M + k + 1)
    u = np.linspace(0, M, N + 1)[:-1]








.. GENERATED FROM PYTHON SOURCE LINES 79-83

When constructinc the spline using `splprep` we have to specify the oder of the basis spline,
the `u` and `t` we just computed, and the periodicity. Since we don't want to smooth our fit
but instead regularize it by fixing the number of control points we need to set `s=0` and
`task=-1`.

.. GENERATED FROM PYTHON SOURCE LINES 83-94

.. code-block:: Python

    tck, u = scipy.interpolate.splprep(contour.T, k=k, u=u, t=t, task=-1, s=0, per=N)
    ts = np.linspace(0, M, 100)
    scipy_vals = scipy.interpolate.splev(ts, tck)
    scipy_control_points = tck[1]

    plt.imshow(mask, cmap="gray", alpha=0.5)
    # plt.scatter(contours[0][:, 1], contours[0][:, 0], label="contour")
    plt.plot(scipy_vals[1], scipy_vals[0], label="scipy")
    plt.scatter(scipy_control_points[1], scipy_control_points[0])
    plt.legend()
    plt.show()



.. image-sg:: /auto_examples/images/sphx_glr_plot_splinebox_vs_scipy_coin_005.png
   :alt: plot splinebox vs scipy coin
   :srcset: /auto_examples/images/sphx_glr_plot_splinebox_vs_scipy_coin_005.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_auto_examples_plot_splinebox_vs_scipy_coin.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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