-2

我有一个二进制 3d 数组,其中包含小组1和大组1. 我想搜索数组,当1找到 a 时,我想搜索x,y,z方向上的周围值并计算1连接的数量。x如果数量少于1我想将该组设置为 0。整个 3d 数组由1和组成0

数组示例:

img  = np.array([[[0,0,0,1,0],
                  [0,0,0,1,1]],
                 [[0,0,0,1,0],
                  [0,0,0,0,0]]])

在 x,y,z 方向有一组1直接相邻。在我的这个场景的代码中,该组是num_group = 4. 由于该组小于 10,因此我想创建该组0

img  = np.array([[[0,0,0,0,0],
                  [0,0,0,0,0]],
                 [[0,0,0,0,0],
                  [0,0,0,0,0]]])

我的数组中有 1-2 个非常大且不同的组。我只想在我的最终数组中包含这些大组。

import nibabel as nib
import numpy as np
import os, sys

x = 10

img = nib.load(\\test.nii).get_fdata()
print(img.shape)
>>>(512,512,30)
size_x, size_y, size_z = vol.shape

for z_slices in range(size_z):
    for y_slices in range(size_y):
        for x_slices in range(size_x):
            num_group = (Find a 1 and count how many 1s are connected)
            if num_group < x:
                num_group = 0
4

1 回答 1

1

您可以为此使用 skimage:

from skimage.measure import regionprops,label
sz = 10  #this is your set threshold
xyz = np.vstack([i.coords for i in regionprops(label(img)) if i.area<sz]) #finding regions and coordinates of regions smaller than threshold
img[tuple(xyz.T)]=0 #setting small regions to 0

输出:

[[[0 0 0 0 0]
  [0 0 0 0 0]]

 [[0 0 0 0 0]
  [0 0 0 0 0]]]
于 2020-09-28T18:51:26.307 回答