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

标签 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)

我想:

(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/

相关文章:

python - 使用 numpy 数组计算规则网格单元格内的点数

python - 如何在 python 中编写模块私有(private)/ protected 方法?

python - PyQt4 在线程中等待来自 GUI 的用户输入

python - PyQt:重置或删除 QTreeWidgetItem 的背景颜色

python - Python 中的无限求和

python - scipy.optimize.minimize 中的逐元素约束

python - 在没有numpy循环的情况下获取簇中元素的坐标

python - 移位插值没有给出预期的行为

python - 在大数组(栅格)中选择大小过滤元素

python - WSGI 中间件推荐