一、Scipy聚类kmeans
SciPy is an open-source software for mathematics, science, and engineering.
聚类实例:
import numpy as np
from scipy.cluster.vq import vq, kmeans, whiten
import matplotlib.pyplot as plt
fe=np.array([[1.9,2.0],
[1.7,2.5],
[1.6,3.1],
[0.1,0.1],
[0.8,0.3],
[0.4,0.3],
[0.22,0.1],
[0.4, 0.3],
[0.4,0.5],
[1.8,1.9]])
book=np.array((fe[0],fe[1]))
print(type(book))
print("book:\n",book)
codebook,distortion=kmeans(fe,book)
#可以写kmeans(wf,2),2表示两个质心,同时启用iter参数
print("codebook:",codebook) #codebook聚类中心
print("distortion: ", distortion) #distortion控制方差大小
plt.scatter(fe[:,0], fe[:,1], c='g') #scatter提供散点图
plt.scatter(codebook[:, 0], codebook[:, 1], c='r')
plt.show()
运行结果:红色的为聚类中心
二、对图像色彩聚类
1.用PIL生成小尺寸的图并提取色彩
①resize或者thumbnail生成缩略图
②getcolors()返回图像中的颜色列表
import os
from PIL import Image
import matplotlib.pyplot as plt
os.chdir(r'D:\python\Ads2021')
im=np.array(Image.open('girl.png'))
#用缩略图聚类
def colorz(filename,n=3):
img=Image.open(filename)
img=img.rotate(-90)
img.thumbnail((200,200))
w,h=img.size
print(w,h)
print('w*h=',w*h)
plt.axis('off')
plt.imshow(img)
plt.show()
points=[]
for count,color in img.getcolors(w*h):
points.append(color)
return points
colorz('girl.png',3)
2.取出图像的色彩和频次
import numpy as np
from scipy.cluster.vq import vq, kmeans, whiten
import matplotlib.pyplot as plt
points=colorz('girl.jpg',3)
print(points[0:10])
fe = np.array(points,dtype=float) #聚类需要是Float或者Double
print(fe[0:10])
book =np.array((fe[100],fe[1],fe[8],fe[8])) #聚类中心,初始值
print(type(book))
print("book: \n",book)
#codebook, distortion = kmeans(fe,book)
codebook, distortion = kmeans(fe,7) #7是聚类中心个数
# 可以写kmeans(wf,2), 2表示两个质心,同时启用iter参数
print("codebook:", codebook) #聚类中心
centers=np.array(codebook,dtype=int) #色彩的均值计算出来一般是浮点数,需要转化为整数int
print(centers)
print("distortion: ", distortion)
fe=np.array(points)
plt.scatter(fe[:,0], fe[:,2], c='b')
plt.scatter(codebook[:, 0], codebook[:,2], c='r') #聚类中心
plt.show()
运行结果:如下7个聚类中心