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
|
|
|
|
2012-10-28 02:21:13 +08:00
|
|
|
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.
|
2010-08-26 19:15:11 +08:00
|
|
|
"""
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2010-08-26 19:15:11 +08:00
|
|
|
|
2012-10-26 16:00:33 +08:00
|
|
|
# Author: Gael Varoquaux <gael.varoquaux@normalesup.org>, Brian Cheung
|
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
|
|
|
|
|
|
|
|
# Apply spectral clustering (this step goes much faster if you have pyamg
|
|
|
|
|
# installed)
|
2015-11-26 04:50:10 +08:00
|
|
|
N_REGIONS = 25
|
2012-10-28 02:34:26 +08:00
|
|
|
|
2020-06-09 11:23:14 +08:00
|
|
|
# %%
|
2010-08-26 19:15:11 +08:00
|
|
|
# Visualize the resulting regions
|
2012-10-28 02:34:26 +08:00
|
|
|
|
|
|
|
|
for assign_labels in ('kmeans', 'discretize'):
|
2012-10-26 16:00:33 +08:00
|
|
|
t0 = time.time()
|
2012-10-28 02:21:13 +08:00
|
|
|
labels = spectral_clustering(graph, n_clusters=N_REGIONS,
|
2017-12-18 17:20:53 +08:00
|
|
|
assign_labels=assign_labels, random_state=42)
|
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)
|
2012-10-26 16:00:33 +08:00
|
|
|
|
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)
|
2012-10-26 16:00:33 +08:00
|
|
|
for l in range(N_REGIONS):
|
2018-02-01 17:16:14 +08:00
|
|
|
plt.contour(labels == l,
|
2018-03-07 13:49:45 +08:00
|
|
|
colors=[plt.cm.nipy_spectral(l / float(N_REGIONS))])
|
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)
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|