本文整理汇总了Python中nibabel.MGHImage方法的典型用法代码示例。如果您正苦于以下问题:Python nibabel.MGHImage方法的具体用法?Python nibabel.MGHImage怎么用?Python nibabel.MGHImage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nibabel
的用法示例。
在下文中一共展示了nibabel.MGHImage方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _mri_landmarks_to_mri_voxels
# 需要导入模块: import nibabel [as 别名]
# 或者: from nibabel import MGHImage [as 别名]
def _mri_landmarks_to_mri_voxels(mri_landmarks, t1_mgh):
"""Convert landmarks from MRI RAS space to MRI voxel space.
Parameters
----------
mri_landmarks : array, shape (3, 3)
The MRI RAS landmark data: rows LPA, NAS, RPA, columns x, y, z.
t1_mgh : nib.MGHImage
The image data in MGH format.
Returns
-------
mri_landmarks : array, shape (3, 3)
The MRI voxel-space landmark data.
"""
# Get landmarks in voxel space, using the T1 data
vox2ras_tkr = t1_mgh.header.get_vox2ras_tkr()
ras2vox_tkr = linalg.inv(vox2ras_tkr)
mri_landmarks = apply_trans(ras2vox_tkr, mri_landmarks) # in vox
return mri_landmarks
示例2: map_image
# 需要导入模块: import nibabel [as 别名]
# 或者: from nibabel import MGHImage [as 别名]
def map_image(img, out_affine, out_shape, ras2ras=np.array([[1.0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]),
order=1):
"""
Function to map image to new voxel space (RAS orientation)
:param nibabel.MGHImage img: the src 3D image with data and affine set
:param np.ndarray out_affine: trg image affine
:param np.ndarray out_shape: the trg shape information
:param np.ndarray ras2ras: ras2ras an additional maping that should be applied (default=id to just reslice)
:param int order: order of interpolation (0=nearest,1=linear(default),2=quadratic,3=cubic)
:return: mapped Image data array
"""
from scipy.ndimage import affine_transform
from numpy.linalg import inv
# compute vox2vox from src to trg
vox2vox = inv(out_affine) @ ras2ras @ img.affine
# here we apply the inverse vox2vox (to pull back the src info to the target image)
new_data = affine_transform(img.get_data(), inv(vox2vox), output_shape=out_shape, order=order)
return new_data
示例3: test_inject_skullstrip
# 需要导入模块: import nibabel [as 别名]
# 或者: from nibabel import MGHImage [as 别名]
def test_inject_skullstrip(tmp_path):
t1_mgz = tmp_path / "sub-01" / "mri" / "T1.mgz"
t1_mgz.parent.mkdir(parents=True)
# T1.mgz images are uint8
nb.MGHImage(np.ones((5, 5, 5), dtype=np.uint8), np.eye(4)).to_filename(str(t1_mgz))
mask_nii = tmp_path / "mask.nii.gz"
# Masks may be in a different space (and need resampling), but should be boolean,
# or uint8 in NIfTI
nb.Nifti1Image(np.ones((6, 6, 6), dtype=np.uint8), np.eye(4)).to_filename(
str(mask_nii)
)
FSInjectBrainExtracted(
subjects_dir=str(tmp_path), subject_id="sub-01", in_brain=str(mask_nii)
).run()
assert Path.exists(tmp_path / "sub-01" / "mri" / "brainmask.auto.mgz")
assert Path.exists(tmp_path / "sub-01" / "mri" / "brainmask.mgz")
# Run a second time to hit "already exists" condition
FSInjectBrainExtracted(
subjects_dir=str(tmp_path), subject_id="sub-01", in_brain=str(mask_nii)
).run()
示例4: is_conform
# 需要导入模块: import nibabel [as 别名]
# 或者: from nibabel import MGHImage [as 别名]
def is_conform(img, eps=1e-06):
"""
Function to check if an image is already conformed or not (Dimensions: 256x256x256, Voxel size: 1x1x1, and
LIA orientation.
:param nibabel.MGHImage img: Loaded source image
:param float eps: allowed deviation from zero for LIA orientation check (default 1e-06).
Small inaccuracies can occur through the inversion operation. Already conformed images are
thus sometimes not correctly recognized. The epsilon accounts for these small shifts.
:return: True if image is already conformed, False otherwise
"""
ishape = img.shape
if len(ishape) > 3 and ishape[3] != 1:
sys.exit('ERROR: Multiple input frames (' + format(img.shape[3]) + ') not supported!')
# check dimensions
if ishape[0] != 256 or ishape[1] != 256 or ishape[2] != 256:
return False
# check voxel size
izoom = img.header.get_zooms()
if izoom[0] != 1.0 or izoom[1] != 1.0 or izoom[2] != 1.0:
return False
# check orientation LIA
iaffine = img.affine[0:3, 0:3] + [[1, 0, 0], [0, 0, -1], [0, 1, 0]]
if np.max(np.abs(iaffine)) > 0.0 + eps:
return False
return True
示例5: conform
# 需要导入模块: import nibabel [as 别名]
# 或者: from nibabel import MGHImage [as 别名]
def conform(img, order=1):
"""
Python version of mri_convert -c, which turns image intensity values into UCHAR, reslices images to standard position, fills up
slices to standard 256x256x256 format and enforces 1 mm isotropic voxel sizes.
Difference to mri_convert -c is that we first interpolate (float image), and then rescale to uchar. mri_convert is
doing it the other way. However, we compute the scale factor from the input to be more similar again
:param nibabel.MGHImage img: loaded source image
:param int order: interpolation order (0=nearest,1=linear(default),2=quadratic,3=cubic)
:return:nibabel.MGHImage new_img: conformed image
"""
from nibabel.freesurfer.mghformat import MGHHeader
cwidth = 256
csize = 1
h1 = MGHHeader.from_header(img.header) # may copy some parameters if input was MGH format
h1.set_data_shape([cwidth, cwidth, cwidth, 1])
h1.set_zooms([csize, csize, csize])
h1['Mdc'] = [[-1, 0, 0], [0, 0, -1], [0, 1, 0]]
h1['fov'] = cwidth
h1['Pxyz_c'] = img.affine.dot(np.hstack((np.array(img.shape[:3]) / 2.0, [1])))[:3]
# from_header does not compute Pxyz_c (and probably others) when importing from nii
# Pxyz is the center of the image in world coords
# get scale for conversion on original input before mapping to be more similar to mri_convert
src_min, scale = getscale(img.get_data(), 0, 255)
mapped_data = map_image(img, h1.get_affine(), h1.get_data_shape(), order=order)
# print("max: "+format(np.max(mapped_data)))
if not img.get_data_dtype() == np.dtype(np.uint8):
if np.max(mapped_data) > 255:
mapped_data = scalecrop(mapped_data, 0, 255, src_min, scale)
new_data = np.uint8(np.rint(mapped_data))
new_img = nib.MGHImage(new_data, h1.get_affine(), h1)
# make sure we store uchar
new_img.set_data_dtype(np.uint8)
return new_img