|
13 | 13 | # Alexandre Gramfort, 2011
|
14 | 14 | # License: BSD 3 clause
|
15 | 15 |
|
16 |
| -import time as time |
17 |
| - |
18 |
| -import numpy as np |
19 |
| -from scipy.ndimage import gaussian_filter |
20 |
| - |
21 |
| -import matplotlib.pyplot as plt |
| 16 | +# %% |
| 17 | +# Generate data |
| 18 | +# ------------- |
22 | 19 |
|
23 | 20 | from skimage.data import coins
|
24 |
| -from skimage.transform import rescale |
25 | 21 |
|
26 |
| -from sklearn.feature_extraction.image import grid_to_graph |
27 |
| -from sklearn.cluster import AgglomerativeClustering |
28 |
| - |
29 |
| - |
30 |
| -# ############################################################################# |
31 |
| -# Generate data |
32 | 22 | orig_coins = coins()
|
33 | 23 |
|
| 24 | +# %% |
34 | 25 | # Resize it to 20% of the original size to speed up the processing
|
35 | 26 | # Applying a Gaussian filter for smoothing prior to down-scaling
|
36 | 27 | # reduces aliasing artifacts.
|
| 28 | + |
| 29 | +import numpy as np |
| 30 | +from scipy.ndimage import gaussian_filter |
| 31 | +from skimage.transform import rescale |
| 32 | + |
37 | 33 | smoothened_coins = gaussian_filter(orig_coins, sigma=2)
|
38 | 34 | rescaled_coins = rescale(
|
39 |
| - smoothened_coins, 0.2, mode="reflect", anti_aliasing=False, multichannel=False |
| 35 | + smoothened_coins, |
| 36 | + 0.2, |
| 37 | + mode="reflect", |
| 38 | + anti_aliasing=False, |
40 | 39 | )
|
41 | 40 |
|
42 | 41 | X = np.reshape(rescaled_coins, (-1, 1))
|
43 | 42 |
|
44 |
| -# ############################################################################# |
45 |
| -# Define the structure A of the data. Pixels connected to their neighbors. |
| 43 | +# %% |
| 44 | +# Define structure of the data |
| 45 | +# ---------------------------- |
| 46 | +# |
| 47 | +# Pixels are connected to their neighbors. |
| 48 | + |
| 49 | +from sklearn.feature_extraction.image import grid_to_graph |
| 50 | + |
46 | 51 | connectivity = grid_to_graph(*rescaled_coins.shape)
|
47 | 52 |
|
48 |
| -# ############################################################################# |
| 53 | +# %% |
49 | 54 | # Compute clustering
|
| 55 | +# ------------------ |
| 56 | + |
| 57 | +import time as time |
| 58 | + |
| 59 | +from sklearn.cluster import AgglomerativeClustering |
| 60 | + |
50 | 61 | print("Compute structured hierarchical clustering...")
|
51 | 62 | st = time.time()
|
52 | 63 | n_clusters = 27 # number of regions
|
|
55 | 66 | )
|
56 | 67 | ward.fit(X)
|
57 | 68 | label = np.reshape(ward.labels_, rescaled_coins.shape)
|
58 |
| -print("Elapsed time: ", time.time() - st) |
59 |
| -print("Number of pixels: ", label.size) |
60 |
| -print("Number of clusters: ", np.unique(label).size) |
| 69 | +print(f"Elapsed time: {time.time() - st:.3f}s") |
| 70 | +print(f"Number of pixels: {label.size}") |
| 71 | +print(f"Number of clusters: {np.unique(label).size}") |
61 | 72 |
|
62 |
| -# ############################################################################# |
| 73 | +# %% |
63 | 74 | # Plot the results on an image
|
| 75 | +# ---------------------------- |
| 76 | +# |
| 77 | +# Agglomerative clustering is able to segment each coin however, we have had to |
| 78 | +# use a ``n_cluster`` larger than the number of coins because the segmentation |
| 79 | +# is finding a large in the background. |
| 80 | + |
| 81 | +import matplotlib.pyplot as plt |
| 82 | + |
64 | 83 | plt.figure(figsize=(5, 5))
|
65 | 84 | plt.imshow(rescaled_coins, cmap=plt.cm.gray)
|
66 | 85 | for l in range(n_clusters):
|
|
70 | 89 | plt.cm.nipy_spectral(l / float(n_clusters)),
|
71 | 90 | ],
|
72 | 91 | )
|
73 |
| -plt.xticks(()) |
74 |
| -plt.yticks(()) |
| 92 | +plt.axis("off") |
75 | 93 | plt.show()
|
0 commit comments