我正在使用 K 均值聚類算法執(zhí)行圖像壓縮。壓縮后得到的圖像是灰度圖像,如何獲得與原始圖像質(zhì)量相近的彩色圖像?import osfrom skimage import iofrom sklearn.cluster import MiniBatchKMeansimport numpy as npalgorithm = "full"for f in os.listdir('.'): if f.endswith('.png'): image = io.imread(f) rows = image.shape[0] cols = image.shape[1] image = image.reshape(image.shape[0] * image.shape[1], image.shape[2]) kmeans = MiniBatchKMeans(n_clusters=128, n_init=10, max_iter=200) kmeans.fit(image) clusters = np.asarray(kmeans.cluster_centers_, dtype=np.uint8) labels = np.asarray(kmeans.labels_, dtype=np.uint8) labels = labels.reshape(rows, cols); # np.save('codebook'+f+'.npy', clusters) io.imsave('compressed_' + f , labels);
圖像壓縮后得到的灰度圖像
繁華開滿天機(jī)
2021-07-02 14:07:36