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, Movable, UnknownDestructibility

Aliases

__copyinit__is_trivial

alias __copyinit__is_trivial = True

__del__is_trivial

alias __del__is_trivial = True

__moveinit__is_trivial

alias __moveinit__is_trivial = True

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): Y-coordinate of first corner.
  • x1 (Scalar): X-coordinate of first corner.
  • y2 (Scalar): Y-coordinate of second corner.
  • x2 (Scalar): 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: 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: The intersection area, or 0 if boxes don't overlap.

area

area(self) -> Scalar[dtype]

Calculate the area of this bounding box.

Returns:

Scalar: The area of the box.

Was this page helpful?