code: def binarize_img(img, threshold=0, mask_img=None, two_sided=True):
      """Binarize an image such that its values are either 0 or 1.
  
      .. versionadded:: 0.8.1
  
      Parameters
      ----------
      img : a 3D/4D Niimg-like object
          Image which should be binarized.
  
      threshold : :obj:`float` or :obj:`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_img : Niimg-like object, default=None
          Mask image applied to mask the input data.
          If None, no masking will be applied.
  
      two_sided : :obj:`bool`
          If `True`, threshold is applied to the absolute value of the image.
          If `False`, threshold is applied to the original value of the image.
  
      Returns
      -------
      :class:`~nibabel.nifti1.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)
  
      """
      warnings.warn(
          'The current default behavior for the "two_sided" argument '
          'is  "True". This behavior will be changed to "False" in '
          "version 0.13.",
          DeprecationWarning,
          stacklevel=3,
      )
  
      return math_img(
          "img.astype(bool).astype(int)",
          img=threshold_img(
              img, threshold, mask_img=mask_img, two_sided=two_sided
          ),
      )
  
imports: import collections.abc
  import copy
  import itertools
  import warnings
  import nibabel
  import numpy
  from joblib import Memory, Parallel, delayed
  from scipy.ndimage import gaussian_filter1d, generate_binary_structure, label
  from scipy.stats import scoreatpercentile
  from ..None import signal
  from .._utils import _repr_niimgs, as_ndarray, check_niimg, check_niimg_3d, check_niimg_4d, fill_doc
  from .._utils.exceptions import DimensionError
  from .._utils.helpers import rename_parameters, stringify_path
  from .._utils.niimg import _get_data, safe_get_data
  from .._utils.niimg_conversions import _index_img, check_same_fov, iter_check_niimg
  from .._utils.param_validation import check_threshold
  from .._utils.path_finding import resolve_globbing
label_desc: Use image node_id 19 for calling a global function with example usage: binarize_img(img, threshold, mask_img, two_sided) and returns: math_img