给定一个由 0 和 1 组成的 N*N 数组,我想构建集群列表(集群是一组标有 1 的连接点)。
scipy.ndimage.label
非常有用,因为它会告诉您连接了哪些点。
但我还想在我的阵列上设置周期性边界条件,即识别点 (0,j)
和 (N,j)
(就像平面我用胶水做一个圆柱体)。所以我需要告诉 scipy.ndimage.label 特征是通过边界连接的。
例如,如果我的原始数组是:
In[187]: a = [[1, 1, 0, 0, 0, 0, 1, 1],[1, 1, 0, 1, 0, 0, 1, 1],[1, 1, 0, 0, 0, 1, 1, 1]]
labels = measurements.label(a)
print(labels)
Out [187]: (array([[1, 1, 0, 0, 0, 0, 2, 2],
[1, 1, 0, 3, 0, 0, 2, 2],
[1, 1, 0, 0, 0, 2, 2, 2]], dtype=int32), 3)
我想:
(array([[1, 1, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 3, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 1, 1, 1]], dtype=int32), 2)
label 的结构参数允许指定连接(例如即使它们对角线接触也连接的特征),它也可以用于此目的吗?
最佳答案
这是在左右边界上施加周期性边界条件的示例。右侧的每个标签都用左侧的相应标签(如果存在)标识。
for y in range(label_image.shape[0]):
if label_image[y, 0] > 0 and label_image[y, -1] > 0:
label_image[label_image == label_image[y, -1]] = label_image[y, 0]
你可以对上下边界做类似的事情。您还可以提出任何其他边界条件,在 for
循环中迭代边界像素,并以类似方式检查 if
语句中的条件。
关于python - 如何为 scipy.ndimage.label 的特征指定周期连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55953353/