diff --git a/monai/data/box_utils.py b/monai/data/box_utils.py index 2f4a1426a9..b344434715 100644 --- a/monai/data/box_utils.py +++ b/monai/data/box_utils.py @@ -1035,8 +1035,8 @@ def spatial_crop_boxes( # convert to float32 since torch.clamp_ does not support float16 boxes_t = boxes_t.to(dtype=COMPUTE_DTYPE) - roi_start_t = convert_to_dst_type(src=roi_start, dst=boxes_t, wrap_sequence=True)[0].to(torch.int16) - roi_end_t = convert_to_dst_type(src=roi_end, dst=boxes_t, wrap_sequence=True)[0].to(torch.int16) + roi_start_t = convert_to_dst_type(src=roi_start, dst=boxes_t, wrap_sequence=True)[0] + roi_end_t = convert_to_dst_type(src=roi_end, dst=boxes_t, wrap_sequence=True)[0] roi_end_t = torch.maximum(roi_end_t, roi_start_t) # makes sure the bounding boxes are within the patch diff --git a/tests/data/test_box_utils.py b/tests/data/test_box_utils.py index 30136d4f1b..71e3270187 100644 --- a/tests/data/test_box_utils.py +++ b/tests/data/test_box_utils.py @@ -35,6 +35,7 @@ convert_box_mode, convert_box_to_standard_mode, non_max_suppression, + spatial_crop_boxes, ) from monai.utils.type_conversion import convert_data_type from tests.test_utils import TEST_NDARRAYS, assert_allclose @@ -269,6 +270,20 @@ def test_integer_truncation_bug(self): self.assertTrue(np.issubdtype(iou.dtype, np.floating)) self.assertGreater(iou[0, 0], 0.0, "IoU should not be truncated to 0") + def test_large_coordinates_are_not_dropped(self): + """Verify large-coordinate boxes are preserved by cropping and clipping.""" + boxes = torch.tensor([[41000.0, 5000.0, 45000.0, 15000.0]], dtype=torch.float32) + + cropped_boxes, keep = spatial_crop_boxes( + boxes=boxes, roi_start=[40000, 0], roi_end=[50000, 20000], remove_empty=True + ) + assert_allclose(keep, torch.tensor([True])) + assert_allclose(cropped_boxes, torch.tensor([[1000.0, 5000.0, 5000.0, 15000.0]])) + + clipped_boxes, keep = clip_boxes_to_image(boxes=boxes, spatial_size=[50000, 50000], remove_empty=True) + assert_allclose(keep, torch.tensor([True])) + assert_allclose(clipped_boxes, boxes) + class TestBatchedNms(unittest.TestCase): @parameterized.expand(TEST_NDARRAYS)