【发布时间】:2021-09-27 11:38:56
【问题描述】:
我有一个二维数组,我在其中使用 ndimage.label() 函数标记集群,如下所示:
import numpy as np
from scipy.ndimage import label
input_array = np.array([[0, 1, 1, 0],
[1, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 0, 1]])
labeled_array, _ = label(input_array)
# Result:
# labeled_array == [[0, 1, 1, 0],
# [1, 1, 0, 0],
# [0, 0, 0, 2],
# [0, 0, 0, 2]]
我可以获得元素计数、质心或标记集群的边界框。但我也想获得集群中每个元素的坐标。是这样的(数据结构不一定要这样,任何数据结构都可以):
{
1: [(0, 1), (0, 2), (1, 0), (1, 1)], # Coordinates of the elements that have the label "1"
2: [(2, 3), (3, 3)] # Coordinates of the elements that have the label "2"
}
我可以遍历标签列表并为它们中的每一个调用np.where(),但我想知道是否有一种方法可以在没有循环的情况下执行此操作,这样会更快?
【问题讨论】:
标签: python numpy scipy cluster-analysis ndimage