code: def debayer_bilinear_npp_masks(img, masks):
      """
      Computes the "bilinear with chroma correction for green channel" as
      defined by the NPP.
      """
      in_dtype = img.dtype
      ndim = len(img.shape)
      assert ndim in (2, 3)
      is_seq = ndim == 3
      conv = conv2d_border101 if not is_seq else conv2d_border101_seq
      red_mask, green_mask, blue_mask = masks
      red_signal = img * red_mask
      green_signal = img * green_mask
      blue_signal = img * blue_mask
      # When inferring red color for blue or green base, there are either
      # four red base neighbors at four corners or two base neighbors in
      # x or y axis. The blue color case is analogous.
      rb_filter = np.array(
          [[1, 2, 1], [2, 4, 2], [1, 2, 1]], dtype=np.int32
      )
      green_x_filter = np.array([[1, 0, 1]], dtype=np.int32)
      green_y_filter = green_x_filter.transpose()
      green_filter = np.array(
          [[0, 1, 0], [1, 4, 1], [0, 1, 0]], dtype=np.int32
      )
      red = conv(red_signal, rb_filter) // 4
      blue = conv(blue_signal, rb_filter) // 4
      green_bilinear = conv(green_signal, green_filter) // 4
      green_x = conv(green_signal, green_x_filter) // 2
      green_y = conv(green_signal, green_y_filter) // 2
  
      def green_with_chroma_correlation(color_signal):
          # For red and blue based positions, there are always four
          # green neighbors (y - 1, x), (y + 1, x), (y, x - 1), (y, x + 1).
          # NPP does not simply average 4 of them to get green intensity.
          # Instead, it averages only two in either y or x axis as explained in
          # https://docs.nvidia.com/cuda/npp/group__image__color__debayer.html
          # I.e. the axis is chosen by looking at:
          # * abs(color(x, y), avg(color(y - 2, x), color(y + 2, x))) and
          # * abs(color(x, y), avg(color(y, x - 2), color(y, x - 2)))
          # and choosing the axis where the difference is smaller.
          # In other words, if we are inferring green color for blue(red)-based
          # position we check in which axis the blue(red) intensity changes less
          # and pick that axis.
          diff_filter_x = np.array([[1, 0, 0, 0, 1]], dtype=np.int32)
          diff_filter_y = diff_filter_x.transpose()
          # First compute the average, then the difference. Doing it with a single
          # conv yields different results (and as this servers as a mask,
          # it results in substantial differences in the end)
          x_avg = conv(color_signal, diff_filter_x) // 2
          y_avg = conv(color_signal, diff_filter_y) // 2
          diff_x = np.abs(color_signal - x_avg)
          diff_y = np.abs(color_signal - y_avg)
          return diff_x < diff_y, diff_x > diff_y
  
      (
          pick_x_red_based,
          pick_y_red_based,
      ) = green_with_chroma_correlation(red_signal)
      (
          pick_x_blue_based,
          pick_y_blue_based,
      ) = green_with_chroma_correlation(blue_signal)
      pick_x = pick_x_red_based + pick_x_blue_based
      pick_y = pick_y_red_based + pick_y_blue_based
      green = (
          pick_x * green_x
          + pick_y * green_y
          + (1 - pick_x - pick_y) * green_bilinear
      )
      return np.stack([red, green, blue], axis=ndim).astype(in_dtype)
  
file_path: DALI/dali/test/python/debayer_test_utils.py
imports: import numpy
  from scipy.signal import convolve2d
label_desc: Use image node_id 8 for calling a global function with example usage: debayer_bilinear_npp_masks(img, masks) and returns: np, unknown,  unknown