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, mask_img=None)[source]#

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

New in version 0.8.1.

Parameters
imga 3D/4D Niimg-like object

Image which should be binarized.

thresholdfloat or str

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, default None, optional

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

Returns
Nifti1Image

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

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)