detect
Syntax
Description
detects objects within a single image or an array of images, bboxes
= detect(detector
,I
)I
, using
a you only look once version 3 (YOLO v3) object detector, detector
.
The input size of the image must be greater than or equal to the network input size of the
pretrained detector. The locations of objects detected are returned as a set of bounding
boxes.
Note
To use the pretrained YOLO v3 object detection networks trained on COCO dataset, you must install the Computer Vision Toolbox™ Model for YOLO v3 Object Detection. You can download and install the Computer Vision Toolbox Model for YOLO v3 Object Detection from Add-On Explorer. For more information about installing add-ons, see Get and Manage Add-Ons. To run this function, you will require the Deep Learning Toolbox™.
detects objects within all the images returned by the detectionResults
= detect(detector
,ds
)read
function of the input
datastore ds
.
[___] = detect(___,
detects objects within the rectangular search region roi
)roi
, in addition
to any combination of arguments from previous syntaxes.
[___] = detect(___,
specifies options using one or more name-value arguments, in addition to any combination
of arguments from previous syntaxes.Name=Value
)
Examples
Detect Objects Using YOLO v3 Object Detector
Load a pretrained YOLO v3 object detector.
detector = yolov3ObjectDetector('tiny-yolov3-coco');
Read a test image and preprocess the test image by using the preprocess
function.
img = imread('sherlock.jpg');
img = preprocess(detector,img);
Detect objects in the test image.
[bboxes,scores,labels] = detect(detector,img);
Display the detection results.
results = table(bboxes,labels,scores)
results=1×3 table
bboxes labels scores
________________________ ______ _______
133 67 283 278 dog 0.51771
detectedImg = insertObjectAnnotation(img,'Rectangle',bboxes,labels);
figure
imshow(detectedImg)
Detect Objects from Images Stored in Image Datastore
Load a pretrained YOLOv3 object detector.
detector = yolov3ObjectDetector('tiny-yolov3-coco');
Read the test data and store as an image datastore object.
location = fullfile(matlabroot,'toolbox','vision','visiondata','vehicles'); imds = imageDatastore(location);
Detect objects in the test dataset. Set the Threshold
parameter value to 0.3 and MiniBatchSize
parameter value to 32.
detectionResults = detect(detector,imds,'Threshold',0.3,'MiniBatchSize',32);
Read an image from the test dataset and extract the corresponding detection results.
num = 10; I = readimage(imds,num); bboxes = detectionResults.Boxes{num}; labels = detectionResults.Labels{num}; scores = detectionResults.Scores{num};
Perform non-maximal suppression to select strongest bounding boxes from the overlapping clusters. Set the OverlapThreshold
parameter value to 0.2.
[bboxes,scores,labels] = selectStrongestBboxMulticlass(bboxes,scores,labels,'OverlapThreshold',0.2);
Display the detection results.
results = table(bboxes,labels,scores)
results=3×3 table
bboxes labels scores
________________________ ______ _______
14 71 52 27 car 0.93352
74 73 7 5 car 0.65369
102 73 15 10 car 0.85313
detectedImg = insertObjectAnnotation(I,'Rectangle',bboxes,labels);
figure
imshow(detectedImg)
Detect Objects Within ROI
Load a pretrained YOLO v3 object detector.
detector = yolov3ObjectDetector('tiny-yolov3-coco');
Read a test image.
img = imread('highway.png');
Specify a region of interest (ROI) within the test image.
roiBox = [70 40 100 100];
Detect objects within the specified ROI.
[bboxes,scores,labels] = detect(detector,img,roiBox);
Display the ROI and the detection results.
img = insertObjectAnnotation(img,'Rectangle',roiBox,'ROI','AnnotationColor',"blue"); detectedImg = insertObjectAnnotation(img,'Rectangle',bboxes,labels); figure imshow(detectedImg)
Input Arguments
detector
— YOLO v3 object detector
yolov3ObjectDetector
object
YOLO v3 object detector, specified as a yolov3ObjectDetector
object.
I
— Test images
numeric array
Test images, specified as a numeric array of size H-by-W-byC or H-by-W-byC-by-T. Images must be real, nonsparse, grayscale or RGB image.
H: Height
W: Width
C: The channel size in each image must be equal to the network's input channel size. For example, for grayscale images, C must be equal to
1
. For RGB color images, it must be equal to3
.T: Number of test images in the array. The function computes the object detection results for each test image in the array.
The intensity range of the test image must be similar to the intensity range of the
images used to train the detector. For example, if you train the detector on
uint8
images, rescale the test image to the range [0, 255] by using
the im2uint8
or rescale
function. The size of the test image must be comparable to the sizes of the images used
in training. If these sizes are very different, the detector has difficulty detecting
objects because the scale of the objects in the test image differs from the scale of the
objects the detector was trained to identify.
Data Types: uint8
| uint16
| int16
| double
| single
ds
— Test images
ImageDatastore
object | CombinedDatastore
object | TransformedDatastore
object
Test images, specified as a ImageDatastore
object,
CombinedDatastore
object, or
TransformedDatastore
object containing full filenames of the test
images. The images in the datastore must be grayscale, or RGB images.
roi
— Search region of interest
[x
y
width
height] vector
Search region of interest, specified as an [x y width height] vector. The vector specifies the upper left corner and size of a region in pixels.
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.
Example: detect(detector,I,Threshold=0.25)
Threshold
— Detection threshold
0.5
(default) | scalar in the range [0, 1]
Detection threshold, specified as a comma-separated pair consisting of
'Threshold'
and a scalar in the range [0, 1]. Detections that
have scores less than this threshold value are removed. To reduce false positives,
increase this value.
SelectStrongest
— Select strongest bounding box
true
(default) | false
Select the strongest bounding box for each detected object, specified as the
comma-separated pair consisting of 'SelectStrongest'
and either
true
or false
.
true
— Returns the strongest bounding box per object. The method calls theselectStrongestBboxMulticlass
function, which uses nonmaximal suppression to eliminate overlapping bounding boxes based on their confidence scores.By default, the
selectStrongestBboxMulticlass
function is called as followsselectStrongestBboxMulticlass(bboxes,scores,... 'RatioType','Union',... 'OverlapThreshold',0.5);
false
— Return all the detected bounding boxes. You can then write your own custom method to eliminate overlapping bounding boxes.
MinSize
— Minimum region size
[1 1]
(default) | vector of the form [height
width]
Minimum region size, specified as the comma-separated pair consisting of
'MinSize'
and a vector of the form [height
width]. Units are in pixels. The minimum region size defines the
size of the smallest region containing the object.
By default, MinSize
is 1-by-1.
MaxSize
— Maximum region size
size
(I
) (default) | vector of the form [height
width]
Maximum region size, specified as the comma-separated pair consisting of
'MaxSize'
and a vector of the form [height
width]. Units are in pixels. The maximum region size defines the
size of the largest region containing the object.
By default, 'MaxSize'
is set to the height and width of the
input image, I
. To reduce computation time, set this value to the
known maximum region size for the objects that can be detected in the input test
image.
MiniBatchSize
— Minimum batch size
128
(default) | scalar
Minimum batch size, specified as the comma-separated pair consisting of
'MiniBatchSize'
and a scalar value. Use the
MiniBatchSize
to process a large collection of image. Images
are grouped into minibatches and processed as a batch to improve computation
efficiency. Increase the minibatch size to decrease processing time. Decrease the size
to use less memory.
ExecutionEnvironment
— Hardware resource
'auto'
(default) | 'gpu'
| 'cpu'
Hardware resource on which to run the detector, specified as the comma-separated
pair consisting of 'ExecutionEnvironment'
and
'auto'
, 'gpu'
, or 'cpu'
.
'auto'
— Use a GPU if it is available. Otherwise, use the CPU.'gpu'
— Use the GPU. To use a GPU, you must have Parallel Computing Toolbox™ and a CUDA®-enabled NVIDIA® GPU. If a suitable GPU is not available, the function returns an error. For information about the supported compute capabilities, see GPU Computing Requirements (Parallel Computing Toolbox).'cpu'
— Use the CPU.
Acceleration
— Performance optimization
'auto'
(default) | 'mex'
| 'none'
Performance optimization, specified as the comma-separated pair consisting of
'Acceleration'
and one of the following:
'auto'
— Automatically apply a number of optimizations suitable for the input network and hardware resource.'mex'
— Compile and execute a MEX function. This option is available when using a GPU only. Using a GPU requires Parallel Computing Toolbox and a CUDA enabled NVIDIA GPU. If Parallel Computing Toolbox or a suitable GPU is not available, then the function returns an error. For information about the supported compute capabilities, see GPU Computing Requirements (Parallel Computing Toolbox).'none'
— Disable all acceleration.
The default option is 'auto'
. If 'auto'
is
specified, MATLAB® applies a number of compatible optimizations. If you use the
'auto'
option, MATLAB does not ever generate a MEX function.
Using the 'Acceleration'
options 'auto'
and
'mex'
can offer performance benefits, but at the expense of an
increased initial run time. Subsequent calls with compatible parameters are faster.
Use performance optimization when you plan to call the function multiple times using
new input data.
The 'mex'
option generates and executes a MEX function based on
the network and parameters used in the function call. You can have several MEX
functions associated with a single network at one time. Clearing the network variable
also clears any MEX functions associated with that network.
The 'mex'
option is only available for input data specified as
a numeric array, cell array of numeric arrays, table, or image datastore. No other
types of datastore support the 'mex'
option.
The 'mex'
option is only available when you are using a GPU.
You must also have a C/C++ compiler installed. For setup instructions, see MEX Setup (GPU Coder).
'mex'
acceleration does not support all layers. For a list of
supported layers, see Supported Layers (GPU Coder).
DetectionPreprocessing
— Option to preprocess test images
'auto'
(default) | 'none'
Option to preprocess the test images before performing object detection, specified
as the comma-separated pair consisting of 'DetectionPreprocessing'
and one of these values:
'auto'
— To preprocess the test image before performing object detection. Thedetect
function calls thepreprocess
function that perform these operations:Rescales the intensity values of the training images to the range [0, 1].
Resizes the training images to one of the nearest network input sizes and updates the bounding box coordinate values for accurate training. The function preserves the original aspect ratio of the training data.
'none'
— To perform object detection without preprocessing the test image. If you choose this option, the datatype of the test image must be eithersingle
ordouble
.
Data Types: char
| string
Output Arguments
bboxes
— Location of objects detected
M-by-4 matrix | M-by-5 matrix | T-by-1 cell array
Location of objects detected within the input image or images, returned as a
M-by-4 matrix or an M-by-5 matrix if the input is a single test image.
T-by-1 cell array if the input is an array of test images. T is the number of test images in the array. M is the number of bounding boxes in an image
The table describes the format of bounding boxes.
Bounding Box | Description |
---|---|
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:
|
scores
— Detection scores
M-by-1 numeric vector | T-by-1 cell array
Detection confidence scores for each bounding box, returned as one of these options:
M-by-1 numeric vector — The input is a single test image. M is the number of bounding boxes detected in the image.
B-by-1 cell array — The input is a batch of test images, where B is the number of test images in the batch. Each cell in the array contains an M-element row vector, where each element indicates the detection score for a bounding box in the corresponding image.
A higher score indicates higher confidence in the detection. The confidence score for each detection is a product of the corresponding objectness score and maximum class probability. The objectness score is the probability that the object in the bounding box belongs to a class in the image. The maximum class probability is the largest probability that a detected object in the bounding box belongs to a particular class.
labels
— Labels for bounding boxes
M-by-1 categorical vector | T-by-1 cell array
Labels for bounding boxes, returned as one of these options:
M-by-1 categorical vector if the input is a single test image.
T-by-1 cell array if the input is an array of test images. T is the number of test images in the array. Each cell in the array contains a M-by-1 categorical vector containing the names of the object classes.
M is the number of bounding boxes detected in an image.
detectionResults
— Detection results
3-column table
Detection results, returned as a 3-column table with variable names, Boxes, Scores, and Labels. The Boxes column can contain rectangles or rotated rectangle bounding boxes of the form :
rectangle — M-by-4 matrices, of M bounding boxes for the objects found in the image. Each row specifies a rectangle as a 4-element vector of the form [x,y,width,height], where (x,y) specifies the upper-left corner location and (width, height) specifies the size in pixels
rotated rectangle — M-by-5 matrices of M bounding boxes for the objects found in the image. Each row specifies a rotated rectangle as a 5-element vector of the form [xctr,yctr,width, height,yaw], where (xctr,yctr) specifies the center, (width,height) specifies the size, and yaw specifies the rotated angle.
info
— Class probabilities and objectness scores
structure array
Class probabilities and objectness scores of the detections, returned as a structure array with these fields.
ClassProbabilities
— Class probabilities for each of the detections, returned as a B-by-1 cell array. B is the number of images in the input batch of images,I
. Each cell in the array contains the class probabilities as an M-by-N numeric matrix. M is the number of bounding boxes and N is the number of classes. Each class probability is a numeric scalar, indicating the probability that the detected object in the bounding box belongs to a class in the image.ObjectnessScores
— Objectness scores for each of the detections, returned as a B-by-1 cell array. B is the number of images in the input batch of images,I
. Each cell in the array contains the objectness score for each bounding box as an M-by-1 numeric vector. M is the number of bounding boxes. Each objectness score is a numeric scalar, indicating the probability that the bounding box contains an object belonging to one of the classes in the image.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
The
roi
argument to thedetect
method must be a code generation constant (coder.const()
) and a 1x4 vector.Only the
Threshold
,SelectStrongest
,MinSize
, andMaxSize
name-value pairs fordetect
are supported.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
The
roi
argument to thedetect
method must be a codegen constant (coder.const()
) and a 1x4 vector.Only the
Threshold
,SelectStrongest
,MinSize
, andMaxSize
name-value pairs are supported.The height, width, and channel of the input image must be fixed size.
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.
Version History
Introduced in R2021aR2024b: Code generation support for rotated rectangle bounding boxes
Starting in R2024b, bboxes
output argument returned as rotated
rectangle bounding box predictions, supports both CPU and GPU code generation.
R2024a: Rotated rectangle bounding box support
The output argument bboxes
now support rotated rectangle bounding box
predictions.
R2024a: Option to return class probabilities and objectness scores
Specify the info
output
argument to return information about the class probability and objectness score for each
detection.
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)