code: def weighted_variance(image, mask, binary_image):
      
      if not np.any(mask):
          return 0
      #
      # Clamp the dynamic range of the foreground
      #
      minval = np.max(image[mask])/256
      if minval == 0:
          return 0
      
      fg = np.log2(np.maximum(image[binary_image & mask], minval))
      bg = np.log2(np.maximum(image[(~ binary_image) & mask], minval))
      nfg = np.product(fg.shape)
      nbg = np.product(bg.shape)
      if nfg == 0:
          return np.var(bg)
      elif nbg == 0:
          return np.var(fg)
      else:
          return (np.var(fg) * nfg + np.var(bg)*nbg) / (nfg+nbg)
docs: Compute the log-transformed variance of foreground and background
      
      image - intensity image used for thresholding
      
      mask - mask of ignored pixels
      
      binary_image - binary image marking foreground and background