Pairwise distance between N points and M boxes. The distance between a point and a box is represented by the distance from the point to 4 edges of the box. Distances are all positive when the point is inside the box. Args: points: Nx2 coordinates. Each row is (x, y)
(points: torch.Tensor, boxes: Boxes)
| 377 | |
| 378 | |
| 379 | def pairwise_point_box_distance(points: torch.Tensor, boxes: Boxes): |
| 380 | """ |
| 381 | Pairwise distance between N points and M boxes. The distance between a |
| 382 | point and a box is represented by the distance from the point to 4 edges |
| 383 | of the box. Distances are all positive when the point is inside the box. |
| 384 | |
| 385 | Args: |
| 386 | points: Nx2 coordinates. Each row is (x, y) |
| 387 | boxes: M boxes |
| 388 | |
| 389 | Returns: |
| 390 | Tensor: distances of size (N, M, 4). The 4 values are distances from |
| 391 | the point to the left, top, right, bottom of the box. |
| 392 | """ |
| 393 | x, y = points.unsqueeze(dim=2).unbind(dim=1) # (N, 1) |
| 394 | x0, y0, x1, y1 = boxes.tensor.unsqueeze(dim=0).unbind(dim=2) # (1, M) |
| 395 | return torch.stack([x - x0, y - y0, x1 - x, y1 - y], dim=2) |
| 396 | |
| 397 | |
| 398 | def matched_pairwise_iou(boxes1: Boxes, boxes2: Boxes) -> torch.Tensor: |