.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/cluster/plot_coin_segmentation.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_auto_examples_cluster_plot_coin_segmentation.py>`
        to download the full example code or to run this example in your browser via Binder

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

.. _sphx_glr_auto_examples_cluster_plot_coin_segmentation.py:


================================================
Segmenting the picture of greek coins in regions
================================================

This example uses :ref:`spectral_clustering` on a graph created from
voxel-to-voxel difference on an image to break this image into multiple
partly-homogeneous regions.

This procedure (spectral clustering on an image) is an efficient
approximate solution for finding normalized graph cuts.

There are two options to assign labels:

* with 'kmeans' spectral clustering will cluster samples in the embedding space
  using a kmeans algorithm
* whereas 'discrete' will iteratively search for the closest partition
  space to the embedding space.

.. GENERATED FROM PYTHON SOURCE LINES 20-69

.. code-block:: default

    print(__doc__)

    # Author: Gael Varoquaux <gael.varoquaux@normalesup.org>, Brian Cheung
    # License: BSD 3 clause

    import time

    import numpy as np
    from scipy.ndimage.filters import gaussian_filter
    import matplotlib.pyplot as plt
    import skimage
    from skimage.data import coins
    from skimage.transform import rescale

    from sklearn.feature_extraction import image
    from sklearn.cluster import spectral_clustering
    from sklearn.utils.fixes import parse_version

    # these were introduced in skimage-0.14
    if parse_version(skimage.__version__) >= parse_version('0.14'):
        rescale_params = {'anti_aliasing': False, 'multichannel': False}
    else:
        rescale_params = {}

    # load the coins as a numpy array
    orig_coins = coins()

    # Resize it to 20% of the original size to speed up the processing
    # Applying a Gaussian filter for smoothing prior to down-scaling
    # reduces aliasing artifacts.
    smoothened_coins = gaussian_filter(orig_coins, sigma=2)
    rescaled_coins = rescale(smoothened_coins, 0.2, mode="reflect",
                             **rescale_params)

    # Convert the image into a graph with the value of the gradient on the
    # edges.
    graph = image.img_to_graph(rescaled_coins)

    # Take a decreasing function of the gradient: an exponential
    # The smaller beta is, the more independent the segmentation is of the
    # actual image. For beta=1, the segmentation is close to a voronoi
    beta = 10
    eps = 1e-6
    graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps

    # Apply spectral clustering (this step goes much faster if you have pyamg
    # installed)
    N_REGIONS = 25








.. GENERATED FROM PYTHON SOURCE LINES 70-71

Visualize the resulting regions

.. GENERATED FROM PYTHON SOURCE LINES 71-90

.. code-block:: default


    for assign_labels in ('kmeans', 'discretize'):
        t0 = time.time()
        labels = spectral_clustering(graph, n_clusters=N_REGIONS,
                                     assign_labels=assign_labels, random_state=42)
        t1 = time.time()
        labels = labels.reshape(rescaled_coins.shape)

        plt.figure(figsize=(5, 5))
        plt.imshow(rescaled_coins, cmap=plt.cm.gray)
        for l in range(N_REGIONS):
            plt.contour(labels == l,
                        colors=[plt.cm.nipy_spectral(l / float(N_REGIONS))])
        plt.xticks(())
        plt.yticks(())
        title = 'Spectral clustering: %s, %.2fs' % (assign_labels, (t1 - t0))
        print(title)
        plt.title(title)
    plt.show()



.. rst-class:: sphx-glr-horizontal


    *

      .. image:: /auto_examples/cluster/images/sphx_glr_plot_coin_segmentation_001.png
          :alt: Spectral clustering: kmeans, 6.28s
          :class: sphx-glr-multi-img

    *

      .. image:: /auto_examples/cluster/images/sphx_glr_plot_coin_segmentation_002.png
          :alt: Spectral clustering: discretize, 5.46s
          :class: sphx-glr-multi-img


.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    Spectral clustering: kmeans, 6.28s
    Spectral clustering: discretize, 5.46s





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

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


.. _sphx_glr_download_auto_examples_cluster_plot_coin_segmentation.py:


.. only :: html

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


  .. container:: binder-badge

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


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

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



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

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


.. only:: html

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

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