This function converts the boxes in src_mode to the dst_mode. Args: boxes: source bounding boxes, Nx4 or Nx6 torch tensor or ndarray. src_mode: source box mode. If it is not given, this func will assume it is ``StandardMode()``. It follows the same format with `
(
boxes: NdarrayOrTensor,
src_mode: str | BoxMode | type[BoxMode] | None = None,
dst_mode: str | BoxMode | type[BoxMode] | None = None,
)
| 544 | |
| 545 | |
| 546 | def convert_box_mode( |
| 547 | boxes: NdarrayOrTensor, |
| 548 | src_mode: str | BoxMode | type[BoxMode] | None = None, |
| 549 | dst_mode: str | BoxMode | type[BoxMode] | None = None, |
| 550 | ) -> NdarrayOrTensor: |
| 551 | """ |
| 552 | This function converts the boxes in src_mode to the dst_mode. |
| 553 | |
| 554 | Args: |
| 555 | boxes: source bounding boxes, Nx4 or Nx6 torch tensor or ndarray. |
| 556 | src_mode: source box mode. If it is not given, this func will assume it is ``StandardMode()``. |
| 557 | It follows the same format with ``mode`` in :func:`~monai.data.box_utils.get_boxmode`. |
| 558 | dst_mode: target box mode. If it is not given, this func will assume it is ``StandardMode()``. |
| 559 | It follows the same format with ``mode`` in :func:`~monai.data.box_utils.get_boxmode`. |
| 560 | |
| 561 | Returns: |
| 562 | bounding boxes with target mode, with same data type as ``boxes``, does not share memory with ``boxes`` |
| 563 | |
| 564 | Example: |
| 565 | .. code-block:: python |
| 566 | |
| 567 | boxes = torch.ones(10,4) |
| 568 | # The following three lines are equivalent |
| 569 | # They convert boxes with format [xmin, ymin, xmax, ymax] to [xcenter, ycenter, xsize, ysize]. |
| 570 | convert_box_mode(boxes=boxes, src_mode="xyxy", dst_mode="ccwh") |
| 571 | convert_box_mode(boxes=boxes, src_mode="xyxy", dst_mode=monai.data.box_utils.CenterSizeMode) |
| 572 | convert_box_mode(boxes=boxes, src_mode="xyxy", dst_mode=monai.data.box_utils.CenterSizeMode()) |
| 573 | """ |
| 574 | # handle empty box |
| 575 | if boxes.shape[0] == 0: |
| 576 | return boxes |
| 577 | |
| 578 | src_boxmode = get_boxmode(src_mode) |
| 579 | dst_boxmode = get_boxmode(dst_mode) |
| 580 | |
| 581 | # if mode not changed, deepcopy the original boxes |
| 582 | if isinstance(src_boxmode, type(dst_boxmode)): |
| 583 | return deepcopy(boxes) |
| 584 | |
| 585 | # convert box mode |
| 586 | # convert numpy to tensor if needed |
| 587 | boxes_t, *_ = convert_data_type(boxes, torch.Tensor) |
| 588 | |
| 589 | # convert boxes to corners |
| 590 | corners = src_boxmode.boxes_to_corners(boxes_t) |
| 591 | |
| 592 | # check validity of corners |
| 593 | spatial_dims = get_spatial_dims(boxes=boxes_t) |
| 594 | for axis in range(spatial_dims): |
| 595 | if (corners[spatial_dims + axis] < corners[axis]).sum() > 0: |
| 596 | warnings.warn("Given boxes has invalid values. The box size must be non-negative.") |
| 597 | |
| 598 | # convert corners to boxes |
| 599 | boxes_t_dst = dst_boxmode.corners_to_boxes(corners) |
| 600 | |
| 601 | # convert tensor back to numpy if needed |
| 602 | boxes_dst, *_ = convert_to_dst_type(src=boxes_t_dst, dst=boxes) |
| 603 | return boxes_dst |
searching dependent graphs…