ML之Hierarchical clustering:利用层次聚类算法来把100张图片自动分成红绿蓝三种色调

举报
一个处女座的程序猿 发表于 2021/03/28 01:43:48 2021/03/28
【摘要】 ML之Hierarchical clustering:利用层次聚类算法来把100张图片自动分成红绿蓝三种色调     目录 输出结果 实现代码         输出结果     实现代码 #!/usr/bin/python# coding:utf-8from PI...

ML之Hierarchical clustering:利用层次聚类算法来把100张图片自动分成红绿蓝三种色调

 

 

目录

输出结果

实现代码


 

 

 

 

输出结果

 

 

实现代码


  
  1. #!/usr/bin/python
  2. # coding:utf-8
  3. from PIL import Image, ImageDraw
  4. from HierarchicalClustering import hcluster
  5. from HierarchicalClustering import getheight
  6. from HierarchicalClustering import getdepth
  7. import numpy as np
  8. import os
  9. def drawdendrogram(clust, imlist, jpeg= 'clusters.jpg'):
  10. h = getheight(clust)*20
  11. w = 1200
  12. depth = getdepth(clust)
  13. scaling = float(w - 150)/depth
  14. img = Image.new('RGB', (w, h), (255, 255, 255))
  15. draw = ImageDraw.Draw(img)
  16. draw.line((0, h/2, 10, h/2), fill=(255, 0, 0))
  17. drawnode(draw, clust, 10, int(h/2), scaling, imlist, img)
  18. img.save(jpeg)
  19. def drawnode(draw,clust,x,y,scaling,imlist,img): if clust.id < 0:
  20. h1 = getheight(clust.left)*20
  21. h2 = getheight(clust.right)*20
  22. top = y - (h1 + h2)/2
  23. bottom = y + (h1 + h2)/2
  24. ll = clust.distance * scaling
  25. draw.line((x, top + h1/2, x, bottom - h2/2), fill=(255, 0, 0))
  26. draw.line((x, top + h1/2, x + ll, top + h1/2), fill=(255, 0, 0))
  27. draw.line((x, bottom - h2/2, x + ll, bottom - h2/2), fill=(255, 0, 0))
  28. drawnode(draw, clust.left, x + ll, top + h1/2, scaling, imlist, img)
  29. drawnode(draw, clust.right, x + ll, bottom - h2/2, scaling, imlist, img)
  30. else:
  31. nodeim = Image.open(imlist[clust.id])
  32. nodeim.thumbnail((20, 20))
  33. ns = nodeim.size
  34. print (x,y - ns[1]//2)
  35. print (x + ns[0])
  36. print (img.paste(nodeim, (int(x), int(y - ns[1]//2), int(x + ns[0]),int(y + ns[1] - ns[1]//2))))
  37. imlist=[]
  38. folderpath = r'F:\File_Python\Crawler'
  39. for filename in os.listdir(folderpath):
  40. if os.path.splitext(filename)[1]=='.jpg':
  41. imlist.append(os.path.join(folderpath,filename))
  42. n=len(imlist)
  43. print(n)
  44. features =np.zeros((n,3))
  45. for i in range(n):
  46. im=np.array(Image.open(imlist[i]))
  47. R = np.mean(im[:,:,0].flatten())
  48. G = np.mean(im[:,:,1].flatten())
  49. B = np.mean(im[:,:,2].flatten())
  50. features[i]=np.array([R,G,B])
  51. tree = hcluster(features)
  52. drawdendrogram(tree, imlist, jpeg=r'C:\Users\99386\Desktop\result.jpg') #

 

 


相关文章
ML之H-clustering:自定义HierarchicalClustering层次聚类算法
ML之H-clustering:利用层次聚类算法来把100张图片自动分成红绿蓝三种色调
 

文章来源: yunyaniu.blog.csdn.net,作者:一个处女座的程序猿,版权归原作者所有,如需转载,请联系作者。

原文链接:yunyaniu.blog.csdn.net/article/details/80020614

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。