Class BoundingBox<T>
- Namespace
- AiDotNet.Augmentation.Image
- Assembly
- AiDotNet.dll
Represents a bounding box annotation for object detection.
public class BoundingBox<T>
Type Parameters
TThe 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
x1TThe first X coordinate.
y1TThe first Y coordinate.
x2TThe second X coordinate or width.
y2TThe second Y coordinate or height.
formatBoundingBoxFormatThe coordinate format.
classIndexintThe class label index.
Properties
ClassIndex
Gets or sets the class label index.
public int ClassIndex { get; set; }
Property Value
ClassName
Gets or sets the class label name.
public string? ClassName { get; set; }
Property Value
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
ImageHeight
Gets or sets the image height (needed for normalized formats).
public int ImageHeight { get; set; }
Property Value
ImageWidth
Gets or sets the image width (needed for normalized formats).
public int ImageWidth { get; set; }
Property Value
Metadata
Gets or sets additional metadata.
public IDictionary<string, object>? Metadata { get; set; }
Property Value
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
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
otherBoundingBox<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
ToXYXY()
Converts this bounding box to XYXY format.
public (double xMin, double yMin, double xMax, double yMax) ToXYXY()
Returns
ToYOLO()
Converts this bounding box to YOLO format (normalized).
public (double cx, double cy, double width, double height) ToYOLO()