A series that implements Coursera's Machine Learning Matlab / Octave programming tasks in Python. This time we will do the first task of unsupervised learning, K-means clustering.
The original image (.png) has 256 gradations for each of RGB and has 16.77 million colors. The challenge is to classify this color information into 16 clusters by K-means clustering and create a 16-color image.
A library called pillow is used for image processing, so install it.
pip install pillow
The pillow documentation is here [http://pillow.readthedocs.org/en/3.0.x/).
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
Use PIL.Image.open ()
to load the image data and load it into a 128 x 128 x 3 3D numpy array. This is reshaped (128 * 128, 3)
and flattened into a 16384 x 3 2D matrix that can be passed to clustering.
img = np.array(Image.open('bird_small.png')) #img is a 128x128x3 3D matrix
img_flat = img.reshape(128*128,3)/255 #Convert to a 16384x3 2D matrix
Clustering can be done in one shot KMeans.fit (data)
. The KMeans parameter specifies n_clusters = 16
to divide into 16 clusters.
model = KMeans(n_clusters=16).fit(img_flat)
As a result of clustering, which cluster each pixel was classified into is stored in model.labels_
(number 0-15), and the value of the center of gravity of each cluster is stored in model.cluster_centers_
(16x3 matrix).
You can use this to create a matrix with the value of each pixel replaced by the centroid of the cluster as model.cluster_centers_ [model.labels_]
(16384 x 3 2D matrix). Reshape (128, 128, 3)
to return this to a 128 x 128 x 3 3D matrix that can be displayed in the image.
img_comp = model.cluster_centers_[model.labels_].reshape(128,128,3)
plt.imshow(img_comp)
Click here for the original image and the resulting image. You can see that it has been reduced to 16 colors.
The full code is here. It's short.
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
img = np.array(Image.open('bird_small.png')) #img is a 128x128x3 3D matrix
img_flat = img.reshape(128*128,3)/255 #Convert to a 16384x3 2D matrix
model = KMeans(n_clusters=16).fit(img_flat)
img_comp = model.cluster_centers_[model.labels_].reshape(128,128,3)
plt.imshow(img_comp)
When I was a student studying image processing 10 years ago, I implemented K-means clustering in C ++ by scratch, but it seems that it was a program with hundreds of lines ... It's a good time with the library in place.
Recommended Posts