In [1]:
%matplotlib inline

In [2]:
# goal is to blur an image with clustering

In [8]:
from scipy import ndimage
import matplotlib.pyplot as plt

In [9]:
img = ndimage.imread("headshot.jpg")

In [10]:
plt.imshow(img)


Out[10]:
<matplotlib.image.AxesImage at 0x109ec2950>

In [11]:
img.shape


Out[11]:
(420, 420, 3)

In [12]:
# to quantize an image we need to convert it to a 2 dimensional
# matrix with length being Height x Width of the image and the
# rows of the matrix being the RGB values

In [13]:
x,y,z = img.shape

In [14]:
long_img = img.reshape(x*y,z)

In [15]:
long_img.shape


Out[15]:
(176400, 3)

In [16]:
# we want 5 distinct colors so set n_clusters to 5
from sklearn import cluster

In [22]:
kmeans = cluster.KMeans(n_clusters=5)
kmeans.fit(long_img)


Out[22]:
KMeans(copy_x=True, init='k-means++', max_iter=300, n_clusters=5, n_init=10,
    n_jobs=1, precompute_distances='auto', random_state=None, tol=0.0001,
    verbose=0)

In [23]:
centers = kmeans.cluster_centers_

In [24]:
centers


Out[24]:
array([[  86.24652104,   68.83443147,   54.02602728],
       [ 194.36172037,  172.19594433,  149.65347344],
       [  24.64137471,   20.41987179,   16.16600025],
       [ 142.58522868,  206.12439004,  226.03920271],
       [ 149.27290093,  132.19386559,  115.32293566]])

In [25]:
labels = kmeans.labels_
labels[:5]


Out[25]:
array([0, 0, 0, 0, 0], dtype=int32)

In [26]:
plt.imshow(centers[labels].reshape(x,y,z))


Out[26]:
<matplotlib.image.AxesImage at 0x109d86590>

In [ ]: