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.intersect_masks

nilearn.masking.intersect_masks(mask_imgs, threshold=0.5, connected=True)[source]

Compute intersection of several masks.

Given a list of input mask images, generate the output image which is the threshold-level intersection of the inputs.

Parameters:
mask_imgslist of Niimg-like objects

See Input and output: neuroimaging data representation. 3D individual masks with same shape and affine.

thresholdfloat, default=0.5

Gives the level of the intersection, must be within [0, 1]. threshold=1 corresponds to keeping the intersection of all masks, whereas threshold=0 is the union of all masks.

connectedbool, optional

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

Returns:
grp_mask3D nibabel.nifti1.Nifti1Image

Intersection of all masks.

Examples

>>> import numpy as np
>>> from nibabel import Nifti1Image
>>> from nilearn.masking import intersect_masks
>>>
>>> mask1 = np.array([[[0, 1, 1], [0, 1, 1], [0, 1, 1]]])
>>> mask_img1 = Nifti1Image(mask1, affine=np.eye(4), dtype=np.int32)
>>>
>>> mask2 = np.array([[[0, 0, 0], [0, 1, 1], [1, 0, 0]]])
>>> mask_img2 = Nifti1Image(mask2, affine=np.eye(4), dtype=np.int32)
>>>
>>> # Compute union with threshold=0
>>> mask_union = intersect_masks(
...     [mask_img1, mask_img2], threshold=0, connected=True
... )
>>> # Check output
>>> mask_union.get_fdata()
array([[[0., 1., 1.],
        [0., 1., 1.],
        [1., 1., 1.]]])
>>>
>>> # Compute intersection with threshold=1
>>> mask_intersection = intersect_masks(
...     [mask_img1, mask_img2], threshold=1, connected=True
... )
>>> # Check output
>>> mask_intersection.get_fdata()
array([[[0., 0., 0.],
        [0., 1., 1.],
        [0., 0., 0.]]])