2012-03-02 00:16:41 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
=========================================================
|
|
|
|
|
Vector Quantization Example
|
|
|
|
|
=========================================================
|
2013-06-06 18:30:42 +08:00
|
|
|
|
2015-11-26 04:50:10 +08:00
|
|
|
Face, a 1024 x 768 size image of a raccoon face,
|
|
|
|
|
is used here to illustrate how `k`-means is
|
|
|
|
|
used for vector quantization.
|
2012-03-02 00:16:41 +08:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
2013-07-30 18:41:56 +08:00
|
|
|
# Code source: Gaël Varoquaux
|
|
|
|
|
# Modified for documentation by Jaques Grobler
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2012-03-02 00:16:41 +08:00
|
|
|
|
2011-12-18 19:39:53 +08:00
|
|
|
import numpy as np
|
|
|
|
|
import scipy as sp
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2012-03-07 06:40:49 +08:00
|
|
|
from sklearn import cluster
|
2011-12-18 19:39:53 +08:00
|
|
|
|
|
|
|
|
|
2017-06-03 05:36:33 +08:00
|
|
|
try: # SciPy >= 0.16 have face in misc
|
|
|
|
|
from scipy.misc import face
|
2021-10-07 16:13:00 +08:00
|
|
|
|
2017-06-03 05:36:33 +08:00
|
|
|
face = face(gray=True)
|
|
|
|
|
except ImportError:
|
2015-11-26 04:50:10 +08:00
|
|
|
face = sp.face(gray=True)
|
|
|
|
|
|
|
|
|
|
n_clusters = 5
|
|
|
|
|
np.random.seed(0)
|
2017-06-03 05:36:33 +08:00
|
|
|
|
2015-11-26 04:50:10 +08:00
|
|
|
X = face.reshape((-1, 1)) # We need an (n_sample, n_feature) array
|
2012-05-16 06:09:47 +08:00
|
|
|
k_means = cluster.KMeans(n_clusters=n_clusters, n_init=4)
|
2011-12-18 19:39:53 +08:00
|
|
|
k_means.fit(X)
|
|
|
|
|
values = k_means.cluster_centers_.squeeze()
|
|
|
|
|
labels = k_means.labels_
|
2012-03-02 00:16:41 +08:00
|
|
|
|
|
|
|
|
# create an array from labels and values
|
2015-11-26 04:50:10 +08:00
|
|
|
face_compressed = np.choose(labels, values)
|
|
|
|
|
face_compressed.shape = face.shape
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2015-11-26 04:50:10 +08:00
|
|
|
vmin = face.min()
|
|
|
|
|
vmax = face.max()
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2015-11-26 04:50:10 +08:00
|
|
|
# original face
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.figure(1, figsize=(3, 2.2))
|
2015-11-26 04:50:10 +08:00
|
|
|
plt.imshow(face, cmap=plt.cm.gray, vmin=vmin, vmax=256)
|
2012-03-02 00:16:41 +08:00
|
|
|
|
2015-11-26 04:50:10 +08:00
|
|
|
# compressed face
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.figure(2, figsize=(3, 2.2))
|
2015-11-26 04:50:10 +08:00
|
|
|
plt.imshow(face_compressed, cmap=plt.cm.gray, vmin=vmin, vmax=vmax)
|
2012-03-02 00:16:41 +08:00
|
|
|
|
2015-11-26 04:50:10 +08:00
|
|
|
# equal bins face
|
2012-04-28 18:04:36 +08:00
|
|
|
regular_values = np.linspace(0, 256, n_clusters + 1)
|
2015-11-26 04:50:10 +08:00
|
|
|
regular_labels = np.searchsorted(regular_values, face) - 1
|
2012-04-28 18:04:36 +08:00
|
|
|
regular_values = 0.5 * (regular_values[1:] + regular_values[:-1]) # mean
|
2015-11-26 04:50:10 +08:00
|
|
|
regular_face = np.choose(regular_labels.ravel(), regular_values, mode="clip")
|
|
|
|
|
regular_face.shape = face.shape
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.figure(3, figsize=(3, 2.2))
|
2015-11-26 04:50:10 +08:00
|
|
|
plt.imshow(regular_face, cmap=plt.cm.gray, vmin=vmin, vmax=vmax)
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2012-03-02 00:16:41 +08:00
|
|
|
# histogram
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.figure(4, figsize=(3, 2.2))
|
|
|
|
|
plt.clf()
|
|
|
|
|
plt.axes([0.01, 0.01, 0.98, 0.98])
|
|
|
|
|
plt.hist(X, bins=256, color=".5", edgecolor=".5")
|
|
|
|
|
plt.yticks(())
|
|
|
|
|
plt.xticks(regular_values)
|
2011-12-18 19:39:53 +08:00
|
|
|
values = np.sort(values)
|
|
|
|
|
for center_1, center_2 in zip(values[:-1], values[1:]):
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.axvline(0.5 * (center_1 + center_2), color="b")
|
2011-12-18 19:39:53 +08:00
|
|
|
|
|
|
|
|
for center_1, center_2 in zip(regular_values[:-1], regular_values[1:]):
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.axvline(0.5 * (center_1 + center_2), color="b", linestyle="--")
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|