python-3.x Nibabel不加载斜率和内部参数

kgqe7b3p  于 4个月前  发布在  Python
关注(0)|答案(1)|浏览(73)

我正在使用nibabel包将一些nparray保存为nifti格式。我指定了一个斜率和截距值,以便将数据缩放回原始格式。然而,当我加载回数据时,scale和截距信息不再可用。下面是一个代码片段来重现该行为:

import numpy as np
import nibabel as nib

# Create dumb 3D data:
img=np.ones((20,20,5),dtype=np.uint16)

# Create an affine matrix:
affine=np.eye(4)

# Create nifti
nii=nib.Nifti1Image(img,affine=affine)

# Set slope and inter values
nii.header.set_slope_inter(slope=1,inter=0)

# Save the nifti on disk
nib.save(nii,'test.nii')

# Load back the data
nii2=nib.load('test.nii')

# Get the slope and inter
print(nii2.header.get_slope_inter())

# It returns (None, None)

字符串
输出为(None,None),尽管nii2.header.has_data_slopenii2.header.has_data_intercept都返回True。
我用mango和itk-snap等nifti阅读器打开了nifti文件,可以检查斜率和截距是否正确显示。
这是一个bug吗?
谢谢你的帮助!

tcomlyy6

tcomlyy61#

不,这不是一个bug。这种行为在Nibabel的文档中有记录。
当第一次创建/加载时,nifti 1对象(Nibabel图像)具有可访问的比例斜率和截距:

import nibabel as nib

# load the image
im = nib.load("image.nii.gz")

# get scale slope/inter values
scale_slope = im.dataobj.slope
scale_intercept = im.dataobj.inter

# scale_slope and scale_intercept won't be None and None, but the following:
slope_inter = im.header.get_slope_inter()

# will probably print (None, None)
print("slope_inter: %r" % (slope_inter,))

字符串
要回答您的问题,在调用set_slope_inter()之后,您设置的新值可在以下位置获得:

image.dataobj.slope
image.dataobj.inter


有点晚了,但我希望这能帮助到别人!

相关问题