0
0
PyTorchml~5 mins

Bounding box handling in PyTorch

Choose your learning style9 modes available
Introduction
Bounding boxes help computers find and locate objects in images by drawing rectangles around them.
When you want to detect objects like cars or people in photos.
When you need to crop parts of an image based on object location.
When training a model to recognize and locate multiple objects.
When evaluating how well a model predicts object positions.
When visualizing detected objects on images.
Syntax
PyTorch
import torch

# Bounding box format: [x_min, y_min, x_max, y_max]
boxes = torch.tensor([[10, 20, 50, 60], [30, 40, 70, 80]])

# Example: compute area of bounding boxes
def box_area(boxes):
    return (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])
Bounding boxes are usually stored as four numbers: left, top, right, bottom coordinates.
Coordinates are often in pixels relative to the image size.
Examples
Calculate the area of one bounding box.
PyTorch
boxes = torch.tensor([[15, 25, 55, 65]])
area = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])
print(area)
Get widths and heights of multiple bounding boxes.
PyTorch
boxes = torch.tensor([[10, 20, 50, 60], [30, 40, 70, 80]])
widths = boxes[:, 2] - boxes[:, 0]
heights = boxes[:, 3] - boxes[:, 1]
print(widths, heights)
Find the center point of a bounding box.
PyTorch
boxes = torch.tensor([[10, 20, 50, 60]])
center_x = (boxes[:, 0] + boxes[:, 2]) / 2
center_y = (boxes[:, 1] + boxes[:, 3]) / 2
print(center_x, center_y)
Sample Model
This program calculates the area of two bounding boxes and checks if a point is inside each box.
PyTorch
import torch

# Define bounding boxes: [x_min, y_min, x_max, y_max]
boxes = torch.tensor([[10, 20, 50, 60], [30, 40, 70, 80]])

# Function to compute area of bounding boxes
def box_area(boxes):
    widths = boxes[:, 2] - boxes[:, 0]
    heights = boxes[:, 3] - boxes[:, 1]
    return widths * heights

# Compute areas
areas = box_area(boxes)
print(f"Areas of bounding boxes: {areas.tolist()}")

# Function to check if a point is inside a bounding box
# point: (x, y), box: [x_min, y_min, x_max, y_max]
def is_point_in_box(point, box):
    x, y = point
    return (box[0] <= x <= box[2]) and (box[1] <= y <= box[3])

# Test point
point = (35, 50)
inside_results = [is_point_in_box(point, box) for box in boxes]
print(f"Is point {point} inside each box? {inside_results}")
OutputSuccess
Important Notes
Bounding box coordinates must be consistent (x_min < x_max and y_min < y_max).
When working with images, remember coordinates start at the top-left corner.
You can convert bounding boxes to other formats like center coordinates plus width and height if needed.
Summary
Bounding boxes mark object locations with four numbers: left, top, right, bottom.
You can calculate box area, width, height, or check if points lie inside boxes.
Handling bounding boxes correctly helps train and evaluate object detection models.