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.masking.compute_background_mask#
- nilearn.masking.compute_background_mask(data_imgs, border_size=2, connected=False, opening=False, target_affine=None, target_shape=None, memory=None, verbose=0)[source]#
Compute a brain mask for the images by guessing the value of the background from the border of the image.
- Parameters
- data_imgsNiimg-like object
See https://nilearn.github.io/stable/manipulating_images/input_output.html # noqa:E501 Images used to compute the mask. 3D and 4D images are accepted.
Note
If a 3D image is given, we suggest to use the mean image.
- border_size
int
, optional The size, in voxel of the border used on the side of the image to determine the value of the background. Default=2.
- connected
bool
, optional If connected is True, only the largest connect component is kept. Default=False.
- opening
bool
orint
, optional This parameter determines whether a morphological opening is performed, to keep only large structures. This step is useful to remove parts of the skull that might have been included.
opening
can be:A boolean : If False, no opening is performed. If True, it is equivalent to
opening=1
.An integer n: The opening is performed via n erosions (see
scipy.ndimage.binary_erosion
). The largest connected component is then estimated ifconnected
is set to True, and 2`n` dilation operations are performed (seescipy.ndimage.binary_dilation
) followed by n erosions. This corresponds to 1 opening operation of order n followed by a closing operator of order n.
Note
Turning off opening (
opening=False
) will also prevent any smoothing applied to the image during the mask computation.Default=False.
- target_affine
numpy.ndarray
, optional. If specified, the image is resampled corresponding to this new affine.
target_affine
can be a 3x3 or a 4x4 matrix. Default=None.Note
This parameter is passed to
nilearn.image.resample_img
.- target_shape
tuple
orlist
, optional. If specified, the image will be resized to match this new shape.
len(target_shape)
must be equal to 3.Note
If
target_shape
is specified, atarget_affine
of shape(4, 4)
must also be given.Default=None.
Note
This parameter is passed to
nilearn.image.resample_img
.- memoryinstance of
joblib.Memory
orstr
Used to cache the masking process. By default, no caching is done. If a
str
is given, it is the path to the caching directory.- verbose
int
, optional Verbosity level (0 means no message). Default=0.
- Returns
- mask
nibabel.nifti1.Nifti1Image
The brain mask (3D image).
- mask