bboxwarp

Apply geometric transformation to bounding boxes

Description

example

bboxB = bboxwarp(bboxA,tform,ref) transforms bounding boxes in bboxA according to the geometric transformation defined by tform. Bounding boxes can be axis-aligned rectangles, rotated rectangles, or cuboids. The spatial reference object, ref, defines the output view into which the boxes are transformed.

[bboxB,indices] = bboxwarp(bboxA,tform,ref) additionally returns a vector of indices that indicate which bounding boxes in bboxA correspond to the warped versions in the output, bboxB.

[___] = bboxwarp(___,'OverlapThreshold',threshold) additionally sets an overlap threshold.

Examples

collapse all

Read an image.

I = imread('peppers.png');

Define bounding boxes and labels.

bboxA = [
    410 230 100 90
    186 78  80  60
    ]
bboxA = 2×4

   410   230   100    90
   186    78    80    60

labelsA = [
    "garlic"
    "onion"
    ];

Define a transform to horizontally flip and translate the image.

tform = affine2d([-1 0 0; 0 1 0; 50 50 1]);

Create an output view for imwarp.

rout = affineOutputView(size(I),tform);

Warp the image.

J = imwarp(I,tform,'OutputView',rout);

Warp the boxes.

[bboxB,indices] = bboxwarp(bboxA,tform,rout);
labelsB = labelsA(indices);

Display the results.

annotatedI = insertObjectAnnotation(I,'Rectangle',bboxA,labelsA);
annotatedJ = insertObjectAnnotation(J,'Rectangle',bboxB,labelsB);
figure
montage({annotatedI, annotatedJ})

Input Arguments

collapse all

Bounding boxes, specified as an M-by-4, M-by-5, or M-by-9 nonsparse numeric matrix of M bounding boxes. Each row, M, of the matrix defines a bounding box as either an axis-aligned rectangle, a rotate rectangle, or a cuboid. The table below describes the format of the bounding boxes.

Bounding BoxRowDescription
Axis-aligned rectangle[xmin, ymin, width, height]This type of bounding box is defined in pixel coordinates as an M-by-4 matrix representing M bounding boxes
Rotated rectangle[xcenter, ycenter, width, height, yaw]This type of bounding box is defined in spatial coordinates as an M-by-5 matrix representing M bounding boxes. The xcenter and ycenter coordinates represent the center of the bounding box. The width and height elements represent the length of the box along the x and y axes, respectively. The yaw represents the rotation angle in degrees. The amount of rotation about the center of the bounding box is measured in the clockwise direction.
Cuboid[xcenter, ycenter, zcenter, width, height, depth, rx, ry, rz]Defined in spatial coordinates as an M-by-9 matrix representing M bounding boxes. The xcenter, ycenter, and ycenter coordinates represent the center of the bounding box. The width and height elements represent the length of the box along the x and y axes, respectively. The rx,ry, and rz rotation angles are in degrees about the cuboid center. The rotation is positive in the clockwise direction, with respect to the positive direction of the axis.

Rotation matrices are computed assuming ZYX order Euler angles [rx, ry, rz].

Geometric transformation, specified in an affine2d object. bboxwarp function supports only scale, rotation, and translation affine transformations.

Spatial reference, specified as an imref2d object returned by the imwarp or affineOutputView function. The object defines the output view to transform boxes. Boxes that are transformed completely outside of the output view defined by ref are discarded.

[J,rout] = imwarp(I,tform);
[bboxB,indices] = bboxwarp(bboxA,tform,rout);
rout = affineOutputView(size(I),tform)
J = imwarp(I,tform,'OutputView',rout);
[bboxB,indices] = bboxwarp(bboxA,tform,rout);

Overlap threshold, specified as a positive scalar less than or equal to 1. The amount of overlap between transformed boxes and the area defined by the output view is defined as:

area(bbox intersect bounding rectangle) / area(bbox)

  • bounding rectangle — Defined by the input spatial reference object, ref.

  • bbox — Result of transforming boxes in bboxA.

If the computed overlap value is greater than the value of the threshold property, then the transformed boxes are clipped to the bounding rectangle border. Otherwise, the boxes are discarded. Lowering the threshold can result in parts of the object getting discarded.

Output Arguments

collapse all

Warped bounding boxes, returned as an M2-by-N matrix of M2 bounding boxes. The number of bounding boxes returned is less than the number of bounding boxes in the input. Each row, M2, of the matrix defines one bounding box of the same type as the input bboxA.

Indices, returned as a vector of integers. The indices indicate which bounding boxes in the input, bboxA, that correspond to the warped versions in the output, bboxB.

Introduced in R2019b