Pythonデータ分析とマシン学習-Kmeansを使って画像圧縮を行います.


ソースのダウンロード:
http://download.csdn.net/download/adam_zs/10194644
from skimage import io
from sklearn.cluster import KMeans
import numpy as np

image = io.imread("test2.jpg")
io.imshow(image)
io.show()

# print(image.shape) #(154, 160, 3)      3     

rows = image.shape[0]
cols = image.shape[1]

image = image.reshape(image.shape[0] * image.shape[1], 3)
kmeans = KMeans(n_clusters=128)  #      128  
kmeans.fit(image)

labels = np.asarray(kmeans.labels_, dtype=np.uint8)  # labels_       
labels = labels.reshape(rows, cols)
io.imsave('compressed_test.jpg', labels)

image = io.imread("compressed_test.jpg")
io.imshow(image)
io.show()
Python数据分析与机器学习-使用Kmeans进行图像压缩_第1张图片
Python数据分析与机器学习-使用Kmeans进行图像压缩_第2张图片