我收到了单个 fMRI 会话的多个 Nifti 图像,其中每个体积扫描都已保存到单个 3D-Nifti 文件中。这些被命名为'foobar_001.nii'、...、'foobar_187.nii'。我想合并这些并编写了以下 for 循环来做到这一点。
import numpy as np
import nibabel as ni
def merge_nii_files (sfile, ns):
# This will load the first image for header information
img = ni.load(sfile % (3, ns[0]))
dshape = list(img.shape)
dshape.append(len(ns))
data = np.empty(dshape, dtype=img.get_data_dtype())
header = img.header
equal_header_test = True
# Now load all the rest of the images
for n, i in enumerate(ns):
img = ni.load(sfile % (3,i))
equal_header_test = equal_header_test and img.header == header
data[...,n] = np.array(img.dataobj)
imgs = ni.Nifti1Image(data, img.affine, header=header)
if not equal_header_test:
print("WARNING: Not all headers were equal!")
return(imgs)
nii_files = "example_%0*d.nii"
images = merge_nii_files(nii_files, range(1,187))
如您所见,我想确保也复制标题信息。我的问题:这个'header=header'真的足够了吗?我问是因为 imgs 具有 4 元组形状,但我使用的标题来自 3 元组形状。我对 nibabel 或 Nifti 文件格式的内部结构不太熟悉。我错过了什么吗,即,我是否需要复制其他任何内容?