ML之Hierarchical clustering:利用层次聚类算法来把100张图片自动分成红绿蓝三种色调
生活随笔
收集整理的這篇文章主要介紹了
ML之Hierarchical clustering:利用层次聚类算法来把100张图片自动分成红绿蓝三种色调
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
ML之Hierarchical clustering:利用層次聚類算法來把100張圖片自動(dòng)分成紅綠藍(lán)三種色調(diào)
?
?
目錄
輸出結(jié)果
實(shí)現(xiàn)代碼
?
?
?
?
輸出結(jié)果
?
?
實(shí)現(xiàn)代碼
#!/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 osdef drawdendrogram(clust, imlist, jpeg= 'clusters.jpg'): h = getheight(clust)*20 w = 1200depth = 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)*20h2 = getheight(clust.right)*20top = y - (h1 + h2)/2bottom = y + (h1 + h2)/2ll = clust.distance * scalingdraw.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.sizeprint (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') #?
?
相關(guān)文章
ML之H-clustering:自定義HierarchicalClustering層次聚類算法
ML之H-clustering:利用層次聚類算法來把100張圖片自動(dòng)分成紅綠藍(lán)三種色調(diào)
?
總結(jié)
以上是生活随笔為你收集整理的ML之Hierarchical clustering:利用层次聚类算法来把100张图片自动分成红绿蓝三种色调的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ML之HierarchicalClust
- 下一篇: Crawler:爬虫基于urllib.r