#!/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的更多相关文章

  1. Python爬虫技术(从网页获取图片)+HierarchicalClustering层次聚类算法,实现自动从网页获取图片然后根据图片色调自动分类—Jason niu

    网上教程太啰嗦,本人最讨厌一大堆没用的废话,直接上,就是干! 网络爬虫?非监督学习? 只有两步,只有两个步骤? Are you kidding me? Are you ok? 来吧,follow me ...

  2. HierarchicalClustering:编写HierarchicalClustering层次聚类算法—Jason niu

    from numpy import * class cluster_node: def __init__(self,vec,left=None,right=None,distance=0.0,id=N ...

  3. 【机器学习算法-python实现】协同过滤(cf)的三种方法实现

    (转载请注明出处:http://blog.csdn.net/buptgshengod) 1.背景       协同过滤(collaborative filtering)是推荐系统经常使用的一种方法.c ...

  4. Python-层次聚类-Hierarchical clustering

    层次聚类关键方法#coding:UTF-8#Hierarchical clustering 层次聚类from E_distance import Euclidean_distance from yez ...

  5. 【Python机器学习实战】聚类算法(2)——层次聚类(HAC)和DBSCAN

    层次聚类和DBSCAN 前面说到K-means聚类算法,K-Means聚类是一种分散性聚类算法,本节主要是基于数据结构的聚类算法--层次聚类和基于密度的聚类算法--DBSCAN两种算法. 1.层次聚类 ...

  6. 机器学习算法总结(五)——聚类算法(K-means,密度聚类,层次聚类)

    本文介绍无监督学习算法,无监督学习是在样本的标签未知的情况下,根据样本的内在规律对样本进行分类,常见的无监督学习就是聚类算法. 在监督学习中我们常根据模型的误差来衡量模型的好坏,通过优化损失函数来改善 ...

  7. AP聚类算法(Affinity propagation Clustering Algorithm )

    AP聚类算法是基于数据点间的"信息传递"的一种聚类算法.与k-均值算法或k中心点算法不同,AP算法不需要在运行算法之前确定聚类的个数.AP算法寻找的"examplars& ...

  8. 挑子学习笔记:两步聚类算法(TwoStep Cluster Algorithm)——改进的BIRCH算法

    转载请标明出处:http://www.cnblogs.com/tiaozistudy/p/twostep_cluster_algorithm.html 两步聚类算法是在SPSS Modeler中使用的 ...

  9. ML: 聚类算法-概论

    聚类分析是一种重要的人类行为,早在孩提时代,一个人就通过不断改进下意识中的聚类模式来学会如何区分猫狗.动物植物.目前在许多领域都得到了广泛的研究和成功的应用,如用于模式识别.数据分析.图像处理.市场研 ...

随机推荐

  1. mongoDB 文档操作_改

    mongoDB 更改操作 格式对比 MySQL update table set .... where .... db.collection.updateOne(query,update,upsert ...

  2. LOJ #2135. 「ZJOI2015」幻想乡战略游戏(点分树)

    题意 给你一颗 \(n\) 个点的树,每个点的度数不超过 \(20\) ,有 \(q\) 次修改点权的操作. 需要动态维护带权重心,也就是找到一个点 \(v\) 使得 \(\displaystyle ...

  3. [python]python3.7中文手册

    https://pythoncaff.com/docs/tutorial/3.7.0

  4. 洛谷P1108 低价购买题解

    看到"你必须用低于你上次购买它的价格购买它",有没有想到什么?没错,又是LIS,倒过来的LIS,所以我们只要把读入的序列倒过来就可以求LIS了,第一问解决. 首先要厘清的是,对于这 ...

  5. java-查看java源码

    安装jdk后,自己的pc下自然而然就可以找到java的源码包.

  6. 2018-2019-2 20175209 实验一《Java开发环境的熟悉》实验报告

    2018-2019-2 20175209 实验一<Java开发环境的熟悉>实验报告 一.实验内容及步骤 1.使用JDK编译.运行简单的Java程序 cd 20175209进入2017520 ...

  7. IMPLEMENTING A GRU/LSTM RNN WITH PYTHON AND THEANO - 学习笔记

    catalogue . 引言 . LSTM NETWORKS . LSTM 的变体 . GRUs (Gated Recurrent Units) . IMPLEMENTATION GRUs 0. 引言 ...

  8. <锋利的jQuery>读书笔记

  9. JDBC Template

    JDBC Template 1. Spring JDBC Spring框架对JDBC的简单封装,提供了一个JDBCTemplate对象用来简化JDBC的开发 步骤: 导入jar包 创建JDBCTemp ...

  10. MyEclipse编码方式设置

    1.windows -> Preferences -> general -> Workspace: