编程语言
首页 > 编程语言> > Python将list矩阵保存为nii文件

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,header,Python,list,nib,np,new,data
来源: https://blog.csdn.net/Sweety_Lin/article/details/119564723