本文整理汇总了Python中nilearn.image.new_img_like方法的典型用法代码示例。如果您正苦于以下问题:Python image.new_img_like方法的具体用法?Python image.new_img_like怎么用?Python image.new_img_like使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nilearn.image
的用法示例。
在下文中一共展示了image.new_img_like方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: inject_skullstripped
# 需要导入模块: from nilearn import image [as 别名]
# 或者: from nilearn.image import new_img_like [as 别名]
def inject_skullstripped(subjects_dir, subject_id, skullstripped):
mridir = op.join(subjects_dir, subject_id, "mri")
t1 = op.join(mridir, "T1.mgz")
bm_auto = op.join(mridir, "brainmask.auto.mgz")
bm = op.join(mridir, "brainmask.mgz")
if not op.exists(bm_auto):
img = nb.load(t1)
mask = nb.load(skullstripped)
bmask = new_img_like(mask, np.asanyarray(mask.dataobj) > 0)
resampled_mask = resample_to_img(bmask, img, "nearest")
masked_image = new_img_like(
img, np.asanyarray(img.dataobj) * resampled_mask.dataobj
)
masked_image.to_filename(bm_auto)
if not op.exists(bm):
copyfile(bm_auto, bm, copy=True, use_hardlink=True)
return subjects_dir, subject_id
示例2: new_nii_like
# 需要导入模块: from nilearn import image [as 别名]
# 或者: from nilearn.image import new_img_like [as 别名]
def new_nii_like(ref_img, data, affine=None, copy_header=True):
"""
Coerces `data` into NiftiImage format like `ref_img`
Parameters
----------
ref_img : :obj:`str` or img_like
Reference image
data : (S [x T]) array_like
Data to be saved
affine : (4 x 4) array_like, optional
Transformation matrix to be used. Default: `ref_img.affine`
copy_header : :obj:`bool`, optional
Whether to copy header from `ref_img` to new image. Default: True
Returns
-------
nii : :obj:`nibabel.nifti1.Nifti1Image`
NiftiImage
"""
ref_img = check_niimg(ref_img)
newdata = data.reshape(ref_img.shape[:3] + data.shape[1:])
if '.nii' not in ref_img.valid_exts:
# this is rather ugly and may lose some information...
nii = nib.Nifti1Image(newdata, affine=ref_img.affine,
header=ref_img.header)
else:
# nilearn's `new_img_like` is a very nice function
nii = new_img_like(ref_img, newdata, affine=affine,
copy_header=copy_header)
nii.set_data_dtype(data.dtype)
return nii
示例3: scale_image
# 需要导入模块: from nilearn import image [as 别名]
# 或者: from nilearn.image import new_img_like [as 别名]
def scale_image(image, scale_factor):
scale_factor = np.asarray(scale_factor)
new_affine = np.copy(image.affine)
new_affine[:3, :3] = image.affine[:3, :3] * scale_factor
new_affine[:, 3][:3] = image.affine[:, 3][:3] + (image.shape * np.diag(image.affine)[:3] * (1 - scale_factor)) / 2
return new_img_like(image, data=image.get_data(), affine=new_affine)
示例4: flip_image
# 需要导入模块: from nilearn import image [as 别名]
# 或者: from nilearn.image import new_img_like [as 别名]
def flip_image(image, axis):
try:
new_data = np.copy(image.get_data())
for axis_index in axis:
new_data = np.flip(new_data, axis=axis_index)
except TypeError:
new_data = np.flip(image.get_data(), axis=axis)
return new_img_like(image, data=new_data)
示例5: resize
# 需要导入模块: from nilearn import image [as 别名]
# 或者: from nilearn.image import new_img_like [as 别名]
def resize(image, new_shape, interpolation="linear"):
image = reorder_img(image, resample=interpolation)
zoom_level = np.divide(new_shape, image.shape)
new_spacing = np.divide(image.header.get_zooms(), zoom_level)
new_data = resample_to_spacing(image.get_data(), image.header.get_zooms(), new_spacing,
interpolation=interpolation)
new_affine = np.copy(image.affine)
np.fill_diagonal(new_affine, new_spacing.tolist() + [1])
new_affine[:3, 3] += calculate_origin_offset(new_spacing, image.header.get_zooms())
return new_img_like(image, new_data, affine=new_affine)
示例6: get_complete_foreground
# 需要导入模块: from nilearn import image [as 别名]
# 或者: from nilearn.image import new_img_like [as 别名]
def get_complete_foreground(training_data_files):
for i, set_of_files in enumerate(training_data_files):
subject_foreground = get_foreground_from_set_of_files(set_of_files)
if i == 0:
foreground = subject_foreground
else:
foreground[subject_foreground > 0] = 1
return new_img_like(read_image(training_data_files[0][-1]), foreground)
示例7: get_foreground_from_set_of_files
# 需要导入模块: from nilearn import image [as 别名]
# 或者: from nilearn.image import new_img_like [as 别名]
def get_foreground_from_set_of_files(set_of_files, background_value=0, tolerance=0.00001, return_image=False):
for i, image_file in enumerate(set_of_files):
image = read_image(image_file)
is_foreground = np.logical_or(image.get_data() < (background_value - tolerance),
image.get_data() > (background_value + tolerance))
if i == 0:
foreground = np.zeros(is_foreground.shape, dtype=np.uint8)
foreground[is_foreground] = 1
if return_image:
return new_img_like(image, foreground)
else:
return foreground
示例8: plot_registration
# 需要导入模块: from nilearn import image [as 别名]
# 或者: from nilearn.image import new_img_like [as 别名]
def plot_registration(
anat_nii,
div_id,
plot_params=None,
order=("z", "x", "y"),
cuts=None,
estimate_brightness=False,
label=None,
contour=None,
compress="auto",
):
"""
Plots the foreground and background views
Default order is: axial, coronal, sagittal
"""
plot_params = {} if plot_params is None else plot_params
# Use default MNI cuts if none defined
if cuts is None:
raise NotImplementedError # TODO
out_files = []
if estimate_brightness:
plot_params = robust_set_limits(anat_nii.get_fdata().reshape(-1), plot_params)
# FreeSurfer ribbon.mgz
ribbon = contour is not None and np.array_equal(
np.unique(contour.get_fdata()), [0, 2, 3, 41, 42]
)
if ribbon:
contour_data = contour.get_fdata() % 39
white = nlimage.new_img_like(contour, contour_data == 2)
pial = nlimage.new_img_like(contour, contour_data >= 2)
# Plot each cut axis
for i, mode in enumerate(list(order)):
plot_params["display_mode"] = mode
plot_params["cut_coords"] = cuts[mode]
if i == 0:
plot_params["title"] = label
else:
plot_params["title"] = None
# Generate nilearn figure
display = plot_anat(anat_nii, **plot_params)
if ribbon:
kwargs = {"levels": [0.5], "linewidths": 0.5}
display.add_contours(white, colors="b", **kwargs)
display.add_contours(pial, colors="r", **kwargs)
elif contour is not None:
display.add_contours(contour, colors="b", levels=[0.5], linewidths=0.5)
svg = extract_svg(display, compress=compress)
display.close()
# Find and replace the figure_1 id.
svg = svg.replace("figure_1", "%s-%s-%s" % (div_id, mode, uuid4()), 1)
out_files.append(fromstring(svg))
return out_files