首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >分层凝聚聚类的实现

分层凝聚聚类的实现
EN

Stack Overflow用户
提问于 2013-12-25 01:32:57
回答 1查看 1.3K关注 0票数 0

我是新手,只想实现RGB图像的分层聚集聚类。为此,我从图像中提取RGB的所有值。然后我处理image.Next,找出它的距离,然后开发链接。现在,我想从with中提取指定索引id的原始数据(即RGB值)。这是我到目前为止所做的代码。

代码语言:javascript
复制
image = Image.open('image.jpg')
image = image.convert('RGB')
im = np.array(image).reshape((-1,3))
rgb = list(im.getdata())
X = pdist(im)
Y = linkage(X)
I = inconsistent(Y)

基于第四列的一致性。为了得到最大的聚类,我选择了最小的截止值。

代码语言:javascript
复制
cutoff = 0.7
cluster_assignments = fclusterdata(Y, cutoff)
# Print the indices of the data points in each cluster.
num_clusters = cluster_assignments.max()
print "%d clusters" % num_clusters
indices = cluster_indices(cluster_assignments)
ind = np.array(enumerate(rgb))
for k, ind in enumerate(indices):
    print "cluster", k + 1, "is", ind
dendrogram(Y)

我得到的结果是这样的

代码语言:javascript
复制
cluster 6 is [ 6 11]
cluster 7 is [ 9 12]
cluster 8 is [15]

表示簇6包含6和11个叶子的索引。现在,在这一点上,我坚持如何映射这些索引来获得原始数据(即rgb值)。图像中每个像素的每个rgb值的索引。然后,我必须生成码本来实现聚集聚类。我不知道如何处理这项任务。读了很多东西,但没有任何线索。

EN

回答 1

Stack Overflow用户

发布于 2013-12-27 11:40:34

以下是我的解决方案:

代码语言:javascript
复制
import numpy as np
from scipy.cluster import hierarchy

im = np.array([[54,101,9],[ 67,89,27],[ 67,85,25],[ 55,106,1],[ 52,108,0],
 [ 55,78,24],[ 19,57,8],[ 19,46,0],[ 95,110,15],[112,159,57],
 [ 67,118,26],[ 76,127,35],[ 74,128,30],[ 25,62,0],[100,120,9],
 [127,145,61],[ 48,112,25],[198,25,21],[203,11,10],[127,171,60],
 [124,173,45],[120,133,19],[109,137,18],[ 60,85,0],[ 37,0,0],
 [187,47,20],[127,170,52],[ 30,56,0]])

groups = hierarchy.fclusterdata(im, 0.7)
idx_sorted = np.argsort(groups)
group_sorted = groups[idx_sorted]
im_sorted = im[idx_sorted]
split_idx = np.where(np.diff(group_sorted) != 0)[0] + 1
np.split(im_sorted, split_idx)

输出:

代码语言:javascript
复制
[array([[203,  11,  10],
       [198,  25,  21]]),
 array([[187,  47,  20]]),
 array([[127, 171,  60],
       [127, 170,  52]]),
 array([[124, 173,  45]]),
 array([[112, 159,  57]]),
 array([[127, 145,  61]]),
 array([[25, 62,  0],
       [30, 56,  0]]),
 array([[19, 57,  8]]),
 array([[19, 46,  0]]),
 array([[109, 137,  18],
       [120, 133,  19]]),
 array([[100, 120,   9],
       [ 95, 110,  15]]),
 array([[67, 89, 27],
       [67, 85, 25]]),
 array([[55, 78, 24]]),
 array([[ 52, 108,   0],
       [ 55, 106,   1]]),
 array([[ 54, 101,   9]]),
 array([[60, 85,  0]]),
 array([[ 74, 128,  30],
       [ 76, 127,  35]]),
 array([[ 67, 118,  26]]),
 array([[ 48, 112,  25]]),
 array([[37,  0,  0]])]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20764701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档