sanitize_bounding_boxes computes the wrong height for rotated boxes
Author: u7k4rs6Created Sep 30, 2026Updated Sep 30, 2026
Describe the bug
sanitize_bounding_boxes (and SanitizeBoundingBoxes) don't filter rotated boxes correctly. Thin boxes survive min_size, and min_area sees an inflated area.
Looks like an off-by-one in _get_sanitize_bounding_boxes_mask in torchvision/transforms/v2/functional/_misc.py. After converting to XYXYXYXY it computes:
dx23 = bounding_boxes[..., 3] - bounding_boxes[..., 5] # y2 - y3
dy23 = bounding_boxes[..., 4] - bounding_boxes[..., 6] # x3 - x4so the "height" mixes coordinates from different corners (for an axis-aligned box it ends up being the diagonal). It should be indices 2/4 and 3/5.
Repro:
import torch
from torchvision import tv_tensors
from torchvision.transforms.v2 import functional as F
boxes = tv_tensors.BoundingBoxes(
torch.tensor([[20, 20, 10, 0.5, 0], [20, 20, 10, 0.5, 30], [20, 20, 10, 10, 0]]),
format="CXCYWHR", canvas_size=(40, 40),
)
print(F.sanitize_bounding_boxes(boxes, min_size=1)[1])
# tensor([True, True, True]), expected tensor([False, False, True])
xyxy = tv_tensors.BoundingBoxes(torch.tensor([[15, 19.75, 25, 20.25]]), format="XYXY", canvas_size=(40, 40))
print(F.sanitize_bounding_boxes(xyxy, min_size=1)[1])
# tensor([False]), same box as the first one above, correctly removedIt hits CXCYWHR, XYWHR and XYXYXYXY. The existing sanitize tests only use XYXY, which is probably why it slipped through. Happy to send a PR with the fix and a test.
Versions
torch 2.14.0+cpu, torchvision 0.29.0+cpu, Python 3.14, Linux. The same code is on main (6da25ff).
Source: pytorch/vision