Hierarchical clustering:利用层次聚类算法来把100张图片自动分成红绿蓝三种色调—Jaosn niu
#!/usr/bin/python
# coding:utf-8
from PIL import Image, ImageDraw
from HierarchicalClustering import hcluster
from HierarchicalClustering import getheight
from HierarchicalClustering import getdepth
import numpy as np
import os def drawdendrogram(clust, imlist, jpeg= 'clusters.jpg'):
h = getheight(clust)*20
w = 1200
depth = getdepth(clust)
scaling = float(w - 150)/depth img = Image.new('RGB', (w, h), (255, 255, 255))
draw = ImageDraw.Draw(img) draw.line((0, h/2, 10, h/2), fill=(255, 0, 0))
drawnode(draw, clust, 10, int(h/2), scaling, imlist, img)
img.save(jpeg) def drawnode(draw,clust,x,y,scaling,imlist,img): if clust.id < 0:
h1 = getheight(clust.left)*20
h2 = getheight(clust.right)*20
top = y - (h1 + h2)/2
bottom = y + (h1 + h2)/2
ll = clust.distance * scaling
draw.line((x, top + h1/2, x, bottom - h2/2), fill=(255, 0, 0)) draw.line((x, top + h1/2, x + ll, top + h1/2), fill=(255, 0, 0)) draw.line((x, bottom - h2/2, x + ll, bottom - h2/2), fill=(255, 0, 0)) drawnode(draw, clust.left, x + ll, top + h1/2, scaling, imlist, img)
drawnode(draw, clust.right, x + ll, bottom - h2/2, scaling, imlist, img)
else:
nodeim = Image.open(imlist[clust.id])
nodeim.thumbnail((20, 20))
ns = nodeim.size
print (x,y - ns[1]//2)
print (x + ns[0])
print (img.paste(nodeim, (int(x), int(y - ns[1]//2), int(x + ns[0]),int(y + ns[1] - ns[1]//2)))) imlist=[]
folderpath = r'F:\File_Python\Crawler'
for filename in os.listdir(folderpath):
if os.path.splitext(filename)[1]=='.jpg':
imlist.append(os.path.join(folderpath,filename))
n=len(imlist)
print(n) features =np.zeros((n,3))
for i in range(n):
im=np.array(Image.open(imlist[i]))
R = np.mean(im[:,:,0].flatten())
G = np.mean(im[:,:,1].flatten())
B = np.mean(im[:,:,2].flatten())
features[i]=np.array([R,G,B]) tree = hcluster(features)
drawdendrogram(tree, imlist, jpeg=r'C:\Users\99386\Desktop\result.jpg') #
Hierarchical clustering:利用层次聚类算法来把100张图片自动分成红绿蓝三种色调—Jaosn niu的更多相关文章
- Python爬虫技术(从网页获取图片)+HierarchicalClustering层次聚类算法,实现自动从网页获取图片然后根据图片色调自动分类—Jason niu
网上教程太啰嗦,本人最讨厌一大堆没用的废话,直接上,就是干! 网络爬虫?非监督学习? 只有两步,只有两个步骤? Are you kidding me? Are you ok? 来吧,follow me ...
- HierarchicalClustering:编写HierarchicalClustering层次聚类算法—Jason niu
from numpy import * class cluster_node: def __init__(self,vec,left=None,right=None,distance=0.0,id=N ...
- 【机器学习算法-python实现】协同过滤(cf)的三种方法实现
(转载请注明出处:http://blog.csdn.net/buptgshengod) 1.背景 协同过滤(collaborative filtering)是推荐系统经常使用的一种方法.c ...
- Python-层次聚类-Hierarchical clustering
层次聚类关键方法#coding:UTF-8#Hierarchical clustering 层次聚类from E_distance import Euclidean_distance from yez ...
- 【Python机器学习实战】聚类算法(2)——层次聚类(HAC)和DBSCAN
层次聚类和DBSCAN 前面说到K-means聚类算法,K-Means聚类是一种分散性聚类算法,本节主要是基于数据结构的聚类算法--层次聚类和基于密度的聚类算法--DBSCAN两种算法. 1.层次聚类 ...
- 机器学习算法总结(五)——聚类算法(K-means,密度聚类,层次聚类)
本文介绍无监督学习算法,无监督学习是在样本的标签未知的情况下,根据样本的内在规律对样本进行分类,常见的无监督学习就是聚类算法. 在监督学习中我们常根据模型的误差来衡量模型的好坏,通过优化损失函数来改善 ...
- AP聚类算法(Affinity propagation Clustering Algorithm )
AP聚类算法是基于数据点间的"信息传递"的一种聚类算法.与k-均值算法或k中心点算法不同,AP算法不需要在运行算法之前确定聚类的个数.AP算法寻找的"examplars& ...
- 挑子学习笔记:两步聚类算法(TwoStep Cluster Algorithm)——改进的BIRCH算法
转载请标明出处:http://www.cnblogs.com/tiaozistudy/p/twostep_cluster_algorithm.html 两步聚类算法是在SPSS Modeler中使用的 ...
- ML: 聚类算法-概论
聚类分析是一种重要的人类行为,早在孩提时代,一个人就通过不断改进下意识中的聚类模式来学会如何区分猫狗.动物植物.目前在许多领域都得到了广泛的研究和成功的应用,如用于模式识别.数据分析.图像处理.市场研 ...
随机推荐
- 一丢丢学习之webpack4 + Vue单文件组件的应用
之前刚学了一些Vue的皮毛于是写了一个本地播放器https://github.com/liwenchi123000/Local-Music-Player,如果觉得ok的朋友可以给个star. 就是很简 ...
- 指针运算中的运算符:&和*
这里&是取地址运算符,*是间接运算符. &a 的运算结果是一个指针,指针的类型是 a 的类型加个 *,指针所指向的类型是 a 的类型,指针所指向的地址嘛,那就是 a 的地址. *p 的 ...
- linux串口编程设置(转载)
(转载)在嵌入式Linux中,串口是一个字设备,访问具体的串行端口的编程与读/写文件 的操作类似,只需打开相应的设备文件即可操作.串口编程特殊在于串 口通信时相关参数与属性的设置.嵌入式Linux的串 ...
- MQ服务器奔溃解决过程
1.MQ服务器崩溃调节: 今天具安卓前端反应, 从昨天下午开始线上服务器使用 电话号码登陆和 使用电话号码注册功能不能使用, 经过前端仔细排查怀疑是后端问题,之后经过与ios前端 确认, 定位为后端服 ...
- Linux(1)-卸载挂载分区
> df -lh > fuser -m -v /dev/mapper/autovg-autolv > fuser -m -k -v /dev/mapper/autovg-autolv ...
- Memcached介绍
Memcached介绍 Memcached是一种免费的.开源的.高性能的.分布式对象缓存系统,通过缓解数据库压力,来提高动态web页面的速度. Memcached是一种内存级别的键值对存储,用来存放数 ...
- solr面板的使用
创建数据库 先别着急点击Add Core,先去目录下创建几个文件. 去solrhome目录下创建一个文件夹,比如test,这个文件夹就是数据库文件夹.
- pymysql的使用及sql注入
pymysql简介 pymysql是python操纵mysql的一个模块,本质上是一个socket客户端 pymysql使用 准备数据 #创建数据库db2,如果已存在,请忽略 CREATE DATAB ...
- mysql数据库truncate表时间长处理
[环境介绍] 系统环境:Linux + mysql 5.7.18 + 主从复制架构 [背景描述] 客户反映用在mysql数据库上truncate一个innode引擎的list分区100G左右表时,耗时 ...
- QGE 在齐次 Besov 空间中的准则
在 [Zhang, Zujin. On the blow-up criterion for the quasi-geostrophic equations in homogeneous Besov s ...