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_multi_background_mask(data_imgs, border_size=2, upper_cutoff=0.85, connected=True, opening=2, threshold=0.5, target_affine=None, target_shape=None, exclude_zeros=False, n_jobs=1, memory=None, verbose=0)[source]#

Compute a common mask for several sessions or subjects of data.

Uses the mask-finding algorithms to extract masks for each session or subject, and then keep only the main connected component of the a given fraction of the intersection of all the masks.

data_imgslist of Niimg-like objects

See Input and output: neuroimaging data representation. A list of arrays, each item being a subject or a session. 3D and 4D images are accepted.


If 3D images are given, we suggest to use the mean image of each session.

thresholdfloat, optional

The inter-session threshold: the fraction of the total number of session in for which a voxel must be in the mask to be kept in the common mask. threshold=1 corresponds to keeping the intersection of all masks, whereas threshold=0 is the union of all masks.

border_sizeint, optional

The size, in voxel of the border used on the side of the image to determine the value of the background. Default=2.

connectedbool, optional

If connected is True, only the largest connect component is kept. Default=True.

target_affinenumpy.ndarray, default=None

If specified, the image is resampled corresponding to this new affine. target_affine can be a 3x3 or a 4x4 matrix.


This parameter is passed to nilearn.image.resample_img.

target_shapetuple or list, default=None

If specified, the image will be resized to match this new shape. len(target_shape) must be equal to 3.


If target_shape is specified, a target_affine of shape (4, 4) must also be given.


This parameter is passed to nilearn.image.resample_img.

memoryinstance of joblib.Memory, str, or pathlib.Path

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.

n_jobsint, default=1

The number of CPUs to use to do the computation. -1 means ‘all CPUs’.

mask3D nibabel.nifti1.Nifti1Image

The brain mask.