Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在数组中查找组?_Python_Arrays_Numpy_Multidimensional Array_Nibabel - Fatal编程技术网

Python 如何在数组中查找组?

Python 如何在数组中查找组?,python,arrays,numpy,multidimensional-array,nibabel,Python,Arrays,Numpy,Multidimensional Array,Nibabel,我有一个二进制3d数组,它有小组1和大组1。我要搜索数组,当找到1时,我要搜索x、y、z方向上的周围值,并计算连接了多少1。如果1的数量小于x我想将该组设置为0。整个3d阵列由1和0组成 img = np.array([[[0,0,0,0,0], [0,0,0,0,0]], [[0,0,0,0,0], [0,0,0,0,0]]]) 阵列示例: img = np.array([[[0,0

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

img  = np.array([[[0,0,0,0,0],
                  [0,0,0,0,0]],
                 [[0,0,0,0,0],
                  [0,0,0,0,0]]])
阵列示例:

img  = np.array([[[0,0,0,1,0],
                  [0,0,0,1,1]],
                 [[0,0,0,1,0],
                  [0,0,0,0,0]]])
有一组
1
在x、y、z方向上直接相邻。在本场景的代码中,组为
num\u 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
将nibabel作为nib导入
将numpy作为np导入
导入操作系统,系统
x=10
img=nib.load(\\test.nii).get_fdata()
打印(图像形状)
>>>(512,512,30)
大小x,大小y,大小z=vol.shape
对于范围内的z_切片(大小为z):
对于范围内的y_切片(大小为y):
对于范围内的x_切片(大小x):
num_group=(查找1并计算连接了多少个1)
如果num_组
您可以使用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

代码与问题的相关性如何?如何定义“组”?我们谈论的是什么类型的连接?你可以只做一个卷积吗?@MadPhysicast我只是展示我目前的方法/思维过程。我愿意接受任何方法。你的代码和文章是断开连接的。详细描述你想要什么,并解释代码是如何实现的。除此之外,没有什么问题。