code: def extract_connect_compoent(binary_mask, minimum_volume=0):
      """
      extract connect compoent from binary mask
      binary mask -> mask w/ [0, 1, 2, ...]
      0 - background
      1 - foreground instance #1 (start with 1)
      2 - foreground instance #2
      """
      assert (
          len(np.unique(binary_mask)) < 3
      ), "Only binary mask is accepted, got mask with {}.".format(
          np.unique(binary_mask).tolist()
      )
      instance_mask = sitk.GetArrayFromImage(
          sitk.RelabelComponent(
              sitk.ConnectedComponent(
                  sitk.GetImageFromArray(binary_mask)
              ),
              minimumObjectSize=minimum_volume,
          )
      )
      return instance_mask
  
imports: from typing import List, Tuple, Union, Callable
  import collections
  import numbers
  import random
  import numpy
  import scipy
  import scipy.ndimage
  import SimpleITK
  from scipy.ndimage.filters import gaussian_filter, gaussian_gradient_magnitude
  from skimage.transform import resize
label_desc: Use image node_id 7 for calling a global function with example usage: extract_connect_compoent(binary_mask, minimum_volume) and returns: instance_mask