k mean

1.观察图片变化

from sklearn.datasets import load_sample_image

import  matplotlib.pyplot as plt
from  sklearn.cluster  import  KMeans
import  numpy as np
flower = load_sample_image( 'flower.jpg' )
plt.imshow(flower)
plt.show()
 
image = flower[:: 3 ,:: 3 ]
plt.imshow(image)
plt.show()
 
#压缩图片

import sys
sys.getsizeof(flower)

sys.getsizeof(load_sample_image)

import matplotlib.image as img
img.imsave("F://02.jpg",flower)
#img.imsave("F://03.jpg",load_sample_image)

 
 2.聚类
x = image.reshape( - 1 , 3 )
n_colors = 64
model = KMeans(n_colors)
labels = model.fit_predict(x)
colors = model.cluster_centers_
new_image = colors[labels]
new_image = new_image.reshape(image.shape)
plt.imshow(new_image.astype(np.uint8))
plt.show()
 

猜你喜欢

转载自www.cnblogs.com/chjh/p/9966474.html