基于图像分割与颜色量化的 k 均值聚类算法

发布于:2022-12-29 ⋅ 阅读:(182) ⋅ 点赞:(0)

基于图像分割与颜色量化的 k 均值聚类算法

将演示如何对 Astronaut图像执行像素矢量量化(Vector Quantization,VQ),将显示图像所需的颜色数量从 250 种减少到 4 种,同时保持整体外观质量。在本例中,像素在三维空间中表示,使用 k 均值查找 4 个颜色簇。在图像处理文献中,码本是从 k 均值(簇群中心)获得的,称为调色板。在调色板中,使用 1 个字节最多可寻址 256 种颜色,而 RGB 编码要求每个像素 3 个字节。GIF 文件格式使用的就是这样的调色板。为了进行比较,我们还将使用随机码本(随机选取颜色)的量化图像。在使用 k 均值聚类算法对图像进行分割前,加载所需的库和输入图像,如下面的代码所示:

#Kmens 图像分割颜色聚类
import numpy as np 
import matplotlib.pyplot as plt 
from sklearn.cluster import KMeans 
from sklearn.metrics import pairwise_distances_argmin 
from skimage.io import imread 
from sklearn.utils import shuffle 
from skimage import img_as_float 
from time import time 
pepper = data.astronaut() 
# Display the original image 
plt.figure(1), plt.clf() 
ax = plt.axes([0, 0, 1, 1]) 
plt.axis('off'), plt.title('Original image (%d colors)' %(len(np.unique(pepper)))), plt.imshow(pepper) 
n_colors = 64 
# Convert to floats instead of the default 8 bits integer coding. Dividing by 
# 255 is important so that plt.imshow behaves works well on float data (need to 
# be in the range [0-1]) 
pepper = np.array(pepper, dtype=np.float64) / 255 
# Load Image and transform to a 2D numpy array. 
w, h, d = original_shape = tuple(pepper.shape) 
# assert d == 3 
image_array = np.reshape(pepper, (np.multiply(pepper.shape[0] , pepper.shape[1]), pepper.shape[2])) 
# image_gray=np.reshape(pepper,(np.multiply(w,h),d)
# image_gray.shape
def recreate_image(codebook, labels, w, h): 
     """Recreate the (compressed) image from the code book & labels""" 
     d = codebook.shape[1] 
     image = np.zeros((w, h, d)) 
     label_idx = 0 
     for i in range(w): 
         for j in range(h): 
             image[i][j] = codebook[labels[label_idx]] 
             label_idx += 1 
     return image 
    # Display all results, alongside original image 
plt.figure(1) 
plt.clf() 
ax = plt.axes([0, 0, 1, 1]) 
plt.axis('off') 
plt.title('Original image (96,615 colors)') 
plt.imshow(pepper)
plt.figure(2, figsize=(10,10)) 
plt.clf() 
i = 1 
for k in [64, 32, 16, 4]: 
     t0 = time() 
     plt.subplot(2,2,i) 
     plt.axis('off') 
     image_array_sample = shuffle(image_array, random_state=0)[:1000,:] 

     kmeans = KMeans(n_clusters=k, random_state=0).fit(image_array_sample) 
     print("done in %0.3fs." % (time() - t0)) 
     # Get labels for all points 
     print("Predicting color indices on the full image (k-means)") 
     t0 = time() 
     labels = kmeans.predict(image_array) 
     print("done in %0.3fs." % (time() - t0)) 
     plt.title('Quantized image (' + str(k) + ' colors, K-Means)') 
     plt.imshow(recreate_image(kmeans.cluster_centers_, labels, w, h)) 
     i += 1 
plt.show() 
plt.figure(3, figsize=(10,10)) 
plt.clf() 
i = 1 
for k in [64, 32, 16, 4]: 
     t0 = time() 
     plt.subplot(2,2,i) 
     plt.axis('off') 
     codebook_random = shuffle(image_array, random_state=0)[:k + 1] 
     print("Predicting color indices on the full image (random)") 
     t0 = time() 
     labels_random = pairwise_distances_argmin(codebook_random,image_array,axis=0) 
     print("done in %0.3fs." % (time() - t0)) 
     plt.title('Quantized image (' + str(k) + ' colors, Random)') 
     plt.imshow(recreate_image(codebook_random, labels_random, w, h)) 
     i += 1 
plt.show() 

运行上述代码,输出结果上图 所示。可以看到,在保留的图像质量方面,k 均值聚类算法对于颜色量化的效果总是比使用随机码本要好。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到