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: 聚类算法-概论
聚类分析是一种重要的人类行为,早在孩提时代,一个人就通过不断改进下意识中的聚类模式来学会如何区分猫狗.动物植物.目前在许多领域都得到了广泛的研究和成功的应用,如用于模式识别.数据分析.图像处理.市场研 ...
随机推荐
- 怎样在ISE14.7中固化FLASH文件
前言 当工程开发完成后,bit文件类型掉电后会消失,而此时采用FLASH固化就很重要了. 软件版本:ISE14.7 流程 1.对生成FLASH文件进行设置:配置速率为33,选择66貌似配置失败,中庸之 ...
- jzoj6101. 【GDOI2019模拟2019.4.2】Path
题目链接:https://jzoj.net/senior/#main/show/6101 记\(f_i\)为从\(i\)号点走到\(n\)号点所花天数的期望 那么根据\(m\)条边等可能的出现一条和一 ...
- python学习 day13 装饰器(一)&推导式
装饰器&推导式 传参位置参数在前,关键词参数在后 函数不被调用内部代码不被执行 函数在被调用的时候,每次都会开辟一个新的内存地址,互不干扰 #经典案例 def func(num): def i ...
- Angular6封装http请求
最近抽空学习了一下Angular6,之前主要使用的是vue,所以免不了的也想对Angular6提供的工具进行一些封装,今天主要就跟大家讲一下这个http模块. 之前使用的ajax库是axios,可以设 ...
- Oracle 获取前几行数据问题的陷阱
查询用户数据表,需要根据时间正序排序,然后获取时间最早的前三条数据,是不是第一印象想这么写: select * from users where rownum<4 order by datati ...
- redis简单命令总结
1.连接到redis服务器:redis-cli -h 127.0.0.1 -p 6379 -a 密码 select index 切换 redis 数据库 flushdb 删除当前数据库所有的 key ...
- 驱动调试(三)oops确定函数PC
目录 驱动调试(三)oops确定函数PC 什么是oops 流程简述 代码仓库 模块例子分析 找到PC值 判断是否属于模块 查看符号表 找到模块 反汇编模块 内核例子分析 找到PC值 判断是否属于模块 ...
- Linux记录-open-falcon开源监控系统部署
参考https://book.open-falcon.org/zh_0_2/quick_install/prepare.html一.安装后端1.环境准备yum -y install redisyum ...
- Windows Server 在IIS上创建安全网站
第一步.打开服务器管理器,创建用户,并设置密码,自己记录下来,注意勾选密码永不过期. 第二步.设置用户隶属组:IIS_IUSRS. 第三步.设置建立的网站文件夹权限,添加刚才建立的用户,并添加修改.读 ...
- Spring Security 之基本概念
Spring Security 是一个安全框架, 可以简单地认为 Spring Security 是放在用户和 Spring 应用之间的一个安全屏障, 每一个 web 请求都先要经过 Spring S ...