Shortcuts

masks_to_boxes

torchvision.ops.masks_to_boxes(masks: Tensor) Tensor[source]

Compute the bounding boxes around the provided masks.

Returns a [N, 4] tensor containing bounding boxes. The boxes are in (x1, y1, x2, y2) format with 0 <= x1 <= x2 and 0 <= y1 <= y2.

Warning

In most cases the output will guarantee x1 < x2 and y1 < y2. But if the input is degenerate, e.g. if a mask is a single row or a single column, then the output may have x1 = x2 or y1 = y2.

Parameters:

masks (Tensor[N, H, W]) – masks to transform where N is the number of masks and (H, W) are the spatial dimensions.

Returns:

bounding boxes

Return type:

Tensor[N, 4]

Examples using masks_to_boxes:

Repurposing masks into bounding boxes

Repurposing masks into bounding boxes

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources