MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / _box_inter_union

Function _box_inter_union

monai/data/box_utils.py:782–817  ·  view source on GitHub ↗

This internal function computes the intersection and union area of two set of boxes. Args: boxes1: bounding boxes, Nx4 or Nx6 torch tensor. The box mode is assumed to be ``StandardMode`` boxes2: bounding boxes, Mx4 or Mx6 torch tensor. The box mode is assumed to be ``Standa

(
    boxes1_t: torch.Tensor, boxes2_t: torch.Tensor, compute_dtype: torch.dtype = torch.float32
)

Source from the content-addressed store, hash-verified

780
781
782def _box_inter_union(
783 boxes1_t: torch.Tensor, boxes2_t: torch.Tensor, compute_dtype: torch.dtype = torch.float32
784) -> tuple[torch.Tensor, torch.Tensor]:
785 """
786 This internal function computes the intersection and union area of two set of boxes.
787
788 Args:
789 boxes1: bounding boxes, Nx4 or Nx6 torch tensor. The box mode is assumed to be ``StandardMode``
790 boxes2: bounding boxes, Mx4 or Mx6 torch tensor. The box mode is assumed to be ``StandardMode``
791 compute_dtype: default torch.float32, dtype with which the results will be computed
792
793 Returns:
794 inter, with size of (N,M) and dtype of ``compute_dtype``.
795 union, with size of (N,M) and dtype of ``compute_dtype``.
796
797 """
798 spatial_dims = get_spatial_dims(boxes=boxes1_t)
799
800 # compute area with float32
801 area1 = box_area(boxes=boxes1_t.to(dtype=compute_dtype)) # (N,)
802 area2 = box_area(boxes=boxes2_t.to(dtype=compute_dtype)) # (M,)
803
804 # get the left top and right bottom points for the NxM combinations
805 lt = torch.max(boxes1_t[:, None, :spatial_dims], boxes2_t[:, :spatial_dims]).to(
806 dtype=compute_dtype
807 ) # (N,M,spatial_dims) left top
808 rb = torch.min(boxes1_t[:, None, spatial_dims:], boxes2_t[:, spatial_dims:]).to(
809 dtype=compute_dtype
810 ) # (N,M,spatial_dims) right bottom
811
812 # compute size for the intersection region for the NxM combinations
813 wh = (rb - lt + TO_REMOVE).clamp(min=0) # (N,M,spatial_dims)
814 inter: torch.Tensor = torch.prod(wh, dim=-1, keepdim=False) # (N,M)
815
816 union: torch.Tensor = area1[:, None] + area2 - inter # type: ignore
817 return inter, union
818
819
820def box_iou(boxes1: NdarrayOrTensor, boxes2: NdarrayOrTensor) -> NdarrayOrTensor:

Callers 2

box_iouFunction · 0.85
box_giouFunction · 0.85

Calls 2

get_spatial_dimsFunction · 0.85
box_areaFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…