Skip to main content

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:

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.