2010-08-26 19:15:11 +08:00
|
|
|
"""
|
2018-02-18 09:12:27 +08:00
|
|
|
================================================
|
|
|
|
|
Segmenting the picture of greek coins in regions
|
|
|
|
|
================================================
|
2010-08-26 19:15:11 +08:00
|
|
|
|
2011-09-26 03:20:37 +08:00
|
|
|
This example uses :ref:`spectral_clustering` on a graph created from
|
2010-08-26 19:15:11 +08:00
|
|
|
voxel-to-voxel difference on an image to break this image into multiple
|
2013-06-27 21:09:16 +08:00
|
|
|
partly-homogeneous regions.
|
2010-08-26 19:15:11 +08:00
|
|
|
|
|
|
|
|
This procedure (spectral clustering on an image) is an efficient
|
|
|
|
|
approximate solution for finding normalized graph cuts.
|
2012-10-26 16:00:33 +08:00
|
|
|
|
2021-11-02 18:32:14 +08:00
|
|
|
There are three options to assign labels:
|
2012-10-28 02:21:13 +08:00
|
|
|
|
2021-11-02 18:32:14 +08:00
|
|
|
* 'kmeans' spectral clustering clusters samples in the embedding space
|
2012-10-28 02:21:13 +08:00
|
|
|
using a kmeans algorithm
|
2021-11-02 18:32:14 +08:00
|
|
|
* 'discrete' iteratively searches for the closest partition
|
|
|
|
|
space to the embedding space of spectral clustering.
|
|
|
|
|
* 'cluster_qr' assigns labels using the QR factorization with pivoting
|
|
|
|
|
that directly determines the partition in the embedding space.
|
2010-08-26 19:15:11 +08:00
|
|
|
"""
|
|
|
|
|
|
2021-11-02 18:32:14 +08:00
|
|
|
# Author: Gael Varoquaux <gael.varoquaux@normalesup.org>
|
|
|
|
|
# Brian Cheung
|
|
|
|
|
# Andrew Knyazev <Andrew.Knyazev@ucdenver.edu>
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2010-08-26 19:15:11 +08:00
|
|
|
|
2012-10-26 16:00:33 +08:00
|
|
|
import time
|
|
|
|
|
|
2010-08-26 19:15:11 +08:00
|
|
|
import numpy as np
|
2018-02-13 00:54:18 +08:00
|
|
|
from scipy.ndimage.filters import gaussian_filter
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2019-01-17 18:41:13 +08:00
|
|
|
import skimage
|
2018-02-18 09:12:27 +08:00
|
|
|
from skimage.data import coins
|
2018-02-13 00:54:18 +08:00
|
|
|
from skimage.transform import rescale
|
2010-08-26 19:15:11 +08:00
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.feature_extraction import image
|
|
|
|
|
from sklearn.cluster import spectral_clustering
|
2020-06-24 19:35:49 +08:00
|
|
|
from sklearn.utils.fixes import parse_version
|
2015-11-26 04:50:10 +08:00
|
|
|
|
2019-01-17 18:41:13 +08:00
|
|
|
# these were introduced in skimage-0.14
|
2020-06-24 19:35:49 +08:00
|
|
|
if parse_version(skimage.__version__) >= parse_version("0.14"):
|
2019-01-17 18:41:13 +08:00
|
|
|
rescale_params = {"anti_aliasing": False, "multichannel": False}
|
|
|
|
|
else:
|
|
|
|
|
rescale_params = {}
|
2015-11-26 04:50:10 +08:00
|
|
|
|
2018-02-18 09:12:27 +08:00
|
|
|
# load the coins as a numpy array
|
|
|
|
|
orig_coins = coins()
|
2015-11-26 04:50:10 +08:00
|
|
|
|
2018-02-18 09:12:27 +08:00
|
|
|
# Resize it to 20% of the original size to speed up the processing
|
2018-02-13 00:54:18 +08:00
|
|
|
# Applying a Gaussian filter for smoothing prior to down-scaling
|
|
|
|
|
# reduces aliasing artifacts.
|
2018-02-18 09:12:27 +08:00
|
|
|
smoothened_coins = gaussian_filter(orig_coins, sigma=2)
|
2019-01-17 18:41:13 +08:00
|
|
|
rescaled_coins = rescale(smoothened_coins, 0.2, mode="reflect", **rescale_params)
|
2010-08-26 19:15:11 +08:00
|
|
|
|
|
|
|
|
# Convert the image into a graph with the value of the gradient on the
|
|
|
|
|
# edges.
|
2018-02-18 09:12:27 +08:00
|
|
|
graph = image.img_to_graph(rescaled_coins)
|
2010-08-26 19:15:11 +08:00
|
|
|
|
|
|
|
|
# Take a decreasing function of the gradient: an exponential
|
2012-05-06 00:33:24 +08:00
|
|
|
# The smaller beta is, the more independent the segmentation is of the
|
2010-08-27 20:12:58 +08:00
|
|
|
# actual image. For beta=1, the segmentation is close to a voronoi
|
2018-02-18 09:12:27 +08:00
|
|
|
beta = 10
|
2011-12-24 02:12:26 +08:00
|
|
|
eps = 1e-6
|
2015-11-26 04:50:10 +08:00
|
|
|
graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps
|
2010-08-26 19:15:11 +08:00
|
|
|
|
2021-11-02 18:32:14 +08:00
|
|
|
# The number of segmented regions to display needs to be chosen manually.
|
|
|
|
|
# The current version of 'spectral_clustering' does not support determining
|
|
|
|
|
# the number of good quality clusters automatically.
|
|
|
|
|
n_regions = 26
|
2012-10-28 02:34:26 +08:00
|
|
|
|
2020-06-09 11:23:14 +08:00
|
|
|
# %%
|
2021-11-02 18:32:14 +08:00
|
|
|
# Compute and visualize the resulting regions
|
|
|
|
|
|
|
|
|
|
# Computing a few extra eigenvectors may speed up the eigen_solver.
|
|
|
|
|
# The spectral clustering quality may also benetif from requesting
|
|
|
|
|
# extra regions for segmentation.
|
|
|
|
|
n_regions_plus = 3
|
|
|
|
|
|
|
|
|
|
# Apply spectral clustering using the default eigen_solver='arpack'.
|
|
|
|
|
# Any implemented solver can be used: eigen_solver='arpack', 'lobpcg', or 'amg'.
|
|
|
|
|
# Choosing eigen_solver='amg' requires an extra package called 'pyamg'.
|
|
|
|
|
# The quality of segmentation and the speed of calculations is mostly determined
|
|
|
|
|
# by the choice of the solver and the value of the tolerance 'eigen_tol'.
|
|
|
|
|
# TODO: varying eigen_tol seems to have no effect for 'lobpcg' and 'amg' #21243.
|
|
|
|
|
for assign_labels in ("kmeans", "discretize", "cluster_qr"):
|
2012-10-26 16:00:33 +08:00
|
|
|
t0 = time.time()
|
2012-10-28 02:21:13 +08:00
|
|
|
labels = spectral_clustering(
|
2021-11-02 18:32:14 +08:00
|
|
|
graph,
|
|
|
|
|
n_clusters=(n_regions + n_regions_plus),
|
|
|
|
|
eigen_tol=1e-7,
|
|
|
|
|
assign_labels=assign_labels,
|
|
|
|
|
random_state=42,
|
2017-12-18 17:20:53 +08:00
|
|
|
)
|
2021-11-02 18:32:14 +08:00
|
|
|
|
2012-10-26 16:00:33 +08:00
|
|
|
t1 = time.time()
|
2018-02-18 09:12:27 +08:00
|
|
|
labels = labels.reshape(rescaled_coins.shape)
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.figure(figsize=(5, 5))
|
2018-02-18 09:12:27 +08:00
|
|
|
plt.imshow(rescaled_coins, cmap=plt.cm.gray)
|
2021-11-02 18:32:14 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.xticks(())
|
|
|
|
|
plt.yticks(())
|
2015-11-26 04:50:10 +08:00
|
|
|
title = "Spectral clustering: %s, %.2fs" % (assign_labels, (t1 - t0))
|
|
|
|
|
print(title)
|
|
|
|
|
plt.title(title)
|
2021-11-02 18:32:14 +08:00
|
|
|
for l in range(n_regions):
|
|
|
|
|
colors = [plt.cm.nipy_spectral((l + 4) / float(n_regions + 4))]
|
|
|
|
|
plt.contour(labels == l, colors=colors)
|
|
|
|
|
# To view individual segments as appear comment in plt.pause(0.5)
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|
2021-11-02 18:32:14 +08:00
|
|
|
|
|
|
|
|
# TODO: After #21194 is merged and #21243 is fixed, check which eigen_solver
|
|
|
|
|
# is the best and set eigen_solver='arpack', 'lobpcg', or 'amg' and eigen_tol
|
|
|
|
|
# explicitly in this example.
|