【问题标题】:How can I flip .nii images using batch processing with python-nibabel?如何使用带有 python-nibabel 的批处理来翻转 .nii 图像?
【发布时间】:2022-10-06 18:07:40
【问题描述】:

我为每位患者提供了 180 张 .nii fMRI 图像,我需要将它们从右向左翻转。我尝试使用 \'for\' 语句来批处理这些图像。但似乎只有以下代码产生一个结果. 这是以下代码:

import nibabel as nib
import numpy as np
import os

NII_DIR=\'/Users/lena/testtest\'
def get_filelist(dir, Filelist):
    if os.path.isdir(dir):
        for s in os.listdir(dir):
            newDir = os.path.join(dir, s)
            Filelist.append(newDir)
    return Filelist

list = get_filelist(NII_DIR, [])
print(len(list))
type(list)

for i in list:
    def read_nii_file1(nii_path):
        nii_img=nib.load(nii_path)
        return nii_img
    nii_img=read_nii_file1(os.path.join(i))

    from nilearn.image import new_img_like
    print(nii_img.affine.shape)
    print(nii_img.affine)

    def flip_image(image, axis):
        try:
            new_data = np.copy(image.get_fdata())
            for axis_index in axis:
                new_data = np.flip(new_data, axis=axis_index)
        except TypeError:
            new_data = np.flip(image.get_fdata(), axis=axis)
        return new_img_like(image, data=new_data)

    new_image = flip_image(nii_img, axis=[0])

    affine = new_image.affine.copy()
    hdr = new_image.header.copy()

    new_data = new_image.get_fdata()
    new_nii = nib.Nifti1Image(new_data, affine, hdr)

#the following is the part I have no idea how to write
    New_NII_DIR = (\'/Users/lena/test_new/001.nii\')
    nib.save(new_nii, New_NII_DIR)

[这里是我的数据示例,包括 180 个阶段][1] [1]:https://i.stack.imgur.com/wDh34.png

    标签: python list image loops nibabel


    【解决方案1】:

    这个问题已经解决了。

    【讨论】:

      猜你喜欢
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 2010-10-05
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 2016-09-20
      • 1970-01-01
      相关资源
      最近更新 更多