使用nibabel以“SPM”样式保存nifti

2024-09-27 21:26:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经使用python分析了一些功能磁共振成像数据,现在我想将我的结果保存为niftis,以便在SPM分析中使用

我的数据分数是一个形状为64的浮点数组(9711597)。我使用了以下代码来保存它:

import nibabel as nib   
import nilearn 

scores_image = nib.Nifti1Image(scores,affine = np.eye(4))
nib.save(scores_image,"scores.nii")

但是,当我将数据加载到SPM中时,我注意到原点和刻度与SPM期望的不同: Comparison of my scores.nii (upper image) and a standard SPM nifti

有人知道哪种代码会自动将my scores变量保存为SPM期望的相同来源和大小吗

更新:以下是SPM图像的标题,其中突出显示了与我自己的图像不同的地方:

comp_img = nib.load('spmT_0014.nii')

print(comp_img.header)
<class 'nibabel.nifti1.Nifti1Header'> object, endian='<'
sizeof_hdr      : 348
data_type       : b''
db_name         : b''
extents         : 0
session_error   : 0
regular         : b'r'                          ## ---> b''
dim_info        : 0
dim             : [  3  97 115  97   1   1   1   1]
intent_p1       : 0.0
intent_p2       : 0.0
intent_p3       : 0.0
intent_code     : none
datatype        : float32
bitpix          : 32
slice_start     : 0
pixdim          : [1. 2. 2. 2. 0. 0. 0. 0.]     ## ---> [1. 1. 1. 1. 1. 1. 1. 1.]
vox_offset      : 0.0
scl_slope       : nan
scl_inter       : nan
slice_end       : 0
slice_code      : unknown
xyzt_units      : 10                            ## ---> 0
cal_max         : 0.0
cal_min         : 0.0
slice_duration  : 0.0
toffset         : 0.0
glmax           : 0
glmin           : 0
descrip         : b''
aux_file        : b''
qform_code      : aligned                       ## ---> unknown
sform_code      : aligned
quatern_b       : 0.0
quatern_c       : 0.0
quatern_d       : 0.0
qoffset_x       : -96.5                         ## ---> 0
qoffset_y       : -132.5                        ## ---> 0
qoffset_z       : -78.5                         ## ---> 0
srow_x          : [  2.    0.    0.  -96.5]     ## ---> [1. 0. 0. 0.]
srow_y          : [   0.     2.     0.  -132.5] ## ---> [0. 1. 0. 0.]
srow_z          : [  0.    0.    2.  -78.5]     ## ---> [0. 0. 1. 0.]
intent_name     : b''
magic           : b'n+1'

Tags: 数据代码imageimportcodeslicespmnii
1条回答
网友
1楼 · 发布于 2024-09-27 21:26:09

好的,如果其他人在某个时候尝试这样做,我已经找到了一种方法,使用以下代码:

from nilearn import image
import nibabel as nib

comp_img = nib.load('spmT_0014.nii')
scores_img = image.new_img_like(comp_img, scores, copy_header=False)

这给了我以下信息(上面的图像是SPM图像,下面的图像是我创建的图像):Comparison in SPM

相关问题 更多 >

    热门问题