Skip to content

clip_boxes_to_image can drop valid boxes for large coordinates #9045

Description

@Rajioba1

Describe the bug
spatial_crop_boxes converts roi_start and roi_end to torch.int16 before clamping boxes. For coordinates >= 32768, the ROI bounds overflow/wrap, so boxes that are fully inside the requested crop can be clamped to zero width and dropped when remove_empty=True.

This is also reachable through the public clip_boxes_to_image API for large images.

To Reproduce

import torch
from monai.data.box_utils import clip_boxes_to_image, spatial_crop_boxes

boxes = torch.tensor([[41000.0, 5000.0, 45000.0, 15000.0]], dtype=torch.float32)

cropped, keep = spatial_crop_boxes(
    boxes,
    roi_start=[40000, 0],
    roi_end=[50000, 20000],
    remove_empty=True,
)
print(keep.tolist(), cropped.tolist())

clipped, keep = clip_boxes_to_image(boxes, spatial_size=[50000, 50000], remove_empty=True)
print(keep.tolist(), clipped.tolist())

Current output:

[False] []
[False] []

Expected behavior
The box is inside the crop/image and should be kept. For the crop example, the expected cropped box is:

[True] [[1000.0, 5000.0, 5000.0, 15000.0]]

For the clip_boxes_to_image example, the expected clipped box is unchanged and kept.

Environment

MONAI version: 1.6.0rc1+48.g8690ae74
Numpy version: 1.26.4
Pytorch version: 2.10.0+cpu
MONAI rev id: 8690ae74a8a489d31fe1f9ac8ef0bff63165383e
System: Windows-11-10.0.26200-SP0
Python version: 3.12.1

Additional context
The overflow appears to come from the .to(torch.int16) casts on the converted ROI bounds in monai/data/box_utils.py. Whole-slide pathology and detection workflows can use image coordinates larger than 32767, so this can silently remove valid detections instead of raising an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions