1
from sklearn.datasets import load_sample_imagefrom sklearn.cluster import KMeansimport matplotlib.pyplot as pltchina = load_sample_image("china.jpg")plt.imshow(china)plt.show()print(china.shape)2
image = china[::3,::3] #行列分别按step为3的距离取x = image.reshape(-1,3) #生成行数自填充,列数为3的二维数组plt.imshow(image)plt.show()print(image.shape,x.shape) n_color = 64model = KMeans(n_color)labels = model.fit_predict(x) #每个点的颜色分类color = model.cluster_centers_ #64个聚类中心,颜色值 color[labels]images = image.reshape(143, 214, 3)print(images.shape)plt.imshow(images)plt.show()
浙公网安备 33010602011771号