Mojo struct
BoundingBox
struct BoundingBox[dtype: DType]
Represents a 2D bounding box for object detection.
The box is stored using two corner points: nw and se.
Note: In this implementation, nw stores the maximum coordinates (max y, max x)
and se stores the minimum coordinates (min y, min x). This differs from the typical
interpretation of "northwest" (usually min x, max y) and "southeast" (usually max x, min y).
This representation allows efficient computation of intersection and union areas.
Fields: nw: Corner storing the maximum coordinates (max y, max x). se: Corner storing the minimum coordinates (min y, min x).
Parametersβ
- βdtype (
DType): The data type for coordinate values.
Fieldsβ
- βnw (
SIMD[dtype, 2]): - βse (
SIMD[dtype, 2]):
Implemented traitsβ
AnyType,
Copyable,
ImplicitlyCopyable,
ImplicitlyDestructible,
Movable
Methodsβ
__init__β
__init__(out self, y1: Scalar[dtype], x1: Scalar[dtype], y2: Scalar[dtype], x2: Scalar[dtype])
Initialize a bounding box from two diagonal corner coordinates.
Note: The corners are automatically ordered to ensure nw contains the maximum coordinates and se contains the minimum coordinates.
Args:
- βy1 (
Scalar[dtype]): Y-coordinate of first corner. - βx1 (
Scalar[dtype]): X-coordinate of first corner. - βy2 (
Scalar[dtype]): Y-coordinate of second corner. - βx2 (
Scalar[dtype]): X-coordinate of second corner.
iouβ
iou(self, other: Self) -> Scalar[dtype]
Calculate Intersection over Union (IoU) with another bounding box.
Args:
- βother (
Self): The other bounding box to compare with.
Returns:
Scalar[dtype]: The IoU value, ranging from 0 (no overlap) to 1 (perfect overlap).
intersection_areaβ
intersection_area(self, other: Self) -> Scalar[dtype]
Calculate the area of intersection with another bounding box.
Args:
- βother (
Self): The other bounding box to intersect with.
Returns:
Scalar[dtype]: The intersection area, or 0 if boxes don't overlap.
areaβ
area(self) -> Scalar[dtype]
Calculate the area of this bounding box.
Returns:
Scalar[dtype]: The area of the box.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!