code: def compute_brain_mask(
      target_img,
      threshold=0.5,
      connected=True,
      opening=2,
      memory=None,
      verbose=0,
      mask_type="whole-brain",
  ):
      """Compute the whole-brain, grey-matter or white-matter mask.
  
      This mask is calculated using MNI152 1mm-resolution template mask onto the
      target image.
  
      Parameters
      ----------
      target_img : Niimg-like object
          See :ref:`extracting_data`.
          Images used to compute the mask. 3D and 4D images are accepted.
          Only the shape and affine of ``target_img`` will be used here.
  
      threshold : :obj:`float`, default=0.5
          The value under which the :term:`MNI` template is cut off.
      %(connected)s
          Default=True.
      %(opening)s
          Default=2.
      %(memory)s
      %(verbose0)s
      %(mask_type)s
  
          .. versionadded:: 0.8.1
  
      Returns
      -------
      mask : :class:`nibabel.nifti1.Nifti1Image`
          The whole-brain mask (3D image).
      """
      if verbose > 0:
          print("Template", mask_type, "mask computation")
  
      target_img = _utils.check_niimg(target_img)
  
      if mask_type == "whole-brain":
          template = load_mni152_template(resolution=1)
      elif mask_type == "gm":
          template = load_mni152_gm_template(resolution=1)
      elif mask_type == "wm":
          template = load_mni152_wm_template(resolution=1)
      else:
          raise ValueError(
              f"Unknown mask type {mask_type}. "
              "Only 'whole-brain', 'gm' or 'wm' are accepted."
          )
  
      resampled_template = cache(resampling.resample_to_img, memory)(
          template, target_img
      )
  
      mask = (get_data(resampled_template) >= threshold).astype("int8")
  
      warning_message = (
          f"{mask_type} mask is empty, "
          "lower the threshold or check your input FOV"
      )
      mask, affine = _post_process_mask(
          mask,
          target_img.affine,
          opening=opening,
          connected=connected,
          warning_msg=warning_message,
      )
  
      return new_img_like(target_img, mask, affine)
  
imports: import numbers
  import warnings
  import numpy
  from joblib import Parallel, delayed
  from scipy.ndimage import binary_dilation, binary_erosion
  from .None import _utils
  from ._utils import fill_doc
  from ._utils.cache_mixin import cache
  from ._utils.ndimage import get_border_data, largest_connected_component
  from ._utils.niimg import safe_get_data
  from .datasets import load_mni152_gm_template, load_mni152_template, load_mni152_wm_template
  from .image import get_data, new_img_like, resampling
label_desc: Use image node_id 9 for calling a global function with example usage: compute_brain_mask(target_img, threshold, connected, opening, memory, verbose, mask_type) and returns: new_img_like