Introduction
Bounding boxes help computers find and locate objects in images by drawing rectangles around them.
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])
boxes = torch.tensor([[15, 25, 55, 65]]) area = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) print(area)
boxes = torch.tensor([[10, 20, 50, 60], [30, 40, 70, 80]]) widths = boxes[:, 2] - boxes[:, 0] heights = boxes[:, 3] - boxes[:, 1] print(widths, heights)
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)
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}")
import torch boxes = torch.tensor([[1, 2, 4, 6], [0, 0, 3, 3]]) widths = boxes[:, 2] - boxes[:, 0] heights = boxes[:, 3] - boxes[:, 1] areas = widths * heights print(areas.tolist())
import torch boxes = torch.tensor([1, 1, 5, 5]) points = torch.tensor([[3, 3], [6, 6]]) inside = (points[:, 0] >= boxes[:, 0]) & (points[:, 0] <= boxes[:, 2]) & (points[:, 1] >= boxes[:, 1]) & (points[:, 1] <= boxes[:, 3]) print(inside)
boxes of shape (N, 4)?Pass the test and this pattern moves unknown