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: 聚类算法-概论
聚类分析是一种重要的人类行为,早在孩提时代,一个人就通过不断改进下意识中的聚类模式来学会如何区分猫狗.动物植物.目前在许多领域都得到了广泛的研究和成功的应用,如用于模式识别.数据分析.图像处理.市场研 ...
随机推荐
- Codeforces 1079C Playing Piano(记忆化搜索)
题目链接:Playing Piano 题意:给定长度为n的序列$a_i$,要求得到同样长度的序列$b_i$.满足一下条件: if $a_i < a_{i+1}$ then $b_i < b ...
- centos7下使用LVM给系统硬盘扩容超详细
简单介绍: LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的 ...
- 【UVA1660】Cable TV Network
题目大意:给定一个 N 个点的无向图,求至少删去多少个点可以使得无向图不连通. 题解:学习到了点边转化思想. 根据网络流的知识可知,一个网络的最小割与网络的最大流相等.不过最小割是图的边集,而本题则是 ...
- IrisSkin 单独控件样式设置 不使用皮肤样式
可以设置控件的Tag为this.skinEngine1.DisableTag [DefaultValue()] [Description("If you do not want skin t ...
- busybox(三)最小根文件系统
目录 busybox(三)最小根文件系统 引入 构建终端 构造inittab 配置应用程序 构建C库 制作映像文件yaffs title: busybox(三)最小根文件系统 tag: arm dat ...
- CMDB资产管理系统开发【day25】:windows客户端开发
1.目录结构 PS Y:\MadkingClient> tree /f 卷 netgame 的文件夹 PATH 列表 卷序列号为 ACE3-896E Y:. ├─bin │ NedStark.p ...
- 关于设计项目UI界面的软件工具
关于画UI界面的软件,我在网上找了几个,今天式用这几款软件还可以 1.墨刀:国产的,这个专门画APP界面的,用起来比较简单,有免费版的,要注册才能用,提供云存储,收费版的云存储空间会多一些.网站: h ...
- I/O模型
目录: IO模型 阻塞IO 非阻塞IO IO多路复用 异步IO 总结: 1.阻塞IO模型 多线程 多进程 线程池 进程池 全是阻塞IO 2.非阻塞IO 协程是一种非阻塞IO 1.setblocking ...
- [Android] Android Java String 转Uri
Uri uri = Uri.parse("https://www.baidu.com") URI uri = new URI("https://www.baidu.com ...
- 02 Redis关闭服务报错---(error) ERR Errors trying to SHUTDOWN. Check logs.
127.0.0.1:6379> shutdown (error) ERR Errors trying to SHUTDOWN. Check logs. 1.在redis.conf中修改日志文件的 ...