Untitled
unknown
plain_text
2 years ago
1.1 kB
18
Indexable
import numpy as np
def calculate_bounding_box(x, y, L, max_width, max_height):
# Calculate half of the length L
half_L = L // 2
# Initialize corners of the bounding box
top_left = [x - half_L, y - half_L]
top_right = [x + half_L, y - half_L]
bottom_left = [x - half_L, y + half_L]
bottom_right = [x + half_L, y + half_L]
# Convert corners into a NumPy array for easier manipulation
corners = np.array([top_left, top_right, bottom_left, bottom_right])
# Clip the corners to ensure they are within the image boundaries
# np.clip(array, min, max)
corners[:, 0] = np.clip(corners[:, 0], 0, max_width) # Clip x coordinates
corners[:, 1] = np.clip(corners[:, 1], 0, max_height) # Clip y coordinates
return corners
# Example usage
x, y = 150, 150 # Center coordinate of the box
L = 100 # Length of the bounding box
max_width = 300 # Width of the image
max_height = 300 # Height of the image
corners = calculate_bounding_box(x, y, L, max_width, max_height)
print("Corners of the bounding box:")
print(corners)
Editor is loading...
Leave a Comment