IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /max/get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /max/get-started.md).

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, SIMDSize(2)]):
  • ​se (SIMD[dtype, SIMDSize(2)]):

Implemented traits​

AnyType, Copyable, ImplicitlyCopyable, ImplicitlyDeletable, Movable

Methods​

__init__​

def __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​

def 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​

def 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​

def area(self) -> Scalar[dtype]

Calculate the area of this bounding box.

Returns:

Scalar[dtype]: The area of the box.