【python】Scipy中的聚类算法

1. 安装:pip install scipy 2. 在jupyter中进行聚类示例 import numpy as npfrom scipy.clust

1. 安装:pip install scipy

2. 在jupyter中进行聚类示例

import numpy as np
from scipy.cluster.vq import vq, kmeans, whiten
import matplotlib.pyplot as pltfe = 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)
print("distortion: ", distortion)plt.scatter(fe[:,0], fe[:,1], c='g')
plt.scatter(codebook[:, 0], codebook[:, 1], c='r')
plt.show()

得出结果

3. 小图片进行聚类 

用PIL生成小尺寸的图片,在小图片上聚类

用resize或者thumbnail(缩略图)

import os
from PIL import Image
import matplotlib.pyplot as pltos.chdir(r'/Users/liruiying/Documents/pythonclass2021/lesson5')
print(os.getcwd())
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.sizeprint(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)

4. 对色彩进行聚类 

#对色彩聚类
import numpy as np
from scipy.cluster.vq import vq, kmeans, whiten
import matplotlib.pyplot as pltpoints=colorz('girl.png',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)  #变为色彩,还得转为整数
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()

 

 

 5. 导入pycharm

将上列代码合并成ImageColor.py

import numpy as np
from PIL import Image
from scipy.cluster.vq import vq, kmeans, whiten
def colorz(filename,n=3):img=Image.open(filename)img=img.rotate(-90)img.thumbnail((200,200))w,h=img.sizeprint(w,h)print('w*h=',w*h)points=[]for count,color in img.getcolors(w*h):points.append(color)return points
def kmeansColor(img,n):points=colorz(img,3)fe = np.array(points,dtype=float)codebook, distortion = kmeans(fe,n)centers=np.array(codebook,dtype=int)return centers

6. 修改main.py

修改一:

from flask import Flask,render_template,request
#增加request
import os
import cv2
import imageColor
#导入imageColor

修改二:

@app.route('/')
def index():#return "Hi,Flask!"#genFrame()picname=request.args.get("picname", type=str)if not picname:picname='static/pic/image0.jpg'pic='static/pic/image'framecount=825#imgcolors=imageColor.kmeansColor('static/pic/image0.jpg',5)imgcolors = imageColor.kmeansColor(picname, 5)return render_template('index.html',pic1=pic,framecount=framecount,imgcolors=imgcolors)

7. 修改index.html

增加以下代码


帧数:{{framecount}}
{{imgcolors}}
{% for c in imgcolors %} 宣传片 {% endfor %}
{% for i in range(framecount) %} {% endfor %}

8. 运行main.py得出结果

点击每个图片,“宣传片”的颜色会根据图片颜色进行变化。