Bounding Boxes

Overview

The Bounding Boxes sample demonstrates GPU-accelerated axis-aligned bounding-box rendering using CV-CUDA’s bndbox operator. Three colored rectangles are drawn over a cat image, each with an independent border color and thickness.

Usage

Basic Usage

Draw boxes on the default cat image:

python3 bndbox.py

Custom Input

Specify your own image:

python3 bndbox.py -i input.jpg -o cat_bndbox.jpg

Command-Line Arguments

Argument

Short Form

Default

Description

--input

-i

tabby_tiger_cat.jpg

Input image file path

--output

-o

cvcuda/.cache/cat_bndbox.jpg

Output image file path

Implementation

Bounding Box Rendering

# bndbox requires a batched NHWC tensor, so wrap the single HWC image in a batch.
# cvcuda.stack promotes [HWC, ...] -> NHWC without copying pixel data.
nhwc_image: cvcuda.Tensor = cvcuda.stack([input_image])

# Describe three axis-aligned boxes to highlight features on the cat image:
#   Red box    – face region (top-centre)
#   Green box  – body torso
#   Blue box   – tail / lower body
# Each BndBoxI takes (x, y, width, height), border thickness, border colour
# (RGB), and fill colour (RGBA).  A fill alpha of 0 leaves the interior
# pixels untouched, so only the border is drawn.
bboxes = cvcuda.BndBoxesI(
    boxes=[
        [
            cvcuda.BndBoxI(
                box=(260, 60, 200, 190),
                thickness=4,
                borderColor=(255, 80, 0),
                fillColor=(255, 80, 0, 0),
            ),
            cvcuda.BndBoxI(
                box=(180, 280, 360, 260),
                thickness=4,
                borderColor=(0, 220, 60),
                fillColor=(0, 220, 60, 0),
            ),
            cvcuda.BndBoxI(
                box=(420, 500, 220, 180),
                thickness=4,
                borderColor=(30, 120, 255),
                fillColor=(30, 120, 255, 0),
            ),
        ],
    ]
)

Key points:

  1. Tensor layout: cvcuda.bndbox() supports NHWC/HWC and NCHW/CHW tensors. The sample uses NHWC: use cvcuda.stack to add the batch dimension to a single HWC image.

  2. BndBoxesI structure: One list of BndBoxI objects per batch image; each box specifies (x, y, width, height) in pixel coordinates.

  3. Fill alpha 0: Setting the RGBA fill alpha to 0 draws only the border, leaving interior pixels unchanged.

  4. In-place semantics: The operator returns a new tensor but operates on a copy; the source tensor is not modified.

  5. Layout restoration: Reshape the NHWC output back to HWC before passing to write_image.

Expected Output

The output shows the original cat image with three colored bounding boxes drawn on it:

../../_images/tabby_tiger_cat.jpg

Original Input Image

../../_images/cat_bndbox.jpg

Output: Three colored bounding boxes drawn on the cat

CV-CUDA Operators Used

Operator

Purpose

cvcuda.bndbox()

Draw axis-aligned bounding boxes with configurable border color and thickness

Common Utilities Used

See Also