Crop 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')
bboxA
— Bounding boxesBounding boxes, specified as an M-by-4, M-by-5, or M-by-9 nonsparse numeric matrix. M is the number of bounding boxes. Each row of the matrix defines a bounding box as either an axis-aligned rectangle, a rotate rectangle, or a cuboid. This table describes the format for each bounding box.
Bounding Box | Description |
---|---|
Axis-aligned rectangle |
Defined in pixel 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. |
window
— Crop windowimages.spatialref.Cuboid
| numeric | imageCrop window, specified as a four-element vector or an images.spatialref.Cuboid
object.
When you specify bboxA
as a rectangular input, the cropping
window must be a four-element vector in the format
[x,y,width,height].
When you specify bboxA
as a cuboid, the cropping window must be
an images.spatialref.Cuboid
object.
threshold
— Overlap threshold1
Overlap threshold, specified 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.
bboxB
— Warped bounding boxesWarped 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
— IndicesIndices, returned as a vector of integers. The indices indicate which bounding boxes
in the input, bboxA
, are within the cropping window.
bboxresize
| bboxwarp
| imcrop
You have a modified version of this example. Do you want to open this example with your edits?