.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/cluster/plot_segmentation_toy.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_cluster_plot_segmentation_toy.py>`
        to download the full example code or to run this example in your browser via JupyterLite or Binder

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

.. _sphx_glr_auto_examples_cluster_plot_segmentation_toy.py:


===========================================
Spectral clustering for image segmentation
===========================================

In this example, an image with connected circles is generated and
spectral clustering is used to separate the circles.

In these settings, the :ref:`spectral_clustering` approach solves the problem
know as 'normalized graph cuts': the image is seen as a graph of
connected voxels, and the spectral clustering algorithm amounts to
choosing graph cuts defining regions while minimizing the ratio of the
gradient along the cut, and the volume of the region.

As the algorithm tries to balance the volume (ie balance the region
sizes), if we take circles with different sizes, the segmentation fails.

In addition, as there is no useful information in the intensity of the image,
or its gradient, we choose to perform the spectral clustering on a graph
that is only weakly informed by the gradient. This is close to performing
a Voronoi partition of the graph.

In addition, we use the mask of the objects to restrict the graph to the
outline of the objects. In this example, we are interested in
separating the objects one from the other, and not from the background.

.. GENERATED FROM PYTHON SOURCE LINES 28-33

.. code-block:: default


    # Authors:  Emmanuelle Gouillart <emmanuelle.gouillart@normalesup.org>
    #           Gael Varoquaux <gael.varoquaux@normalesup.org>
    # License: BSD 3 clause








.. GENERATED FROM PYTHON SOURCE LINES 34-36

Generate the data
-----------------

.. GENERATED FROM PYTHON SOURCE LINES 36-53

.. code-block:: default

    import numpy as np

    l = 100
    x, y = np.indices((l, l))

    center1 = (28, 24)
    center2 = (40, 50)
    center3 = (67, 58)
    center4 = (24, 70)

    radius1, radius2, radius3, radius4 = 16, 14, 15, 14

    circle1 = (x - center1[0]) ** 2 + (y - center1[1]) ** 2 < radius1**2
    circle2 = (x - center2[0]) ** 2 + (y - center2[1]) ** 2 < radius2**2
    circle3 = (x - center3[0]) ** 2 + (y - center3[1]) ** 2 < radius3**2
    circle4 = (x - center4[0]) ** 2 + (y - center4[1]) ** 2 < radius4**2








.. GENERATED FROM PYTHON SOURCE LINES 54-56

Plotting four circles
---------------------

.. GENERATED FROM PYTHON SOURCE LINES 56-66

.. code-block:: default

    img = circle1 + circle2 + circle3 + circle4

    # We use a mask that limits to the foreground: the problem that we are
    # interested in here is not separating the objects from the background,
    # but separating them one from the other.
    mask = img.astype(bool)

    img = img.astype(float)
    img += 1 + 0.2 * np.random.randn(*img.shape)








.. GENERATED FROM PYTHON SOURCE LINES 67-69

Convert the image into a graph with the value of the gradient on the
edges.

.. GENERATED FROM PYTHON SOURCE LINES 69-73

.. code-block:: default

    from sklearn.feature_extraction import image

    graph = image.img_to_graph(img, mask=mask)








.. GENERATED FROM PYTHON SOURCE LINES 74-76

Take a decreasing function of the gradient resulting in a segmentation
that is close to a Voronoi partition

.. GENERATED FROM PYTHON SOURCE LINES 76-78

.. code-block:: default

    graph.data = np.exp(-graph.data / graph.data.std())








.. GENERATED FROM PYTHON SOURCE LINES 79-81

Here we perform spectral clustering using the arpack solver since amg is
numerically unstable on this example. We then plot the results.

.. GENERATED FROM PYTHON SOURCE LINES 81-95

.. code-block:: default

    import matplotlib.pyplot as plt

    from sklearn.cluster import spectral_clustering

    labels = spectral_clustering(graph, n_clusters=4, eigen_solver="arpack")
    label_im = np.full(mask.shape, -1.0)
    label_im[mask] = labels

    fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))
    axs[0].matshow(img)
    axs[1].matshow(label_im)

    plt.show()




.. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_segmentation_toy_001.png
   :alt: plot segmentation toy
   :srcset: /auto_examples/cluster/images/sphx_glr_plot_segmentation_toy_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 96-101

Plotting two circles
--------------------
Here we repeat the above process but only consider the first two circles
we generated. Note that this results in a cleaner separation between the
circles as the region sizes are easier to balance in this case.

.. GENERATED FROM PYTHON SOURCE LINES 101-120

.. code-block:: default


    img = circle1 + circle2
    mask = img.astype(bool)
    img = img.astype(float)

    img += 1 + 0.2 * np.random.randn(*img.shape)

    graph = image.img_to_graph(img, mask=mask)
    graph.data = np.exp(-graph.data / graph.data.std())

    labels = spectral_clustering(graph, n_clusters=2, eigen_solver="arpack")
    label_im = np.full(mask.shape, -1.0)
    label_im[mask] = labels

    fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))
    axs[0].matshow(img)
    axs[1].matshow(label_im)

    plt.show()



.. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_segmentation_toy_002.png
   :alt: plot segmentation toy
   :srcset: /auto_examples/cluster/images/sphx_glr_plot_segmentation_toy_002.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_auto_examples_cluster_plot_segmentation_toy.py:

.. only:: html

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


    .. container:: binder-badge

      .. image:: images/binder_badge_logo.svg
        :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/1.3.X?urlpath=lab/tree/notebooks/auto_examples/cluster/plot_segmentation_toy.ipynb
        :alt: Launch binder
        :width: 150 px



    .. container:: lite-badge

      .. image:: images/jupyterlite_badge_logo.svg
        :target: ../../lite/lab/?path=auto_examples/cluster/plot_segmentation_toy.ipynb
        :alt: Launch JupyterLite
        :width: 150 px

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

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

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

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


.. only:: html

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

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