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张图片

你可能感兴趣的:(python,Python数据分析与机器学习)