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

nilearn.image.binarize_img(img, threshold=0.0, mask_img=None, two_sided=False, copy_header=True)[source]

Binarize an image such that its values are either 0 or 1.

Added in Nilearn 0.8.1.

Parameters:
imga 3D/4D Niimg-like object or SurfaceImage

Image which should be binarized.

thresholdfloat or str, default=0.0

If float, we threshold the image based on image intensities meaning voxels which have intensities greater than this value will be kept. The given value should be within the range of minimum and maximum intensity of the input image. If string, it should finish with percent sign e.g. “80%” and we threshold based on the score obtained using this percentile on the image data. The voxels which have intensities greater than this score will be kept. The given string should be within the range of “0%” to “100%”.

mask_imgNiimg-like object or SurfaceImage, default=None

Mask image applied to mask the input data. If None, no masking will be applied.

two_sidedbool, default=False

If True, threshold is applied to the absolute value of the image. If False, threshold is applied to the original value of the image.

Added in Nilearn 0.10.3.

Changed in Nilearn 0.13.0dev: Default was changed to False.

copy_headerbool, default=True

Indicated if the header of the reference image should be used to create the new image. Ignored for SurfaceImage.

Ignored for SurfaceImage.

Added in Nilearn 0.11.0.

Returns:
Nifti1Image
or SurfaceImage

Binarized version of the given input image. Output dtype is int8.

See also

nilearn.image.threshold_img

To simply threshold but not binarize images.

Examples

Let’s load an image using nilearn datasets module:

>>> from nilearn import datasets
>>> anatomical_image = datasets.load_mni152_template()

Now we binarize it, generating a pseudo brainmask:

>>> from nilearn.image import binarize_img
>>> img = binarize_img(anatomical_image)