2010-12-03 19:10:45 +08:00
|
|
|
"""
|
2018-02-18 09:12:27 +08:00
|
|
|
======================================================================
|
|
|
|
|
A demo of structured Ward hierarchical clustering on an image of coins
|
|
|
|
|
======================================================================
|
2010-12-03 19:10:45 +08:00
|
|
|
|
2011-01-23 06:37:20 +08:00
|
|
|
Compute the segmentation of a 2D image with Ward hierarchical
|
|
|
|
|
clustering. The clustering is spatially constrained in order
|
|
|
|
|
for each segmented region to be in one piece.
|
2021-10-22 21:33:22 +08:00
|
|
|
|
2010-12-03 19:10:45 +08:00
|
|
|
"""
|
2011-01-23 06:37:20 +08:00
|
|
|
|
|
|
|
|
# Author : Vincent Michel, 2010
|
|
|
|
|
# Alexandre Gramfort, 2011
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2011-01-23 06:37:20 +08:00
|
|
|
|
2022-04-22 04:08:25 +08:00
|
|
|
# %%
|
|
|
|
|
# Generate data
|
|
|
|
|
# -------------
|
2015-11-26 04:50:10 +08:00
|
|
|
|
2018-02-18 09:12:27 +08:00
|
|
|
from skimage.data import coins
|
2018-02-13 00:54:18 +08:00
|
|
|
|
2018-02-18 09:12:27 +08:00
|
|
|
orig_coins = coins()
|
2015-11-26 04:50:10 +08:00
|
|
|
|
2022-04-22 04:08:25 +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.
|
2022-04-22 04:08:25 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
from scipy.ndimage import gaussian_filter
|
|
|
|
|
from skimage.transform import rescale
|
|
|
|
|
|
2018-02-18 09:12:27 +08:00
|
|
|
smoothened_coins = gaussian_filter(orig_coins, sigma=2)
|
2022-03-02 18:32:06 +08:00
|
|
|
rescaled_coins = rescale(
|
2022-04-22 04:08:25 +08:00
|
|
|
smoothened_coins,
|
|
|
|
|
0.2,
|
|
|
|
|
mode="reflect",
|
|
|
|
|
anti_aliasing=False,
|
2022-03-02 18:32:06 +08:00
|
|
|
)
|
2015-11-26 04:50:10 +08:00
|
|
|
|
2018-02-18 09:12:27 +08:00
|
|
|
X = np.reshape(rescaled_coins, (-1, 1))
|
2010-12-03 19:10:45 +08:00
|
|
|
|
2022-04-22 04:08:25 +08:00
|
|
|
# %%
|
|
|
|
|
# Define structure of the data
|
|
|
|
|
# ----------------------------
|
|
|
|
|
#
|
|
|
|
|
# Pixels are connected to their neighbors.
|
|
|
|
|
|
|
|
|
|
from sklearn.feature_extraction.image import grid_to_graph
|
|
|
|
|
|
2018-02-18 09:12:27 +08:00
|
|
|
connectivity = grid_to_graph(*rescaled_coins.shape)
|
2010-12-03 19:10:45 +08:00
|
|
|
|
2022-04-22 04:08:25 +08:00
|
|
|
# %%
|
2010-12-03 19:10:45 +08:00
|
|
|
# Compute clustering
|
2022-04-22 04:08:25 +08:00
|
|
|
# ------------------
|
|
|
|
|
|
|
|
|
|
import time as time
|
|
|
|
|
|
|
|
|
|
from sklearn.cluster import AgglomerativeClustering
|
|
|
|
|
|
2013-02-01 22:04:03 +08:00
|
|
|
print("Compute structured hierarchical clustering...")
|
2010-12-03 19:10:45 +08:00
|
|
|
st = time.time()
|
2018-02-18 09:12:27 +08:00
|
|
|
n_clusters = 27 # number of regions
|
2015-11-26 04:50:10 +08:00
|
|
|
ward = AgglomerativeClustering(
|
|
|
|
|
n_clusters=n_clusters, linkage="ward", connectivity=connectivity
|
|
|
|
|
)
|
|
|
|
|
ward.fit(X)
|
2018-02-18 09:12:27 +08:00
|
|
|
label = np.reshape(ward.labels_, rescaled_coins.shape)
|
2022-04-22 04:08:25 +08:00
|
|
|
print(f"Elapsed time: {time.time() - st:.3f}s")
|
|
|
|
|
print(f"Number of pixels: {label.size}")
|
|
|
|
|
print(f"Number of clusters: {np.unique(label).size}")
|
2010-12-03 19:10:45 +08:00
|
|
|
|
2022-04-22 04:08:25 +08:00
|
|
|
# %%
|
2011-01-03 00:44:25 +08:00
|
|
|
# Plot the results on an image
|
2022-04-22 04:08:25 +08:00
|
|
|
# ----------------------------
|
|
|
|
|
#
|
|
|
|
|
# Agglomerative clustering is able to segment each coin however, we have had to
|
|
|
|
|
# use a ``n_cluster`` larger than the number of coins because the segmentation
|
|
|
|
|
# is finding a large in the background.
|
|
|
|
|
|
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
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)
|
2011-01-26 02:16:14 +08:00
|
|
|
for l in range(n_clusters):
|
2018-02-01 17:16:14 +08:00
|
|
|
plt.contour(
|
|
|
|
|
label == l,
|
2018-03-07 13:49:45 +08:00
|
|
|
colors=[
|
|
|
|
|
plt.cm.nipy_spectral(l / float(n_clusters)),
|
|
|
|
|
],
|
|
|
|
|
)
|
2022-04-22 04:08:25 +08:00
|
|
|
plt.axis("off")
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|