code: def convert_mask_to_box(
      boxes_mask: NdarrayOrTensor,
      bg_label: int = -1,
      box_dtype: DtypeLike | torch.dtype = torch.float32,
      label_dtype: DtypeLike | torch.dtype = torch.long,
  ) -> tuple[NdarrayOrTensor, NdarrayOrTensor]:
      """
      Convert int16 mask image to box, which has the same size with the input image
  
      Args:
          boxes_mask: int16 array, sized (num_box, H, W). Each channel represents a box.
              The foreground region in channel c has intensity of labels[c].
              The background intensity is bg_label.
          bg_label: background labels for the boxes_mask
          box_dtype: output dtype for boxes
          label_dtype: output dtype for labels
  
      Return:
          - bounding boxes, Nx4 or Nx6 torch tensor or ndarray. The box mode is assumed to be ``StandardMode``.
          - classification foreground(fg) labels, dtype should be int, sized (N,).
      """
      look_up_option(len(boxes_mask.shape), [3, 4])
      spatial_size = list(boxes_mask.shape[1:])
      spatial_dims = get_spatial_dims(spatial_size=spatial_size)
  
      boxes_mask_np, *_ = convert_data_type(boxes_mask, np.ndarray)
  
      boxes_list = []
      labels_list = []
      for b in range(boxes_mask_np.shape[0]):
          fg_indices = np.nonzero(boxes_mask_np[b, ...] - bg_label)
          if fg_indices[0].shape[0] == 0:
              continue
          boxes_b = []
          for fd_i in fg_indices:
              boxes_b.append(min(fd_i))  # top left corner
          for fd_i in fg_indices:
              boxes_b.append(
                  max(fd_i) + 1 - TO_REMOVE
              )  # bottom right corner
          boxes_list.append(boxes_b)
          if spatial_dims == 2:
              labels_list.append(
                  boxes_mask_np[b, fg_indices[0][0], fg_indices[1][0]]
              )
          if spatial_dims == 3:
              labels_list.append(
                  boxes_mask_np[
                      b,
                      fg_indices[0][0],
                      fg_indices[1][0],
                      fg_indices[2][0],
                  ]
              )
  
      if len(boxes_list) == 0:
          boxes_np, labels_np = np.zeros(
              [0, 2 * spatial_dims]
          ), np.zeros([0])
      else:
          boxes_np, labels_np = np.asarray(boxes_list), np.asarray(
              labels_list
          )
      boxes, *_ = convert_to_dst_type(
          src=boxes_np, dst=boxes_mask, dtype=box_dtype
      )
      labels, *_ = convert_to_dst_type(
          src=labels_np, dst=boxes_mask, dtype=label_dtype
      )
      return boxes, labels
  
imports: from __future__ import annotations
  from collections.abc import Sequence
  from copy import deepcopy
  import numpy
  import torch
  from monai.config.type_definitions import DtypeLike, NdarrayOrTensor, NdarrayTensor
  from monai.data.box_utils import COMPUTE_DTYPE, TO_REMOVE, get_spatial_dims
  from monai.transforms import Resize
  from monai.transforms.utils import create_scale
  from monai.utils import look_up_option
  from monai.utils.misc import ensure_tuple, ensure_tuple_rep
  from monai.utils.type_conversion import convert_data_type, convert_to_dst_type
label_desc: Use image node_id 7 for calling a global function with example usage: convert_mask_to_box(boxes_mask, bg_label, box_dtype, label_dtype) and returns: boxes,  labels