是否可以将3D阵列转换为坐标系?

问题描述

是否可以获取3D数组并将其转换为坐标系?我的数组由0和1组成。如果值为1,我要采用xyz坐标。最后,我想将所有坐标输出到一个csv文件中。

import nibabel as nib

coord = []
img = nib.load('test.nii').get_fdata().astype(int)

test.nii数组:

[[[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 1 ... 1 1 0]
  ...
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 1 1 1]
  [0 1 0 ... 0 0 0]]

 [[1 0 0 ... 0 0 0]
  [0 0 1 ... 0 0 0]
  [0 1 0 ... 0 0 0]
  ...
  [0 1 0 ... 0 0 0]
  [0 1 0 ... 0 0 0]
  [0 0 0 ... 1 0 0]]

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 1 0]
  [0 0 0 ... 0 0 0]
  ...
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 1 0 ... 0 1 1]]

 ...

 [[0 0 0 ... 1 0 0]
  [0 0 1 ... 0 0 0]
  [0 0 1 ... 0 0 0]
  ...
  [0 0 0 ... 1 0 0]
  [0 0 0 ... 1 0 0]
  [0 0 0 ... 1 0 0]]

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 1]
  ...
  [0 1 0 ... 0 0 0]
  [1 0 0 ... 0 0 0]
  [1 0 0 ... 0 0 0]]

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  ...
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 1 0]
  [0 1 0 ... 0 0 0]]]

解决方法

这不一定是最好的解决方案,但让我们保持简单(如果框架为我们做到了,那将很好,但是...嗯):

data = [[[0,0],[0,1,1],0]],[[1,0]]]

for x in range(len(data)):
    for y in range(len(data[x])):
        for z in range(len(data[x][y])):
            if data[x][y][z] == 1:
                print(f"{x} {y} {z}")

产量:

0 2 2
0 2 3
0 2 4
0 4 3
0 4 4
0 4 5
0 5 1
1 0 0
1 1 2
1 2 1
1 3 1
1 4 1
1 5 3
,

使用np.where(),可以获得满足您条件的元素的行,列和深度索引。 试试这个:

row_idx,col_idx,depth_idx = np.where(img==1)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...