selectStrongestBboxMulticlass
Select strongest multiclass bounding boxes from overlapping clusters
Syntax
Description
returns selected bounding boxes that have high confidence scores. The function uses
greedy nonmaximal suppression (NMS) to eliminate overlapping bounding boxes from the
selectedBboxes
= selectStrongestBboxMulticlass(bboxes
,scores
,labels
)bboxes
input, only if they have the same class
label.
[
additionally returns the scores, labels, and index associated with the selected
bounding boxes.selectedBboxes
,selectedScores
,selectedLabels
,index
]
= selectStrongestBboxMulticlass(bboxes
,scores
,labels
)
[___]
= selectStrongestBboxMulticlass(___,
uses additional options specified by one or more Name,Value
)Name,Value
pair arguments.
Examples
Run Multiclass Nonmaximal Suppression on Bounding Boxes Using People Detector
Create detectors using two different models. These will be used to generate multiclass detection results.
detectorInria = peopleDetectorACF('inria100x41'); detectorCaltech = peopleDetectorACF('caltech50x21');
Apply the detectors.
I = imread('visionteam1.jpg'); [bboxesInria,scoresInria] = detect(detectorInria,I,'SelectStrongest',false); [bboxesCaltech,scoresCaltech] = detect(detectorCaltech,I,'SelectStrongest',false);
Create categorical labels for each the result of each detector.
labelsInria = repelem("inria",numel(scoresInria),1); labelsInria = categorical(labelsInria,{'inria','caltech'}); labelsCaltech = repelem("caltech",numel(scoresCaltech),1); labelsCaltech = categorical(labelsCaltech,{'inria','caltech'});
Combine results from all detectors to for multiclass detection results.
allBBoxes = [bboxesInria;bboxesCaltech]; allScores = [scoresInria;scoresCaltech]; allLabels = [labelsInria;labelsCaltech];
Run multiclass nonmaximal suppression.
[bboxes,scores,labels] = selectStrongestBboxMulticlass(allBBoxes,allScores,allLabels,... 'RatioType','Min','OverlapThreshold',0.65);
Annotate detected people.
annotations = string(labels) + ": " + string(scores); I = insertObjectAnnotation(I,'rectangle',bboxes,cellstr(annotations)); imshow(I) title('Detected People, Scores, and Labels')
Input Arguments
bboxes
— Bounding boxes
Mby4 matrix  Mby5 matrix
Bounding boxes, specified as an Mby4 or Mby5 nonsparse numeric matrix. M is the number of bounding boxes. Each row of the matrix defines a bounding box as either an axisaligned rectangle or a rotated rectangle. 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:

Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
scores
— Confidence scores
Mby1 vector
Confidence scores corresponding to the input bounding boxes, specified as
an Mby1 vector. The
selectStrongestBboxMulticlass
function uses greedy
NMS to eliminate overlapping bounding boxes and associate the confidence
score with the boxes. A higher score represents a higher confidence in
keeping the bounding box. The scores
input must be
real, finite, and nonsparse.
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
labels
— Labels
Mby1 categorical vector  Mby1 numeric vector
Labels corresponding to the input bounding boxes, specified as an Mby1 categorical or numeric vector.
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
 categorical
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: 'RatioType'
,'Union'
sets the
'RatioType'
property to
'Union'
.
RatioType
— Bounding box overlap ratio denominator
'Union'
(default)  'Min'
Ratio type, specified as the character vector
'Union'
or 'Min'
.
Set the ratio type to
'Union'
to compute the ratio as the area of intersection betweenbboxA
andbboxB
, divided by the area of the union of the two.Set the ratio type to
'Min'
to compute the ratio as the area of intersection betweenbboxA
andbboxB
, divided by the minimum area of the two bounding boxes.
Data Types: char
OverlapThreshold
— Overlap ratio threshold
0.5
(default)  scalar in the range [0 1]
Overlap ratio threshold, specified as the commaseparated pair
consisting of 'OverlapThreshold
' and a scalar in
the range [0 1]. When the overlap ratio is above the threshold, the
function removes bounding boxes around the reference box. Decrease the
threshold to reduce the number of selected bounding boxes. However, if
you decrease the threshold too much, you might eliminate boxes that
represent objects close to each other in the image.
Data Types: single
 double
NumStrongest
— Maximum number of strongest boxes
inf
(default)  positive scalar
Maximum number of strongest boxes, specified as the commaseparated
pair consisting of 'NumStrongest'
and
inf
or a positive scalar. Use this argument to
reduce processing time when you have a priori knowledge about the
maximum number of boxes. Set the value to inf
to
select all the strongest, nonoverlapping, bounding boxes.
When the labels
input contains categorical
labels, you can also specify a vector that contains the maximum number
of strongest boxes for each category in the labels input. The length of
the specified vector must equal the number of categories in the
label.
Output Arguments
selectedBboxes
— Selected bounding boxes
Mby4 matrix  Mby5 matrix
Selected bounding boxes, returned as an Mby4 or an Mby5 matrix. The 4element vectors represent axisaligned rectangles and the 5element vectors represent rotated rectangles.
The selectedBbox
output returns the selected bounding
boxes from the bbox
input that have the highest
confidence score. The function uses nonmaximal suppression to eliminate
overlapping bounding boxes.
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
selectedScores
— Scores of selected bounding boxes
Mby1 vector
Scores of selected bounding boxes, returned as an
Mby1 vector. The Mth score in the
selectedScores
output corresponds to the
Mth bounding box in the
selectedBboxes
output. The data type of
selectedScores
matches the data type of
scores
.
selectedLabels
— Labels of selected bounding boxes
Mby1 categorical vector  Mby1 numeric vector
Labels of selected bounding boxes, returned as an
Mby1 categorical or numeric vector. The
Mth label in the selectedLabels
output corresponds to the Mth bounding box in the
selectedBboxes
output. The data type of
selectedLabels
matches the data type of
labels
.
index
— Index of selected bounding boxes
Mby1 vector
Index of selected bounding boxes, returned as an Mby1
vector. The index
vector contains the indices to the
selected boxes in the bboxes
input.
Data Types: double
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
GPU Code generation is not supported for rotated rectangle bounding box inputs.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
GPU Arrays is not supported for rotated rectangle bounding box inputs.
Version History
See Also
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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)