Python将list矩阵保存为nii文件

import nibabel as nib
import numpy as np

# 首先读取nii文件并完成维度转换和NAN值替换
path='../data/'
fileName='lactate.nii'
data = nib.load(path+fileName).get_fdata()
data = data.transpose(2, 0, 1)
dataTest = np.nan_to_num(data)

# 某任务产生了新的list
dataTestGT=np.mean(dataTest[:,...],axis=0)[np.newaxis,...]

resultImgs=[]
inputImgs=[]

for index in range(dataTest.shape[0]):
    
    im=dataTest[index]
    gt=dataTestGT[0] 
    
    means = prediction.tiledPredict(...)
    
    resultImgs.append(means)
    inputImgs.append(im)
 
# 保存list为nii
resultImgsshift = np.array(resultImgs).transpose(1, 2, 0)
new_header = nib.load(path+fileName).header.copy()
new_data = np.array(resultImgsshift)
new_img = nib.nifti1.Nifti1Image(new_data, None, header=new_header)
nib.save(new_img, 'N2VPrediction.nii')
 

上一篇:读取nii文件


下一篇:利用Nibabel包处理nii格式文件