【问题标题】:How can I reshape a 3D nii gz image array to a 2D?如何将 3D nii gz 图像阵列重塑为 2D?
【发布时间】:2022-01-10 01:29:15
【问题描述】:

我有很多 nii.gz 图像需要从 3D 重塑为 2D,我正在尝试使用以下代码,但由于 nii.gz 扩展,它无法正常工作:

import nibabel as nib
img = nib.load('teste01converted.nii.gz')
img.shape
newimg = img.reshape(332,360*360)

谁能帮帮我?

这是我正在使用的图像类型的示例:https://drive.google.com/drive/folders/1681T99hp6qZRUgx1Ej_h1hwFv1g_LJo4?usp=sharing

【问题讨论】:

    标签: python arrays image reshape neuro-image


    【解决方案1】:

    根据 NiBabel Getting Started 指南,您可以将 NiBabel 对象转换为 numpy 数组,然后您可以使用 reshape:

    无需将任何主要图像数据加载到内存中即可获得此信息。当然也可以以 NumPy 数组的形式访问图像数据

    >>> data = img.get_fdata()
    >>> data.shape
    (128, 96, 24, 2)
    >>> type(data)
    <... 'numpy.ndarray'>
    

    所以你可以像这样调整你的代码:

    import nibabel as nib
    img = nib.load('teste01converted.nii.gz')
    newimg = img.get_fdata().reshape(332,360*360)
    

    当我这样做时,我得到一个具有这种形状的 numpy 数组:

    (332, 129600)
    

    顺便说一下,我对神经影像学一无所知,所以我不知道你要求的转换是否有意义。

    【讨论】:

    • 非常感谢,成功了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    相关资源
    最近更新 更多