Distance of center points between two sets of boxes Args: boxes1: bounding boxes, Nx4 or Nx6 torch tensor or ndarray. The box mode is assumed to be ``StandardMode`` boxes2: bounding boxes, Mx4 or Mx6 torch tensor or ndarray. The box mode is assumed to be ``StandardMode``
(
boxes1: NdarrayOrTensor, boxes2: NdarrayOrTensor, euclidean: bool = True
)
| 678 | |
| 679 | |
| 680 | def boxes_center_distance( |
| 681 | boxes1: NdarrayOrTensor, boxes2: NdarrayOrTensor, euclidean: bool = True |
| 682 | ) -> tuple[NdarrayOrTensor, NdarrayOrTensor, NdarrayOrTensor]: |
| 683 | """ |
| 684 | Distance of center points between two sets of boxes |
| 685 | |
| 686 | Args: |
| 687 | boxes1: bounding boxes, Nx4 or Nx6 torch tensor or ndarray. The box mode is assumed to be ``StandardMode`` |
| 688 | boxes2: bounding boxes, Mx4 or Mx6 torch tensor or ndarray. The box mode is assumed to be ``StandardMode`` |
| 689 | euclidean: computed the euclidean distance otherwise it uses the l1 distance |
| 690 | |
| 691 | Returns: |
| 692 | - The pairwise distances for every element in boxes1 and boxes2, |
| 693 | with size of (N,M) and same data type as ``boxes1``. |
| 694 | - Center points of boxes1, with size of (N,spatial_dims) and same data type as ``boxes1``. |
| 695 | - Center points of boxes2, with size of (M,spatial_dims) and same data type as ``boxes1``. |
| 696 | |
| 697 | Reference: |
| 698 | https://github.com/MIC-DKFZ/nnDetection/blob/main/nndet/core/boxes/ops.py |
| 699 | |
| 700 | """ |
| 701 | |
| 702 | if not isinstance(boxes1, type(boxes2)): |
| 703 | warnings.warn(f"boxes1 is {type(boxes1)}, while boxes2 is {type(boxes2)}. The result will be {type(boxes1)}.") |
| 704 | |
| 705 | # convert numpy to tensor if needed |
| 706 | boxes1_t, *_ = convert_data_type(boxes1, torch.Tensor) |
| 707 | boxes2_t, *_ = convert_data_type(boxes2, torch.Tensor) |
| 708 | |
| 709 | center1 = box_centers(boxes1_t.to(COMPUTE_DTYPE)) # (N, spatial_dims) |
| 710 | center2 = box_centers(boxes2_t.to(COMPUTE_DTYPE)) # (M, spatial_dims) |
| 711 | |
| 712 | if euclidean: |
| 713 | dists = (center1[:, None] - center2[None]).pow(2).sum(-1).sqrt() # type: ignore |
| 714 | else: |
| 715 | # before sum: (N, M, spatial_dims) |
| 716 | dists = (center1[:, None] - center2[None]).sum(-1) |
| 717 | |
| 718 | # convert tensor back to numpy if needed |
| 719 | (dists, center1, center2), *_ = convert_to_dst_type(src=(dists, center1, center2), dst=boxes1) |
| 720 | return dists, center1, center2 |
| 721 | |
| 722 | |
| 723 | def is_valid_box_values(boxes: NdarrayOrTensor) -> bool: |
searching dependent graphs…