通过 3 个 Numpy 3D 数组创建新的 Numpy 3D

问题描述 投票:0回答:1

我的代码有问题。 clahe_slices3 以 3D 形式保存的内容。我希望 clahe_slices1 的内容保存在第一维,clahe_slices2 保存在第二维,clahe_slices3 保存在第三维。在 clahe_slices 中,这里提供的是每个形状的形状: clahe_slices.shape=(240,240,155) clahe_slices1.shape=(240,240,155) clahe_slices2.shape=(240,240,155) clahe_slices3.shape=(240,240,155) 我的代码

nifti_img = nib.load(file_path)

        # Get the image data
img_data = nifti_img.get_fdata()

def apply_filter_to_nifti_image(img_data):

     clahe_slices = np.zeros_like(img_data)

     clahe_slices1 = np.zeros_like(img_data)

     clahe_slices2 = np.zeros_like(img_data)

     clahe_slices3 = np.zeros_like(img_data)

        

        
    for i in range(img_data.shape[0]):
    # Assuming the slice dimension is the first dimension
        clahe_slices[i, :, :] = clahe_slices1[i, :, :]
    for i in range(img_data.shape[1]):
        # Assuming the slice dimension is the second dimension
        clahe_slices[:, i, :] = clahe_slices2[:, i, :]
    for i in range(img_data.shape[2]):
        # Assuming the slice dimension is the third dimension
        clahe_slices[:, :, i] = clahe_slices3[:, :, i]

    

    # Example usage:
    return clahe_slices
apply_filter_to_nifti_image(img_data)

我已经过滤了幻灯片,但是在编辑维度时必须像这样指定,当我选择维度时,它会过滤其他维度,但不会检查其他维度中的幻灯片内容是否为空,因此结果存在问题。 这就是为什么我每次都会考虑应用过滤器并选择维度,最后我将每个维度的结果组合起来以获得正确的结果。这里,当我指定这个时,它不会保存clahe_slices1和clahe_slices2的结果,而只保存所有维度上最后一个循环的结果。我还是 nifti 和 numpy 的新手。 请给我解决这个问题的方法,谢谢 代码太长,我只突出了问题中的重要元素 我的代码有问题。 clahe_slices3 clahe_slices中所有维度保存的内容。我希望 clahe_slices1 的内容保存在第一维,clahe_slices2 保存在第二维,clahe_slices3 保存在第三维。在 clahe_slices 中,

我期望由 claheslices1 的第一维切片、claheslices2 的第二维切片和 claheslices3 的第三维切片形成一个 3D 数组。但我在 claheslices 中得到的结果是所有特殊维度 claheslices3 的切片

numpy
1个回答
0
投票

让我们尝试在一个小的 (3,3) 数组中复制

apply...
函数。制作
res
接收者数组,以及两个具有不同值的大小相似的数组:

In [38]: res = np.zeros((3,3)); res1 = np.arange(1,10).reshape(3,3); res2 = np.arange(10,100,10).reshape(3,3)

In [39]: res,res1,res2
Out[39]: 
(array([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]]),
 array([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]]),
 array([[10, 20, 30],
        [40, 50, 60],
        [70, 80, 90]]))

应用第一次迭代:

In [40]: for i in range(3): res[i,:] = res1[i,:]
In [41]: res
Out[41]: 
array([[1., 2., 3.],
       [4., 5., 6.],
       [7., 8., 9.]])

这只是复制

res1
的所有行。

然后复制

res2
中的所有列。为了让它更有趣,让“+=”而不是仅仅复制:

In [42]: for i in range(3): res[:,i] += res2[:,i]
In [43]: res
Out[43]: 
array([[11., 22., 33.],
       [44., 55., 66.],
       [77., 88., 99.]])

这与将两个数组相加相同:

In [44]: res1+res2
Out[44]: 
array([[11, 22, 33],
       [44, 55, 66],
       [77, 88, 99]])

我认为您还不清楚

 formed from the first dimensional slices of claheslices1, the second dimensional slices of claheslices2,..
的含义。

数组的所有第一维切片都是整个数组。同样,所有第二维切片。

我可以从 2 平面 3D 数组开始,然后复制到单独的平面。

In [45]: res = np.zeros((2,3,3))
In [46]: for i in range(3): res[0,i,:] = res1[i,:]
In [47]: for i in range(3): res[1,:,i] += res2[:,i]
In [48]: res
Out[48]: 
array([[[ 1.,  2.,  3.],
        [ 4.,  5.,  6.],
        [ 7.,  8.,  9.]],

       [[10., 20., 30.],
        [40., 50., 60.],
        [70., 80., 90.]]])

stack
np.array((res1,res2))
相同:

In [49]: np.stack((res1,res2))
Out[49]: 
array([[[ 1,  2,  3],
        [ 4,  5,  6],
        [ 7,  8,  9]],

       [[10, 20, 30],
        [40, 50, 60],
        [70, 80, 90]]])
© www.soinside.com 2019 - 2024. All rights reserved.