Python 2D-cluster finder

时间:2012-12-12 02:57:45

标签: python

我有一个由0和1组成的数组.1s形成连续的簇,如图所示。

Clustering

预先不知道簇的数量。

是否有某种方法可以创建包含所有群集位置的列表,或者包含每个群集的列表,其中包含所有群集的位置。例如:

cluster_list = continuous_cluster_finder(data_array)
cluster_list[0] = [(pixel1_x, pixel1_y), (pixel2_x, pixel2_y),...]

2 个答案:

答案 0 :(得分:2)

从描述中不清楚问题的确切限制是什么。 假设您可以在左侧,右侧,上方,下方区分一个簇,则以下内容可以解决问题......

#!/usr/bin/env python

data = [ #top-left
         [0,0,1,1,0,0],
         [0,0,1,1,0,0],
         [1,1,0,0,1,1],
         [1,1,0,0,1,1],
         [0,0,1,1,0,0],
         [0,0,1,1,0,0],
         [1,1,0,0,1,1],
         [1,1,0,0,1,1],
       ]             # bottom-right

d = {} # point --> clid
dcl = {} # clid --> [point1,point2,...]

def process_point(t):
    global clid # cluster id
    val = data[t[0]][t[1]]
    above = (t[0]-1, t[1])
    abovevalid = 0 <= above[0] < maxX and 0 <= above[1] < maxY
    #below = (t[0]+1, t[1]) # We do not need that because we scan from top-left to bottom-right
    left = (t[0], t[1]-1)
    leftvalid = 0 <= left[0] < maxX and 0 <= left[1] < maxY
    #right = (t[0], t[1]+1) # We do not need that because we scan from top-left to bottom-right

    if not val: # for zero return
        return
    if left in d and above in d and d[above] != d[left]:
        # left and above on different clusters, merge them
        prevclid = d[left]
        dcl[d[above]].extend(dcl[prevclid]) # update dcl
        for l in dcl[d[left]]:
            d[l] = d[above] # update d
        del dcl[prevclid]
        dcl[d[above]].append(t)
        d[t] = d[above]
    elif above in d and abovevalid:
        dcl[d[above]].append(t)
        d[t] = d[above]
    elif left in d and leftvalid:
        dcl[d[left]].append(t)
        d[t] = d[left]
    else: # First saw this one 
        dcl[clid] = [t]
        d[t] = clid
        clid += 1

def print_output():
    for k in dcl: # Print output
        print k, dcl[k]

def main():
    global clid
    global maxX
    global maxY
    maxX = len(data)
    maxY = len(data[0])
    clid = 0
    for i in xrange(maxX):
        for j in xrange(maxY):
            process_point((i,j))
    print_output()

if __name__ == "__main__":
    main()

打印......

0 [(0, 2), (0, 3), (1, 2), (1, 3)]
1 [(2, 0), (2, 1), (3, 0), (3, 1)]
2 [(2, 4), (2, 5), (3, 4), (3, 5)]
3 [(4, 2), (4, 3), (5, 2), (5, 3)]
4 [(6, 0), (6, 1), (7, 0), (7, 1)]
5 [(6, 4), (6, 5), (7, 4), (7, 5)]

答案 1 :(得分:1)

您可以查看众所周知的'blob'查找算法,这些算法用于图像处理以隔离相同颜色的区域。您还可以通过查找岛屿并标记它们来酿造您自己的口味(所有这些岛屿在开始时都是未访问的);全部连接(在3x3网格中,中心像素为8连接)和访问像素形成一个区域;你需要在地图中找到所有这些区域。

Blob查找是您需要查找的内容。