聚类应用,图像压缩 ---kmeans
import matplotlib.pyplot as plt # plt 用于显示图片
import matplotlib.image as mpimg # mpimg 用于读取图片
from sklearn.cluster import KMeans
import numpy as np
pixel = mpimg.imread(‘lena_cor.jpg’)
pixel = pixel.reshape((512*512 , 3)) #转成二维数组

kmeans = KMeans(n_clusters=16, random_state=0).fit(pixel)#聚类

newPixel = []
for i in kmeans.labels_:
newPixel.append(list(kmeans.cluster_centers_[i,:]))

newPixel = np.array(newPixel)
newPixel = newPixel.reshape((512,512,3))
newPixel=newPixel.astype(np.uint8) #转成无符号uint8
mpimg.imsave(‘compressed_lena.jpg’,newPixel)

聚类应用,图像压缩 ---kmeans

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-14
  • 2021-09-19
  • 2022-12-23
  • 2021-11-20
猜你喜欢
  • 2021-08-02
  • 2022-12-23
  • 2021-12-18
  • 2021-04-23
  • 2022-01-30
  • 2022-12-23
相关资源
相似解决方案