code: def transform_by_mapping(
      img, mask, mapping, cutoff, type_hist="quartile"
  ):
      """
      Performs the standardisation of a given image.
  
      :param img: image to standardise
      :param mask: mask over which to determine the landmarks
      :param mapping: mapping landmarks to use for the piecewise linear
          transformations
      :param cutoff: cutoff points for the mapping
      :param type_hist: Type of landmarks scheme to use: choice between
          quartile percentile and median
      :return new_img: the standardised image
      """
      image_shape = img.shape
      img = img.reshape(-1)
      mask = mask.reshape(-1)
  
      type_hist = look_up_operations(
          type_hist.lower(), SUPPORTED_CUTPOINTS
      )
      if type_hist == "quartile":
          range_to_use = [0, 3, 6, 9, 12]
      elif type_hist == "percentile":
          range_to_use = [0, 1, 2, 4, 5, 6, 7, 8, 10, 11, 12]
      elif type_hist == "median":
          range_to_use = [0, 6, 12]
      else:
          raise ValueError("unknown cutting points type_str")
      assert len(mapping) >= len(
          range_to_use
      ), "wrong mapping format, please check the histogram reference file"
      mapping = np.asarray(mapping)
      cutoff = __standardise_cutoff(cutoff, type_hist)
      perc = __compute_percentiles(img, mask, cutoff)
      # Apply linear histogram standardisation
      range_mapping = mapping[range_to_use]
      range_perc = perc[range_to_use]
      diff_mapping = range_mapping[1:] - range_mapping[:-1]
      diff_perc = range_perc[1:] - range_perc[:-1]
  
      # handling the case where two landmarks are the same
      # for a given input image.  This usually happens when
      # image background are not removed from the image.
      diff_perc[diff_perc == 0] = np.inf
  
      affine_map = np.zeros([2, len(range_to_use) - 1])
      # compute slopes of the linear models
      affine_map[0] = diff_mapping / diff_perc
      # compute intercepts of the linear models
      affine_map[1] = (
          range_mapping[:-1] - affine_map[0] * range_perc[:-1]
      )
  
      bin_id = np.digitize(img, range_perc[1:-1], right=False)
      lin_img = affine_map[0, bin_id]
      aff_img = affine_map[1, bin_id]
      # handling below cutoff[0] over cutoff[1]
      # values are mapped linearly and then smoothed
      new_img = lin_img * img + aff_img
  
      # Apply smooth thresholding (exponential)
      # below cutoff[0] and over cutoff[1]
      # this might not guarantee one to one mapping
  
      # lowest_values = img <= range_perc[0]
      # highest_values = img >= range_perc[-1]
      # new_img[lowest_values] = smooth_threshold(
      #     new_img[lowest_values], mode='low')
      # new_img[highest_values] = smooth_threshold(
      #     new_img[highest_values], mode='high')
  
      # Apply mask and set background to zero
      # new_img[mask == False] = 0.
      new_img = new_img.reshape(image_shape)
      return new_img
  
file_path: NiftyNet/niftynet/utilities/histogram_standardisation.py
imports: from __future__ import absolute_import, print_function, division
  import os
  import numpy
  import numpy.ma
  import tensorflow
  from niftynet.io.misc_io import touch_folder
  from niftynet.utilities.util_common import look_up_operations, print_progress_bar
label_desc: Use image node_id 6 for calling a global function with example usage: transform_by_mapping(img, mask, mapping, cutoff, type_hist) and returns: new_img