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

nilearn.image.largest_connected_component_img(imgs)[source]

Return the largest connected component of an image or list of images.

Added in Nilearn 0.3.1.

Parameters:
imgsNiimg-like object or iterable of Niimg-like objects (3D)

Image(s) to extract the largest connected component from. See Input and output: neuroimaging data representation.

Returns:
3D Niimg-like object or list of

Image or list of images containing the largest connected component.

Notes

Handling big-endian in given Nifti image This function changes the existing byte-ordering information to new byte order, if the dtype in given Nifti image has non-native data type. This operation is done internally to avoid big-endian issues with scipy ndimage module.

Examples

>>> import numpy as np
>>> from nibabel import Nifti1Image
>>>
>>> # Create a simple 3D image with two components.
>>> shape = (2, 2, 1)
>>> img = Nifti1Image(
...     np.concatenate(
...         [
...             np.ones(shape),
...             np.zeros(shape),
...             np.ones(shape),
...             np.ones(shape),
...         ],
...         axis=-1,
...     ),
...     affine=np.eye(4),
...     dtype=np.int32,
... )
>>> img.get_fdata()
array([[[1., 0., 1., 1.],
        [1., 0., 1., 1.]],
       [[1., 0., 1., 1.],
        [1., 0., 1., 1.]]])
>>> from nilearn.image import largest_connected_component_img
>>> largest_cc_image = largest_connected_component_img(img)
>>> largest_cc_image.get_fdata()
array([[[0., 0., 1., 1.],
        [0., 0., 1., 1.]],
       [[0., 0., 1., 1.],
        [0., 0., 1., 1.]]])