Note
This page is a reference documentation. It only explains the function signature, and not how to use it. Please refer to the user guide for the big picture.
nilearn.image.resample_to_img¶
- nilearn.image.resample_to_img(source_img, target_img, interpolation='continuous', copy=True, order='F', clip=False, fill_value=0, force_resample=True, copy_header=True)[source]¶
Resample a Niimg-like source image on a target Niimg-like image.
No registration is performed: the image should already be aligned.
Added in Nilearn 0.2.4.
- Parameters:
- source_imgNiimg-like object
See Input and output: neuroimaging data representation. Image(s) to resample.
- target_imgNiimg-like object
See Input and output: neuroimaging data representation. Reference image taken for resampling.
- interpolation
str, default=’continuous’ Can be ‘continuous’, ‘linear’, or ‘nearest’. Indicates the resample method.
- copy
bool, default=True If True, guarantees that output array has no memory in common with input array. In all cases, input images are never modified by this function.
- order“F” or “C”, default=”F”
Data ordering in output array. This function is slightly faster with Fortran ordering.
- clip
bool, default=False If False, no clip is performed. If True, all resampled image values above max(img) and under min(img) are cllipped to min(img) and max(img).
- fill_value
float, default=0 Use a fill value for points outside of input volume.
- force_resample
bool, default=True False is intended for testing, this prevents the use of a padding optimization.
- copy_header
bool, default=True Indicated if the header of the reference image should be used to create the new image. Ignored for
SurfaceImage.Added in Nilearn 0.11.0.
- Returns:
- resamplednibabel.Nifti1Image
input image, resampled to have respectively target image shape and affine as shape and affine.
See also
Examples
Resample a source image to match the shape and affine of a target image:
>>> import numpy as np >>> import nibabel as nib >>> from nilearn import image >>> source_data = np.zeros((4, 4, 4)) >>> source_img = nib.Nifti1Image(source_data, affine=np.eye(4)) >>> target_data = np.zeros((2, 2, 2)) >>> target_img = nib.Nifti1Image( ... target_data, ... affine=np.eye(4) * 2, # 2 mm voxels ... ) >>> resampled = image.resample_to_img(source_img, target_img) >>> resampled.shape (2, 2, 2) >>> resampled.affine[:3, :3] array([[2., 0., 0.], [0., 2., 0.], [0., 0., 2.]])
The resampled image inherits the shape and affine of the target image:
>>> np.array_equal(resampled.affine, target_img.affine) True >>> resampled.shape == target_img.shape True