bboxwarp
Apply geometric transformation to bounding boxes
Syntax
Description
bboxB = bboxwarp(
transforms bounding boxes in bboxA
,tform
,ref
)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.
This function supports 2-D and 3-D bounding boxes.
[___] = bboxcrop(___,
specifies options using one or more name-value arguments in addition to any combination of
arguments from previous syntaxes. For example, Name=Value
)OverlapThreshold=1
, sets
the positive overlap threshold to 1
.
Examples
Transform Images and Corresponding Bounding Boxes
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
bboxA
— Bounding boxes
M-by-4 matrix | M-by-5 matrix | M-by-9 matrix | nonsparse numeric
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 Box | Description |
---|---|
Axis-aligned rectangle |
Defined in spatial coordinates as an M-by-4 numeric matrix with rows of the form [x y w h], where:
|
Rotated rectangle |
Defined in spatial coordinates as an M-by-5 numeric matrix with rows of the form [xctr yctr xlen ylen yaw], where:
|
Cuboid |
Defined in spatial coordinates as an M-by-9 numeric matrix with rows of the form [xctr yctr zctr xlen ylen zlen xrot yrot zrot], where:
The figure shows how these values determine the position of a cuboid. |
tform
— Geometric transformation
affine2d
object | affine3d
object
ref
— Spatial reference
imref2d
object | imref3d
object
Spatial reference, specified as an imref2d
or imref3d
object. To obtain one of these
objects, you can use the imwarp
or the
affineOutputView
function. For cuboid inputs,
ref
must be a imref3d
object. 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);
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: OverlapThreshold=1
, sets the positive overlap threshold to
1
.
OverlapThreshold
— Overlap threshold
1 (default) | positive scalar less than or equal to 1
Overlap threshold, specified as a positive scalar less than or equal to
1
. The amount of overlap between transformed boxes and the region
W, defined by the output view, is defined as:
area
(intersect
(bboxA
,W))/area
(bboxB
,W).
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
bboxB
— Warped bounding boxes
M2-by-N matrix | nonsparse numeric
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
. When bboxB
contains floating point
data, the function returns it with the same type as bboxA
.
Otherwise, the function returns bboxB
as type
single
.
Version History
Introduced in R2019bR2022a: Bounding Box Coordinates: Data augmentation for object detection using spatial coordinates
Behavior changed in R2022a
The bboxresize
,
bboxcrop
,
bboxwarp
, and
showShape
functions assume the input bounding box coordinates for axis-aligned rectangles are
specified in spatial coordinates and return the transformed bounding boxes in spatial
coordinates.
See Also
imcrop
| bboxcrop
| bboxresize
| imwarp
| bboxerase
Ouvrir l'exemple
Vous possédez une version modifiée de cet exemple. Souhaitez-vous ouvrir cet exemple avec vos modifications ?
Commande MATLAB
Vous avez cliqué sur un lien qui correspond à cette commande MATLAB :
Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)