cv2ext.bboxes package¶
Module contents¶
Subpackage containing tools for working with simple bounding boxes.
Functions¶
bounding()Get a bounding box which encloses all the given bounding boxes.
calculate_metrics()Compute accuracy metrics between two sets of bounding boxes.
constrain()Constrain a bounding box to be within the bounds of an image.
draw_bboxes()Draw bounding boxes on an image.
euclidean()Compute the euclidean distance between two bounding boxes.
filter_bboxes_by_region()Filter a sequence of bounding boxes by a region to contain them in.
iou()Calculate the intersection over union of two bounding boxes.
ious()Calculate the intersection over union of a set of bounding boxes.
manhattan()Compute the manhattan distance between two bounding boxes.
match()Greedily find matches between two lists of bounding boxes.
mean_ap()Calculate the mean average precision of a set of bounding boxes.
nms()Perform non-maximum suppression on a set of bounding boxes.
resize()Resize a bounding box based on one image size to another.
resize_many()Resize a set of bounding boxes based on one image size to another.
score_bbox()Score a bounding box relative to a target bbox.
score_bboxes()Score a set of bounding boxes relative to a target bbox.
valid()Check if a bounding box is valid.
within()Check if a bounding box is within the bounds of an image.
xyxy_to_nxyxy()Convert bounding boxes from (x1, y1, x2, y2) to normalized (x1, y1, x2, y2).
xyxy_to_xywh()Convert bounding boxes from (x1, y1, x2, y2) to (x, y, w, h).
xyxy_to_nxywh()Convert bounding boxes from ;’x1, y1, x2, y2’ to normalized ‘(x, y, w, h)’.
xyxy_to_yolo()Convert bounding boxes from (x1, y1, x2, y2) to (cx, cy, w, h) in YOLO format.
xywh_to_xyxy()Convert bounding boxes from (x, y, w, h) to (x1, y1, x2, y2).
xywh_to_nxyxy()Convert bounding boxes from (x, y, w, h) to normalized (x1, y1, x2, y2).
xywh_to_nxywh()Convert bounding boxes from (x, y, w, h) to normalized (x, y, w, h).
xywh_to_yolo()Convert bounding boxes from (x, y, w, h) to (cx, cy, w, h) in YOLO format.
nxyxy_to_xyxy()Convert bounding boxes from normalized (x1, y1, x2, y2) to (x1, y1, x2, y2).
nxywh_to_xywh()Convert bounding boxes from normalized (x, y, w, h) to (x, y, w, h).
nxywh_to_nxywh()Convert bounding boxes from normalized (x, y, w, h) to normalized (x, y, w, h).
nxyxy_to_yolo()Convert bounding boxes from normalized (x1, y1, x2, y2) to (cx, cy, w, h) in YOLO format.
nxywh_to_xyxy()Convert bounding boxes from normalized (x, y, w, h) to (x1, y1, x2, y2).
nxywh_to_nxyxy()Convert bounding boxes from normalized (x, y, w, h) to normalized (x1, y1, x2, y2).
nxywh_to_xywh()Convert bounding boxes from normalized (x, y, w, h) to (x, y, w, h).
nxywh_to_yolo()Convert bounding boxes from normalized (x, y, w, h) to (cx, cy, w, h) in YOLO format.
yolo_to_xyxy()Convert bounding boxes from YOLO format (cx, cy, w, h) to (x1, y1, x2, y2).
yolo_to_nxyxy()Convert bounding boxes from YOLO format (cx, cy, w, h) to normalized (x1, y1, x2, y2).
yolo_to_xywh()Convert bounding boxes from YOLO format (cx, cy, w, h) to (x, y, w, h).
yolo_to_nxywh()Convert bounding boxes from YOLO format (cx, cy, w, h) to normalized (x, y, w, h).
- cv2ext.bboxes.bounding(bboxes: Sequence[tuple[int, int, int, int]]) tuple[int, int, int, int][source]¶
Get a bounding box which encloses all the given bounding boxes.
- cv2ext.bboxes.calculate_metrics(bboxes1: Sequence[tuple[int, int, int, int] | tuple[tuple[int, int, int, int], float, int]], bboxes2: Sequence[tuple[int, int, int, int] | tuple[tuple[int, int, int, int], float, int]], iou_threshold: float = 0.5, epsilon: float = 1e-06, *, class_agnostic: bool = False) tuple[list[tuple[int, int]], dict[str, float]][source]¶
Compute accuracy metrics between two Sequences of bounding boxes/detections.
Bounding boxes are matched using the greedy algolrithm from
match().- Parameters:
bboxes1 (Sequence[tuple[int, int, int, int] | tuple[tuple[int, int, int, int], float, int]]) – The first Sequence of bounding boxes
bboxes2 (Sequence[tuple[int, int, int, int] | tuple[tuple[int, int, int, int], float, int]]) – The second Sequence of bounding boxes
iou_threshold (float, optional) – The IOU threshold which determines whether two bounding boxes are a match. By default, 0.5
epsilon (float, optional) – The minimum/default value to prevent divide by zero errors. By default, 1e-6
class_agnostic (bool, optional) – Whether or not to compare class ID (if present) By default, False
- Returns:
tuple[ – list[tuple[int, int]], dict[str, float],
] – The list of matching indices and a dict of the computer metrics Metrics are: tp, fp, fn, precision, recall, f1
- cv2ext.bboxes.constrain(bbox: tuple[int, int, int, int], image_size: tuple[int, int]) tuple[int, int, int, int][source]¶
Constrain a bounding box to the dimensions of an image.
Coordinates are not assumed to be positive, and the bounding box is not assumed to be within the image at all. As such, it is possible to return (0, 0, 0, 0) or (width, height, width, height) if the bounding box is completely outside the image.
- cv2ext.bboxes.draw_bboxes(image: np.ndarray, bboxes: Sequence[tuple[int, int, int, int]], confidences: Sequence[float] | None = None, classes: Sequence[str | int] | None = None, class_map: dict[int, str] | None = None, color: Color | tuple[int, int, int] = Color.RED, thickness: int = 2, opacity: float | None = None, *, copy: bool | None = None) np.ndarray[source]¶
Draw bounding boxes on an image.
- Parameters:
image (np.ndarray) – The image to draw the bounding boxes on.
bboxes (Sequence[tuple[int, int, int, int]]) – The Sequence of bounding boxes to draw. The bounding boxes should be in form: x1, y1, x2, y2 (top-left, bottom-right) format
confidences (Sequence[float], optional) – The confidence values for each bounding box. If provided, the confidence values will be drawn on the image.
classes (Sequence[str | int], optional) – The class labels for each bounding box. If provided, the class labels will be drawn on the image with the bounding boxes. If the label is a string, it will be used as is, otherwise it will be used as an index into the class_map.
class_map (dict[int, str], optional) – The class map to use for converting class indices to labels.
color (Color, tuple[int, int, int], optional) – The color to draw the bounding boxes. In BGR format and the default is Color.RED.
thickness (int, optional) – The thickness of the bounding box lines. Default is 2.
opacity (float, optional) – The opacity to draw the bounding boxes with. By default None or 100%. Can have a high performance impact.
copy (bool, optional) – Whether or not to copy the image before drawing. Default is False.
- Returns:
The image with the bounding boxes drawn.
- Return type:
np.ndarray
- cv2ext.bboxes.euclidean(bbox1: tuple[int, int, int, int], bbox2: tuple[int, int, int, int]) float[source]¶
Compute the euclidean distance between two bboxes.
The computation is performed between the centers of the bounding boxes.
- cv2ext.bboxes.filter_bboxes_by_region(bboxes: Sequence[tuple[int, int, int, int]], region: tuple[int, int, int, int], overlap: float = 0.6) list[tuple[int, int, int, int]][source]¶
Get the bounding boxes contained within a region.
- Parameters:
- Returns:
The bounding boxes in the region.
- Return type:
- cv2ext.bboxes.iou(bbox1: tuple[int, int, int, int], bbox2: tuple[int, int, int, int]) float[source]¶
Calculate the intersection over union of two bounding boxes.
- cv2ext.bboxes.ious(bboxes1: list[tuple[int, int, int, int]], bboxes2: list[tuple[int, int, int, int]]) list[float][source]¶
Calculate the intersection over union of two lists of bounding boxes.
- Parameters:
- Returns:
The intersection over union of the two lists of bounding boxes.
- Return type:
- cv2ext.bboxes.manhattan(bbox1: tuple[int, int, int, int], bbox2: tuple[int, int, int, int]) float[source]¶
Compute the manhattan distance between two bboxes.
The computation is performed between the centers of the bounding boxes.
- cv2ext.bboxes.match(bboxes1: Sequence[tuple[int, int, int, int] | tuple[tuple[int, int, int, int], float, int]], bboxes2: Sequence[tuple[int, int, int, int] | tuple[tuple[int, int, int, int], float, int]], iou_threshold: float = 0.5, *, class_agnostic: bool = False) list[tuple[int, int]][source]¶
Match bounding boxes using a greedy algorithm.
- Parameters:
bboxes1 (Sequence[tuple[int, int, int, int] | tuple[tuple[int, int, int, int], float, int]]) – The first Sequence of bounding boxes
bboxes2 (Sequence[tuple[int, int, int, int] | tuple[tuple[int, int, int, int], float, int]]) – The second Sequence of bounding boxes
iou_threshold (float, optional) – The IOU threshold which determines whether two bounding boxes are a match. By default, 0.5
class_agnostic (bool, optional) – Whether or not to compare class ID (if present) By default, False
- Returns:
A list of the matching indices
- Return type:
- cv2ext.bboxes.mean_ap(bboxes: list[list[tuple[tuple[int, int, int, int], int, float]]], gt_bboxes: list[list[tuple[tuple[int, int, int, int], int]]], num_classes: int, iou_threshold: float = 0.5) float[source]¶
Calculate the mean average precision for a set of bounding boxes.
bboxes and gt_bboxes are lists of lists representing the bounding boxes for each image. Each bounding box is represented as a tuple of the form ((x1, y1, x2, y2), class, confidence).
- Parameters:
bboxes (list[list[tuple[tuple[int, int, int, int], int, float]]]) – A list of lists of bounding boxes, each represented as a tuple of the form ((x1, y1, x2, y2), class, confidence
gt_bboxes (list[list[tuple[tuple[int, int, int, int], int]]]) – A list of lists of ground truth bounding boxes, each represented as a tuple of the form ((x1, y1, x2, y2), class)
num_classes (int) – The number of classes in the dataset.
iou_threshold (float, optional) – The threshold for considering a detection a true positive, by default 0.5
- Returns:
The mean average precision of the bounding boxes.
- Return type:
- Raises:
ValueError – If the length of bboxes and gt_bboxes are not equal.
ValueError – If the length is zero.
- cv2ext.bboxes.nms(bboxes: list[tuple[tuple[int, int, int, int], float, int]], iou_threshold: float = 0.5, *, agnostic: bool | None = None) list[tuple[tuple[int, int, int, int], float, int]][source]¶
Perform non-maximum suppression on a list of bounding boxes.
- Parameters:
bboxes (list[tuple[tuple[int, int, int, int], float, int]]) – A list of bounding boxes, each represented as a tuple of the form ((x1, y1, x2, y2), confidence, class
iou_threshold (float) – The intersection over union threshold for non-maximum suppression.
agnostic (bool, optional) – If set to True, then bounding boxes of different classes can be compared. By default None, will only compare same classes.
- Returns:
A list of bounding boxes, each represented as a tuple of the form ((x1, y1, x2, y2), confidence, class
- Return type:
- cv2ext.bboxes.nxywh_to_nxyxy(bbox: tuple[float, float, float, float]) tuple[float, float, float, float][source]¶
Convert a bounding box from normalized (x, y, w, h) format to normalized (x, y, x, y).
- Parameters:
- Returns:
The converted bounding box in normalized format.
- Return type:
- cv2ext.bboxes.nxywh_to_xywh(bbox: tuple[float, float, float, float], image_width: int, image_height: int) tuple[int, int, int, int][source]¶
Convert a bounding box from normalized (x, y, w, h) format to (x, y, w, h).
- Parameters:
- Returns:
The converted bounding box in (x, y, w, h) format.
- Return type:
- cv2ext.bboxes.nxywh_to_xyxy(bbox: tuple[float, float, float, float], image_width: int, image_height: int) tuple[int, int, int, int][source]¶
Convert a bounding box from normalized (x, y, w, h) format to (xmin, ymin, xmax, ymax).
- Parameters:
- Returns:
The converted bounding box in (xmin, ymin, xmax, ymax) format.
- Return type:
- cv2ext.bboxes.nxywh_to_yolo(bbox: tuple[float, float, float, float]) tuple[float, float, float, float][source]¶
Convert a bounding box from normalized (x, y, w, h) to YOLO format (x_center, y_center, width, height).
- cv2ext.bboxes.nxyxy_to_nxywh(bbox: tuple[float, float, float, float], image_width: int, image_height: int) tuple[float, float, float, float][source]¶
Convert a normalized (x, y, x, y) bbox to normalized (x, y, w, h) format.
- Parameters:
- Returns:
The converted bounding box in normalized (x, y, w, h) format.
- Return type:
- cv2ext.bboxes.nxyxy_to_xywh(bbox: tuple[float, float, float, float], image_width: int, image_height: int) tuple[int, int, int, int][source]¶
Convert a normalized (x, y, x, y) bbox to absolute (x, y, w, h) coordinates.
- Parameters:
- Returns:
The converted bounding box in (x, y, w, h) format.
- Return type:
- cv2ext.bboxes.nxyxy_to_xyxy(bbox: tuple[float, float, float, float], image_width: int, image_height: int) tuple[int, int, int, int][source]¶
Convert a normalized (x, y, x, y) bbox to absolute (x, y, x, y) coordinates.
- Parameters:
- Returns:
The converted bounding box in (xmin, ymin, xmax, ymax) format.
- Return type:
- cv2ext.bboxes.nxyxy_to_yolo(bbox: tuple[int, int, int, int]) tuple[float, float, float, float][source]¶
Convert a bounding box from normalized (xmin, ymin, xmax, ymax) to YOLO format (x_center, y_center, width, height).
- cv2ext.bboxes.resize(bbox: tuple[int, int, int, int], s1: tuple[int, int], s2: tuple[int, int]) tuple[int, int, int, int][source]¶
Resizes a bounding box based on one image size to another.
- Parameters:
- Returns:
The resized bounding box. Bounding box is in form xyxy.
- Return type:
- cv2ext.bboxes.resize_many(bboxes: list[tuple[int, int, int, int]], s1: tuple[int, int], s2: tuple[int, int]) list[tuple[int, int, int, int]][source]¶
Resizes a list of bounding boxes based on one image size to another.
- Parameters:
- Returns:
The resized bounding boxes. Bounding boxes are in form xyxy.
- Return type:
- cv2ext.bboxes.score_bbox(target_bbox: tuple[int, int, int, int], pred_bbox: tuple[int, int, int, int]) float[source]¶
Compute a simple score metric for two bounding boxes.
The score is computed as an aggregate between the euclidean distance and the area difference.
- Parameters:
- Returns:
The score between the two bounding boxes.
- Return type:
- cv2ext.bboxes.score_bboxes(target_bbox: tuple[int, int, int, int], pred_bboxs: list[tuple[int, int, int, int]]) list[float][source]¶
Compute a simple score metric for a target bounding box and a list of predicted bounding boxes.
The score is computed as an aggregate between the euclidean distance and the area difference.
- Parameters:
- Returns:
The scores between the target bounding box and the list of predicted bounding boxes.
- Return type:
- cv2ext.bboxes.valid(bbox: tuple[int, int, int, int], shape: tuple[int, int] | None = None) bool[source]¶
Check if a bounding box is valid.
The conditions for a valid bounding box are that: the top-left corner is strictly above the bottom-right corner, and the bounding box has strictly greater than zero area. Can also check if the bounding box is within the bounds of an image.
- Parameters:
- Returns:
True if the bounding box is valid, False otherwise.
- Return type:
- cv2ext.bboxes.within(bbox: tuple[int, int, int, int], shape: tuple[int, int]) bool[source]¶
Check if a bounding box is within the bounds of an image.
The conditions for a bounding box to be within the bounds of an image are that the top-left corner is within the bounds of the image, and the bottom-right corner is within the bounds of the image.
- cv2ext.bboxes.xywh_to_nxywh(bbox: tuple[int, int, int, int], image_width: int, image_height: int) tuple[float, float, float, float][source]¶
Convert a bounding box from (x, y, w, h) to normalized (x, y, w, h) format.
- Parameters:
- Returns:
The converted bounding box in normalized (x, y, w, h) format.
- Return type:
- cv2ext.bboxes.xywh_to_nxyxy(bbox: tuple[int, int, int, int], image_width: int, image_height: int) tuple[float, float, float, float][source]¶
Convert a bounding box from (x, y, w, h) to normalized (x, y, x, y).
- Parameters:
- Returns:
The converted bounding box in normalized format.
- Return type:
- cv2ext.bboxes.xywh_to_xyxy(bbox: tuple[int, int, int, int]) tuple[int, int, int, int][source]¶
Convert a bounding box from (x, y, w, h) to (xmin, ymin, xmax, ymax).
- Parameters:
bbox (tuple[int, int, int, int]) – The bounding box to convert. Bounding box is format (x, y, w, h), where (x, y) is the top-left corner and (w, h) is the width and height.
- Returns:
The converted bounding box. Bounding box is format (xmin, ymin, xmax, ymax), where (xmin, ymin) is the top-left corner and (xmax, ymax) is the bottom-right corner.
- Return type:
- cv2ext.bboxes.xywh_to_yolo(bbox: tuple[int, int, int, int], image_width: int, image_height: int) tuple[float, float, float, float][source]¶
Convert a bounding box from (x, y, w, h) to YOLO format (x_center, y_center, width, height).
- Parameters:
- Returns:
The converted bounding box in YOLO format (x_center, y_center, width, height).
- Return type:
- cv2ext.bboxes.xyxy_to_nxywh(bbox: tuple[int, int, int, int], image_width: int, image_height: int) tuple[float, float, float, float][source]¶
Convert a bounding box from (xmin, ymin, xmax, ymax) to normalized (x, y, w, h) format.
- Parameters:
- Returns:
The converted bounding box in normalized (x, y, w, h) format.
- Return type:
- cv2ext.bboxes.xyxy_to_nxyxy(bbox: tuple[int, int, int, int], image_width: int, image_height: int) tuple[float, float, float, float][source]¶
Convert a bounding box from (x, y, x, y) to normalized (x, y, x, y).
- Parameters:
- Returns:
The converted bounding box in normalized format.
- Return type:
- cv2ext.bboxes.xyxy_to_xywh(bbox: tuple[int, int, int, int]) tuple[int, int, int, int][source]¶
Convert a bounding box from (xmin, ymin, xmax, ymax) to (x, y, w, h).
- Parameters:
bbox (tuple[int, int, int, int]) – The bounding box to convert. Bounding box is format (xmin, ymin, xmax, ymax), where (xmin, ymin) is the top-left corner and (xmax, ymax) is the bottom-right corner.
- Returns:
The converted bounding box. Bounding box is format (x, y, w, h), where (x, y) is the top-left corner and (w, h) is the width and height.
- Return type:
- cv2ext.bboxes.xyxy_to_yolo(bbox: tuple[int, int, int, int], image_width: int, image_height: int) tuple[float, float, float, float][source]¶
Convert a bounding box from (xmin, ymin, xmax, ymax) to YOLO format (x_center, y_center, width, height).
- Parameters:
- Returns:
The converted bounding box in YOLO format (x_center, y_center, width, height).
- Return type:
- cv2ext.bboxes.yolo_to_nxywh(bbox: tuple[float, float, float, float]) tuple[float, float, float, float][source]¶
Convert a YOLO bounding box format to normalized (x, y, w, h).
- cv2ext.bboxes.yolo_to_nxyxy(bbox: tuple[float, float, float, float]) tuple[float, float, float, float][source]¶
Convert a YOLO bounding box format to normalized (xmin, ymin, xmax, ymax).
- cv2ext.bboxes.yolo_to_xywh(bbox: tuple[float, float, float, float], image_width: int, image_height: int) tuple[int, int, int, int][source]¶
Convert a YOLO bounding box format to (x, y, w, h).
- Parameters:
- Returns:
The converted bounding box in (x, y, w, h) format.
- Return type:
- cv2ext.bboxes.yolo_to_xyxy(bbox: tuple[float, float, float, float], image_width: int, image_height: int) tuple[int, int, int, int][source]¶
Convert a YOLO bounding box format to (xmin, ymin, xmax, ymax).
- Parameters:
- Returns:
The converted bounding box in (xmin, ymin, xmax, ymax) format.
- Return type: