2010-12-03 19:10:45 +08:00
|
|
|
"""
|
2011-01-23 06:37:20 +08:00
|
|
|
===============================================================
|
|
|
|
|
A demo of structured Ward hierarchical clustering on Lena 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
|
|
|
|
|
import numpy as np
|
|
|
|
|
import scipy as sp
|
|
|
|
|
import pylab as pl
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.feature_extraction.image import grid_to_graph
|
|
|
|
|
from sklearn.cluster import Ward
|
2010-12-03 19:10:45 +08:00
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
# Generate data
|
2011-09-03 15:08:02 +08:00
|
|
|
lena = sp.misc.lena()
|
2010-12-03 19:10:45 +08:00
|
|
|
# Downsample the image by a factor of 4
|
|
|
|
|
lena = lena[::2, ::2] + lena[1::2, ::2] + lena[::2, 1::2] + lena[1::2, 1::2]
|
2011-07-10 20:07:58 +08:00
|
|
|
X = np.reshape(lena, (-1, 1))
|
2010-12-03 19:10:45 +08:00
|
|
|
|
|
|
|
|
###############################################################################
|
2011-02-28 23:58:00 +08:00
|
|
|
# Define the structure A of the data. Pixels connected to their neighbors.
|
2011-02-26 04:37:41 +08:00
|
|
|
connectivity = grid_to_graph(*lena.shape)
|
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
|
2011-09-01 17:20:05 +08:00
|
|
|
ward = Ward(n_clusters=n_clusters, connectivity=connectivity).fit(X)
|
2011-07-10 20:07:58 +08:00
|
|
|
label = np.reshape(ward.labels_, lena.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
|
|
|
|
|
|
|
|
###############################################################################
|
2011-01-03 00:44:25 +08:00
|
|
|
# Plot the results on an image
|
|
|
|
|
pl.figure(figsize=(5, 5))
|
2011-01-23 06:37:20 +08:00
|
|
|
pl.imshow(lena, cmap=pl.cm.gray)
|
2011-01-26 02:16:14 +08:00
|
|
|
for l in range(n_clusters):
|
2011-01-03 00:44:25 +08:00
|
|
|
pl.contour(label == l, contours=1,
|
2012-12-25 20:16:05 +08:00
|
|
|
colors=[pl.cm.spectral(l / float(n_clusters)), ])
|
2011-01-03 00:44:25 +08:00
|
|
|
pl.xticks(())
|
|
|
|
|
pl.yticks(())
|
2010-12-03 19:10:45 +08:00
|
|
|
pl.show()
|