Table of Contents

Class BoundingBox<T>

Namespace
AiDotNet.Augmentation.Image
Assembly
AiDotNet.dll

Represents a bounding box annotation for object detection.

public class BoundingBox<T>

Type Parameters

T

The numeric type for coordinates.

Inheritance
BoundingBox<T>
Inherited Members

Remarks

Bounding boxes are rectangular regions that localize objects in images. This class supports multiple coordinate formats used by different frameworks and can convert between them.

For Beginners: A bounding box is simply a rectangle around an object in an image, defined by its coordinates. Different systems use different ways to specify these coordinates (corners vs center, pixels vs percentages).

Constructors

BoundingBox()

Creates an empty bounding box.

public BoundingBox()

BoundingBox(T, T, T, T, BoundingBoxFormat, int)

Creates a bounding box with the specified coordinates.

public BoundingBox(T x1, T y1, T x2, T y2, BoundingBoxFormat format = BoundingBoxFormat.XYXY, int classIndex = 0)

Parameters

x1 T

The first X coordinate.

y1 T

The first Y coordinate.

x2 T

The second X coordinate or width.

y2 T

The second Y coordinate or height.

format BoundingBoxFormat

The coordinate format.

classIndex int

The class label index.

Properties

ClassIndex

Gets or sets the class label index.

public int ClassIndex { get; set; }

Property Value

int

ClassName

Gets or sets the class label name.

public string? ClassName { get; set; }

Property Value

string

Confidence

Gets or sets the confidence score (if from a detector).

public T? Confidence { get; set; }

Property Value

T

Format

Gets or sets the current coordinate format.

public BoundingBoxFormat Format { get; set; }

Property Value

BoundingBoxFormat

ImageHeight

Gets or sets the image height (needed for normalized formats).

public int ImageHeight { get; set; }

Property Value

int

ImageWidth

Gets or sets the image width (needed for normalized formats).

public int ImageWidth { get; set; }

Property Value

int

Metadata

Gets or sets additional metadata.

public IDictionary<string, object>? Metadata { get; set; }

Property Value

IDictionary<string, object>

X1

Gets or sets the X coordinate of the first point.

public T X1 { get; set; }

Property Value

T

Remarks

Interpretation depends on format: x_min (XYXY, XYWH), x_center (CXCYWH, YOLO).

X2

Gets or sets the X coordinate of the second point or width.

public T X2 { get; set; }

Property Value

T

Y1

Gets or sets the Y coordinate of the first point.

public T Y1 { get; set; }

Property Value

T

Y2

Gets or sets the Y coordinate of the second point or height.

public T Y2 { get; set; }

Property Value

T

Methods

Area()

Calculates the area of this bounding box.

public double Area()

Returns

double

The area in square pixels.

Clip(int, int)

Clips this bounding box to image boundaries.

public void Clip(int width, int height)

Parameters

width int

The image width.

height int

The image height.

Clone()

Creates a deep copy of this bounding box.

public BoundingBox<T> Clone()

Returns

BoundingBox<T>

A new bounding box with the same values.

IoU(BoundingBox<T>)

Calculates the IoU (Intersection over Union) with another bounding box.

public double IoU(BoundingBox<T> other)

Parameters

other BoundingBox<T>

The other bounding box.

Returns

double

The IoU value between 0 and 1.

IsValid()

Checks if this bounding box is valid (has positive area).

public bool IsValid()

Returns

bool

True if the box has positive width and height.

ToCXCYWH()

Converts this bounding box to CXCYWH format.

public (double cx, double cy, double width, double height) ToCXCYWH()

Returns

(double xMin, double yMin, double xMax, double yMax)

Coordinates as [x_center, y_center, width, height].

ToXYWH()

Converts this bounding box to XYWH format.

public (double x, double y, double width, double height) ToXYWH()

Returns

(double xMin, double yMin, double xMax, double yMax)

Coordinates as [x_min, y_min, width, height].

ToXYXY()

Converts this bounding box to XYXY format.

public (double xMin, double yMin, double xMax, double yMax) ToXYXY()

Returns

(double xMin, double yMin, double xMax, double yMax)

Coordinates as [x_min, y_min, x_max, y_max].

ToYOLO()

Converts this bounding box to YOLO format (normalized).

public (double cx, double cy, double width, double height) ToYOLO()

Returns

(double xMin, double yMin, double xMax, double yMax)

Coordinates as [x_center, y_center, width, height] normalized to [0, 1].