bboxcrop
Crop bounding boxes
Syntax
Description
[___] = bboxcrop(___,
specifies options using one or more namevalue 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
Center Crop Image 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" ];
Create a center cropping window.
targetSize = [256 256]; win = centerCropWindow2d(size(I),targetSize);
Center crop the image.
[r,c] = deal(win.YLimits(1):win.YLimits(2),win.XLimits(1):win.XLimits(2)); J = I(r,c,:);
Center crop boxes and labels. Boxes outside the cropping window are removed.
[bboxB,indices] = bboxcrop(bboxA,win); labelsB = labelsA(indices);
Display the results.
figure I = insertObjectAnnotation(I,'Rectangle',bboxA,labelsA); J = insertObjectAnnotation(J,'Rectangle',bboxB,labelsB); imshowpair(I,J,'montage')
Input Arguments
bboxA
— Bounding boxes
Mby4 matrix  Mby5 matrix  Mby9 matrix
Bounding boxes, specified as an Mby4, Mby5, or Mby9 nonsparse numeric matrix. M is the number of bounding boxes. Each row of the matrix defines a bounding box as either an axisaligned rectangle, a rotated rectangle, or a cuboid. This table describes the format for each bounding box.
Bounding Box  Description 

Axisaligned rectangle 
Defined in spatial coordinates as an Mby4 numeric matrix with rows of the form [x y w h], where:

Rotated rectangle 
Defined in spatial coordinates as an Mby5 numeric matrix with rows of the form [xctr yctr xlen ylen yaw], where:

Cuboid 
Defined in spatial coordinates as an Mby9 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. 
window
— Cropping window
fourelement vector (default)  images.spatialref.Rectangle
 sixelement vector  images.spatialref.Cuboid
 numeric  image
Cropping window, specified as a fourelement vector, a sixelement vector, or a
Cuboid
object. The
output bounding box positions are relative to the location of the cropping
window.
When you specify bboxA
as a rectangular input, the cropping
window must be a fourelement vector in the format
[x,y,width,height],
or a Rectangle
object.
When you specify bboxA
as a cuboid, the cropping window must be
a sixelement vector in the format
[x,y,zwidth,height,depth],
or a Cuboid
object.
NameValue Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Namevalue 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 the input boxes,
bboxA
, and the cropping area, window
, is
defined as:
area
(intersect
(bboxA
,window
))/area
(union
(bboxB
,window
)).
If the computed overlap value is greater than the value of the
threshold
property, then the 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
— Cropped bounding boxes
M2byN matrix  nonsparse numeric
Cropped bounding boxes, returned as an M2byN
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
. The output bounding box positions are relative to the
location of the cropping window.
indices
— Indices
vector of integers
Indices, returned as a vector of integers. The indices indicate which bounding boxes
in the input, bboxA
, are within the cropping window.
Version History
Introduced in R2019bR2022a: Bounding Box Coordinates: Data augmentation for object detection using spatial coordinates
The bboxresize
,
bboxcrop
,
bboxwarp
, and
showShape
functions assume the input bounding box coordinates for axisaligned rectangles are
specified in spatial coordinates and return the transformed bounding boxes in spatial
coordinates.
See Also
imcrop
 bboxresize
 bboxwarp
 bboxerase
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
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)