使用 scipy.ndimage.label 标记 3d numpy 数组 [英] label 3d numpy array with scipy.ndimage.label

查看:109
本文介绍了使用 scipy.ndimage.label 标记 3d numpy 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由 1 和 0 组成的大型 3d numpy 数组.我想使用 scipy.ndimage.label 工具来标记每个子阵列(2d)中的特征.

3d 数组的一个子集看起来像:

subset=np.array([[[1, 0, 0],[1, 0, 1],[0, 0, 0]],[[0, 0, 0],[1, 0, 1],[0, 0, 1]],[[0, 0, 0],[1, 0, 0],[0, 1, 1]],[[0, 0, 0],[1, 0, 0],[1, 1, 1]]], dtype=uint8)

当我在这个子集的一小部分使用标签工具时是正确的:

>>>label(subset[0:3])(数组([[[1, 0, 0],[1, 0, 2],[0, 0, 0]],[[0, 0, 0],[1, 0, 2],[0, 0, 2]],[[0, 0, 0],[1, 0, 0],[0, 2, 2]]]), 2)

但是,当我使用整个子集时,标签工具无法正常工作:

>>>标签(子集)(数组([[[1, 0, 0],[1, 0, 1],[0, 0, 0]],[[0, 0, 0],[1, 0, 1],[0, 0, 1]],[[0, 0, 0],[1, 0, 0],[0, 1, 1]],[[0, 0, 0],[1, 0, 0],[1, 1, 1]]]), 1)

有什么想法可以解决这个问题吗?

ps.我试图标记的完整数组由 350219 个二维数组组成.

解决方案

我在 dan-man 的帮助下回答了这个问题.

我必须为标签工具定义一个新的 3D 结构:

str_3D=array([[[0, 0, 0],[0, 0, 0],[0, 0, 0]],[[0, 1, 0],[1, 1, 1],[0, 1, 0]],[[0, 0, 0],[0, 0, 0],[0, 0, 0]]], dtype='uint8')

现在标签为我的子集返回以下内容:

<预><代码>>>>标签(子集,结构=str_3D)# 输出:(数组([[[1, 0, 0],[1, 0, 2],[0, 0, 0]],[[0, 0, 0],[3, 0, 4],[0, 0, 4]],[[0, 0, 0],[5, 0, 0],[0, 6, 6]],[[0, 0, 0],[7, 0, 0],[7, 7, 7]]]), 7)

I've got a large 3d numpy array which consists of ones and zeros. I would like to use the scipy.ndimage.label tool to label the features in each sub-array (2d).

A subset of the 3d-array looks like:

subset=np.array([[[1, 0, 0],
    [1, 0, 1],
    [0, 0, 0]],

   [[0, 0, 0],
    [1, 0, 1],
    [0, 0, 1]],

   [[0, 0, 0],
    [1, 0, 0],
    [0, 1, 1]],

   [[0, 0, 0],
    [1, 0, 0],
    [1, 1, 1]]], dtype=uint8)

When I use the label tool on a small part of this subset is works correct:

>>>label(subset[0:3])    
(array([[[1, 0, 0],
    [1, 0, 2],
    [0, 0, 0]],

   [[0, 0, 0],
    [1, 0, 2],
    [0, 0, 2]],

   [[0, 0, 0],
    [1, 0, 0],
    [0, 2, 2]]]), 2)

However, when I use the entire subset the label tool is not working properly:

>>>label(subset)
(array([[[1, 0, 0],
    [1, 0, 1],
    [0, 0, 0]],

   [[0, 0, 0],
    [1, 0, 1],
    [0, 0, 1]],

   [[0, 0, 0],
    [1, 0, 0],
    [0, 1, 1]],

   [[0, 0, 0],
    [1, 0, 0],
    [1, 1, 1]]]), 1)

Any ideas how this problem can be tackled?

ps. The complete array which I am trying to label consists of 350219 2d arrays.

解决方案

I answered this question with the help of dan-man.

I had to define a new 3D structure for the label tool:

str_3D=array([[[0, 0, 0],
    [0, 0, 0],
    [0, 0, 0]],

   [[0, 1, 0],
    [1, 1, 1],
    [0, 1, 0]],

   [[0, 0, 0],
    [0, 0, 0],
    [0, 0, 0]]], dtype='uint8')

Now the label returns the following for my subset:

>>> label(subset,structure=str_3D)
# outputs:
(array([[[1, 0, 0],
    [1, 0, 2],
    [0, 0, 0]],

   [[0, 0, 0],
    [3, 0, 4],
    [0, 0, 4]],

   [[0, 0, 0],
    [5, 0, 0],
    [0, 6, 6]],

   [[0, 0, 0],
    [7, 0, 0],
    [7, 7, 7]]]), 7)

这篇关于使用 scipy.ndimage.label 标记 3d numpy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆