我有一个二进制 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