def swapaxes_boxes(boxes: NdarrayOrTensor, axis1: int, axis2: int): """ Interchange two axes of boxes. Args: boxes: bounding boxes, Nx4 or Nx6 torch tensor or ndarray. The box mode is assumed to be ``StandardMode`` axis1: First axis. axis2: Second axis. Returns: boxes with two axes interchanged. """ spatial_dims: int = get_spatial_dims(boxes=boxes) if isinstance(boxes, torch.Tensor): boxes_swap = boxes.clone() else: boxes_swap = deepcopy(boxes) # type: ignore boxes_swap[:, [axis1, axis2]] = boxes_swap[:, [axis2, axis1]] # type: ignore boxes_swap[:, [spatial_dims + axis1, spatial_dims + axis2]] = boxes_swap[ # type: ignore :, [spatial_dims + axis2, spatial_dims + axis1]] return boxes_swap
def flip_boxes( boxes: NdarrayOrTensor, spatial_size: Union[Sequence[int], int], flip_axes: Optional[Union[Sequence[int], int]] = None, ): """ Flip boxes when the corresponding image is flipped Args: boxes: bounding boxes, Nx4 or Nx6 torch tensor or ndarray. The box mode is assumed to be ``StandardMode`` spatial_size: image spatial size. flip_axes: spatial axes along which to flip over. Default is None. The default `axis=None` will flip over all of the axes of the input array. If axis is negative it counts from the last to the first axis. If axis is a tuple of ints, flipping is performed on all of the axes specified in the tuple. Returns: flipped boxes, with same data type as ``boxes``, does not share memory with ``boxes`` """ spatial_dims: int = get_spatial_dims(boxes=boxes) spatial_size = ensure_tuple_rep(spatial_size, spatial_dims) if flip_axes is None: flip_axes = tuple(range(0, spatial_dims)) flip_axes = ensure_tuple(flip_axes) # flip box if isinstance(boxes, torch.Tensor): _flip_boxes = boxes.clone() else: _flip_boxes = deepcopy(boxes) # type: ignore for axis in flip_axes: _flip_boxes[:, axis + spatial_dims] = spatial_size[ axis] - boxes[:, axis] - TO_REMOVE _flip_boxes[:, axis] = spatial_size[ axis] - boxes[:, axis + spatial_dims] - TO_REMOVE return _flip_boxes