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


歌曲刻苦
歌曲刻苦 2022-09-20 10:09:28 52364
分类专栏: 资讯

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张图片自动分成红绿蓝三种色调
 

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:https://www.xckfsq.com/news/show.html?id=4051
赞同 0
评论 0 条
歌曲刻苦L0
粉丝 0 发表 15 + 关注 私信
上周热门
如何使用 StarRocks 管理和优化数据湖中的数据?  2951
【软件正版化】软件正版化工作要点  2872
统信UOS试玩黑神话:悟空  2833
信刻光盘安全隔离与信息交换系统  2728
镜舟科技与中启乘数科技达成战略合作,共筑数据服务新生态  1261
grub引导程序无法找到指定设备和分区  1226
华为全联接大会2024丨软通动力分论坛精彩议程抢先看!  165
2024海洋能源产业融合发展论坛暨博览会同期活动-海洋能源与数字化智能化论坛成功举办  163
点击报名 | 京东2025校招进校行程预告  163
华为纯血鸿蒙正式版9月底见!但Mate 70的内情还得接着挖...  159
本周热议
我的信创开放社区兼职赚钱历程 40
今天你签到了吗? 27
如何玩转信创开放社区—从小白进阶到专家 15
信创开放社区邀请他人注册的具体步骤如下 15
方德桌面操作系统 14
用抖音玩法闯信创开放社区——用平台宣传企业产品服务 13
我有15积分有什么用? 13
如何让你先人一步获得悬赏问题信息?(创作者必看) 12
2024中国信创产业发展大会暨中国信息科技创新与应用博览会 9
中央国家机关政府采购中心:应当将CPU、操作系统符合安全可靠测评要求纳入采购需求 8

加入交流群

请使用微信扫一扫!