如何为 scipy.ndimage.label 的特征指定周期性连接?

Njh*_*jha 4 python scipy ndimage

给定一个由 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)
Run Code Online (Sandbox Code Playgroud)

我想要:

(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)
Run Code Online (Sandbox Code Playgroud)

标签的结构参数允许指定连接(例如,即使它们对角线接触,也可以连接特征),它也可以用于此目的吗?

buz*_*jwa 5

下面是在左右边界上施加周期性边界条件的示例。右侧的每个标签均与左侧相应的标签(如果存在)进行标识。

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]
Run Code Online (Sandbox Code Playgroud)

您可以对上限和下限执行类似的操作。您还可以提出任何其他边界条件,迭代循环中的边界像素,并以类似的方式for检查语句中的条件。if