2010-12-03 19:10:45 +08:00
|
|
|
"""
|
2015-11-26 04:50:10 +08:00
|
|
|
=========================================================================
|
|
|
|
|
A demo of structured Ward hierarchical clustering on a raccoon face image
|
|
|
|
|
=========================================================================
|
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.
|
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
|
|
|
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2010-12-03 19:10:45 +08:00
|
|
|
|
|
|
|
|
import time as time
|
2015-11-26 04:50:10 +08:00
|
|
|
|
2010-12-03 19:10:45 +08:00
|
|
|
import numpy as np
|
|
|
|
|
import scipy as sp
|
2015-11-26 04:50:10 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2015-11-26 04:50:10 +08:00
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.feature_extraction.image import grid_to_graph
|
2014-02-06 19:51:42 +08:00
|
|
|
from sklearn.cluster import AgglomerativeClustering
|
2015-11-26 04:50:10 +08:00
|
|
|
|
2010-12-03 19:10:45 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2010-12-03 19:10:45 +08:00
|
|
|
# Generate data
|
2017-06-03 05:36:33 +08:00
|
|
|
try: # SciPy >= 0.16 have face in misc
|
|
|
|
|
from scipy.misc import face
|
|
|
|
|
face = face(gray=True)
|
|
|
|
|
except ImportError:
|
2015-11-26 04:50:10 +08:00
|
|
|
face = sp.face(gray=True)
|
|
|
|
|
|
|
|
|
|
# Resize it to 10% of the original size to speed up the processing
|
|
|
|
|
face = sp.misc.imresize(face, 0.10) / 255.
|
|
|
|
|
|
|
|
|
|
X = np.reshape(face, (-1, 1))
|
2010-12-03 19:10:45 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2011-02-28 23:58:00 +08:00
|
|
|
# Define the structure A of the data. Pixels connected to their neighbors.
|
2015-11-26 04:50:10 +08:00
|
|
|
connectivity = grid_to_graph(*face.shape)
|
2010-12-03 19:10:45 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2010-12-03 19:10:45 +08:00
|
|
|
# Compute clustering
|
2013-02-01 22:04:03 +08:00
|
|
|
print("Compute structured hierarchical clustering...")
|
2010-12-03 19:10:45 +08:00
|
|
|
st = time.time()
|
2011-12-24 02:12:26 +08:00
|
|
|
n_clusters = 15 # number of regions
|
2015-11-26 04:50:10 +08:00
|
|
|
ward = AgglomerativeClustering(n_clusters=n_clusters, linkage='ward',
|
|
|
|
|
connectivity=connectivity)
|
|
|
|
|
ward.fit(X)
|
|
|
|
|
label = np.reshape(ward.labels_, face.shape)
|
2013-02-01 22:04:03 +08:00
|
|
|
print("Elapsed time: ", time.time() - st)
|
|
|
|
|
print("Number of pixels: ", label.size)
|
|
|
|
|
print("Number of clusters: ", np.unique(label).size)
|
2010-12-03 19:10:45 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2011-01-03 00:44:25 +08:00
|
|
|
# Plot the results on an image
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.figure(figsize=(5, 5))
|
2015-11-26 04:50:10 +08:00
|
|
|
plt.imshow(face, cmap=plt.cm.gray)
|
2011-01-26 02:16:14 +08:00
|
|
|
for l in range(n_clusters):
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.contour(label == l, contours=1,
|
2014-05-15 10:35:13 +08:00
|
|
|
colors=[plt.cm.spectral(l / float(n_clusters)), ])
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.xticks(())
|
|
|
|
|
plt.yticks(())
|
|
|
|
|
plt.show()
|