python练习7 Scipy-kmeans聚类色彩提取

一、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个聚类中心

​​​​​​​


三、把色彩聚类加入到Flask框架里

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值