Scipy标签的邻居

时间:2012-01-09 20:16:53

标签: python numpy scipy

我有一个标有scipy.ndimage.measurements.label的对象数组,名为Labels。我有其他数组Data包含与Labels相关的内容。如何制作第三个数组Neighbourhoods,用于将最近的标签映射到 x,y L

鉴于LabelsData,如何使用python / numpy / scipy获取Neighbourhoods

Labels = array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 1, 1, 1, 1, 0, 0, 0, 0, 0],
                [0, 1, 1, 1, 1, 0, 0, 0, 0, 0],
                [0, 1, 1, 1, 1, 0, 0, 0, 0, 0],
                [0, 1, 1, 1, 1, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 2, 2, 2, 0],
                [0, 0, 0, 0, 0, 0, 2, 2, 2, 0],
                [0, 0, 0, 0, 0, 0, 2, 2, 2, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] )

Data = array([[1, 1, 1, 1, 1, 1, 2, 3, 4, 5],
              [1, 0, 0, 0, 0, 1, 2, 3, 4, 5],
              [1, 0, 0, 0, 0, 1, 2, 3, 4, 4],
              [1, 0, 0, 0, 0, 1, 2, 3, 3, 3],
              [1, 0, 0, 0, 0, 1, 2, 2, 2, 2],
              [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
              [2, 2, 2, 2, 2, 1, 0, 0, 0, 1],
              [3, 3, 3, 3, 2, 1, 0, 0, 0, 1],
              [4, 4, 4, 3, 2, 1, 0, 0, 0, 1],
              [5, 5, 4, 3, 2, 1, 1, 1, 1, 1]] )

Neighbourhoods = array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                        [1, 0, 0, 0, 0, 1, 1, 1, 1, 1],
                        [1, 0, 0, 0, 0, 1, 1, 1, 0, 2],
                        [1, 0, 0, 0, 0, 1, 1, 0, 2, 2],
                        [1, 0, 0, 0, 0, 1, 0, 2, 2, 2],
                        [1, 1, 1, 1, 1, 0, 2, 2, 2, 2],
                        [1, 1, 1, 1, 0, 2, 0, 0, 0, 2],
                        [1, 1, 1, 0, 2, 2, 0, 0, 0, 2],
                        [1, 1, 0, 2, 2, 2, 0, 0, 0, 2],
                        [1, 1, 2, 2, 2, 2, 2, 2, 2, 2]] )

注意:我不确定关系会发生什么,所以在上面的Neighbourhoods

中使用了零

1 个答案:

答案 0 :(得分:2)

正如David Zaslavsky所说,这是voroni图的工作。这是一个笨拙的实现:http://blancosilva.wordpress.com/2010/12/15/image-processing-with-numpy-scipy-and-matplotlibs-in-sage/

相关功能为scipy.ndimage.distance_transform_edt。它有一个return_indices选项,可以利用它来做你需要的事情(以及计算原始距离(例子中的data))。

举个例子:

import numpy as np
from scipy.ndimage import distance_transform_edt

labels = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                  [0, 1, 1, 1, 1, 0, 0, 0, 0, 0],
                  [0, 1, 1, 1, 1, 0, 0, 0, 0, 0],
                  [0, 1, 1, 1, 1, 0, 0, 0, 0, 0],
                  [0, 1, 1, 1, 1, 0, 0, 0, 0, 0],
                  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                  [0, 0, 0, 0, 0, 0, 2, 2, 2, 0],
                  [0, 0, 0, 0, 0, 0, 2, 2, 2, 0],
                  [0, 0, 0, 0, 0, 0, 2, 2, 2, 0],
                  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] )
i, j = distance_transform_edt(labels == 0, return_distances=False, 
                              return_indices=True) 
neighborhoods = labels[i,j]
print neighborhoods

这会产生:

array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 2],
       [1, 1, 1, 1, 1, 1, 1, 1, 2, 2],
       [1, 1, 1, 1, 1, 1, 1, 2, 2, 2],
       [1, 1, 1, 1, 1, 1, 2, 2, 2, 2],
       [1, 1, 1, 1, 1, 2, 2, 2, 2, 2],
       [1, 1, 1, 1, 2, 2, 2, 2, 2, 2],
       [1, 1, 1, 2, 2, 2, 2, 2, 2, 2],
       [1, 1, 2, 2, 2, 2, 2, 2, 2, 2]])