Pythonデータ分析とマシン学習-Kmeansを使って画像圧縮を行います.
1339 ワード
ソースのダウンロード:
http://download.csdn.net/download/adam_zs/10194644
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()