K-means 是常见的聚类算法,主要思想是在集合中找到 K 个中心,以中心为基础把集合分为 k 个部分。
矢量量化算法可以看作基于 K-means 算法找出的 K 个中心点,把中心点周围的点的值用中心的值取代。
实现
用 Python 提供的库很容易可以将一副图像进行矢量量化。
#!/usr/bin/env python
from pylab import imread, imshow, figure, show, subplot
from numpy import reshape, uint8, flipud
from scipy.cluster.vq import kmeans,vq
from scipy import misc
import Image
quantization_layer = 16
img = imread('jet.jpg')
print img.shape
pixel = img
# performing the clustering
centroids,_ = kmeans(pixel, quantization_layer) # six colors will be found
# quantization
qnt,_ = vq(pixel,centroids)
centers_idx = qnt
clustered = centroids[centers_idx]
# display images
figure(1)
subplot(211)
imshow(img)
subplot(212)
imshow(clustered)
show()
参考
http://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.vq.kmeans.html
http://blog.sina.com.cn/s/blog_49b5f5080101bfwa.html
http://blog.csdn.net/superdont/article/details/46399041