0

我希望将 ~1000 个.nrrd文件转换为 Nifit ( .nii.gz) 格式。我一直在使用3DSlicerResampleScalarVectorDWIVolume命令行模块来完成这项任务。但是这个过程真的很慢。转换我系统上的每个文件大约需要 4 分钟。我想知道人们使用什么工具进行此类转换?

4

2 回答 2

5
import vtk

def readnrrd(filename):
    """Read image in nrrd format."""
    reader = vtk.vtkNrrdReader()
    reader.SetFileName(filename)
    reader.Update()
    info = reader.GetInformation()
    return reader.GetOutput(), info

def writenifti(image,filename, info):
    """Write nifti file."""
    writer = vtk.vtkNIFTIImageWriter()
    writer.SetInputData(image)
    writer.SetFileName(filename)
    writer.SetInformation(info)
    writer.Write()

m, info = readnrrd('/media/neubias/b0c7dd3a-8b12-435e-8303-2c331d05b365/DATA/Henry_data/mri.nrrd')
writenifti(m, '/media/neubias/b0c7dd3a-8b12-435e-8303-2c331d05b365/DATA/Henry_data/mri_prueba2.nii', info)
于 2018-03-27T09:16:19.420 回答
3

以下代码可用于将文件夹中的所有“.nrrd”文件转换为压缩的“nifti”格式:

import os
from glob import glob
import nrrd #pip install pynrrd, if pynrrd is not already installed
import nibabel as nib #pip install nibabel, if nibabel is not already installed
import numpy as np

baseDir = os.path.normpath('path/to/file')
files = glob(baseDir+'/*.nrrd')

for file in files:
#load nrrd 
  _nrrd = nrrd.read(file)
  data = _nrrd[0]
  header = _nrrd[1]

#save nifti
  img = nib.Nifti1Image(data, np.eye(4))
  nib.save(img,os.path.join(baseDir, file[-8:-5] + '.nii.gz'))

例如,此脚本将分别转换toabc.nrrd和中xyz.nrrd的文件。baseDirabc.nii.gzxyz.nii.gz

于 2018-01-26T20:42:20.980 回答