detect
Detect objects using YOLO v3 object detector configured for monocular camera
Since R2023a
Syntax
Description
detects objects within image bboxes
= detect(detector
,I
)I
using a you only look once version 3
(YOLO v3) object detector configured for a monocular camera. The function returns the
locations of detected objects as a set of bounding boxes.
Using this function with CUDA®-enabled NVIDIA® GPU is highly recommended. A GPU reduces computation time significantly. Using a GPU requires Parallel Computing Toolbox™. For information about the supported compute capabilities, see GPU Computing Requirements (Parallel Computing Toolbox).
[___] = detect(___,
detects objects within the rectangular search region specified by roi
)roi
using input and output arguments from any of the previous syntaxes.
detects objects within the series of images returned by the detectionResults
= detect(detector
,ds
)read
function
of the input datastore.
[___] = detect(___,
also specifies options using one or more name-value arguments.Name=Value
)
Examples
Detect Objects Using Monocular Camera and YOLO v3
Configure a YOLO v3 object detector for use with a monocular camera mounted on an ego vehicle. Use this detector to detect vehicles and stop signs within an image captured by the camera.
Load a yolov3ObjectDetector
object pretrained on the COCO data set.
detector = yolov3ObjectDetector;
Model a monocular camera sensor by creating a monoCamera
object. This object contains the camera intrinsics and the location of the camera on the ego vehicle.
focalLength = [309.4362 344.2161]; % [fx fy] principalPoint = [318.9034 257.5352]; % [cx cy] imageSize = [480 640]; % [mrows ncols] height = 2.1798; % height of camera above ground, in meters pitch = 14; % pitch of camera, in degrees intrinsics = cameraIntrinsics(focalLength,principalPoint,imageSize); sensor = monoCamera(intrinsics,height,Pitch=pitch);
Configure the detector for use with the camera. Limit the width of detected objects to 1.5–2.5 meters. The configured detector is a yolov3ObjectDetectorMonoCamera
object.
vehicleWidth = [1.5 2.5]; detectorMonoCam = configureDetectorMonoCamera(detector,sensor,vehicleWidth);
Read an image captured by the camera.
I = imread("object-detection-test.png");
Detect the vehicles and stop signs in the image by using the detector. Annotate the image with the bounding boxes for the detections and the class labels.
[bboxes,scores,labels] = detect(detectorMonoCam,I); I = insertObjectAnnotation(I,"rectangle",bboxes,labels,AnnotationColor="green"); imshow(I)
Input Arguments
detector
— YOLO v3 object detector configured for monocular camera
yolov3ObjectDetectorMonoCamera
object
YOLO v3 object detector configured for a monocular camera, specified as a yolov3ObjectDetectorMonoCamera
object. To create this object, use the
configureDetectorMonoCamera
function
with a monoCamera
object and yolov3ObjectDetector
object as inputs.
I
— Input image
H-by-W-by-C-by-B
numeric array of images
Input image, specified as an H-by-W-by-C-by-B numeric array of images. Images must be real, nonsparse, grayscale or RGB images.
H — Height in pixels.
W — Width in pixels.
C — The channel size in each image must be equal to the network input channel size. For example, for grayscale images, C must be equal to
1
. For RGB color images, it must be equal to3
.B — Number of images in the array.
The detector is sensitive to the range of the input image. Therefore, ensure that
the input image range is similar to the range of the images used to train the detector.
For example, if the detector was trained on uint8
images, rescale
this input image to the range [0, 255] by using the im2uint8
or rescale
function. The size of this input 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 input image differs from the
scale of the objects the detector was trained to identify.
Data Types: uint8
| uint16
| int16
| double
| single
| logical
ds
— Datastore
datastore
object
Datastore, specified as a datastore
object containing a collection of
images. Each image must be a grayscale, RGB, or multichannel image. The function
processes only the first column of the datastore, which must contain images and must be
cell arrays or tables with multiple columns.
roi
— Search region of interest
[x
y
width
height] vector
Search region of interest, specified as a four-element vector of the form [x y width height]. 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 scalar in the range [0, 1]. The function removes detections that have scores less than this threshold value. To reduce false positives, increase this value.
SelectStrongest
— Option to select strongest bounding box
true
(default) | false
Option to select the strongest bounding box for each detected object, specified as
true
or false
.
true
— Return the strongest bounding box per object using theselectStrongestBboxMulticlass
function, which uses nonmaximal suppression to eliminate overlapping bounding boxes based on their confidence scores.By default, the
selectStrongestBboxMulticlass
function is called as follows:selectStrongestBboxMulticlass(bbox,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) | two-element vector
Minimum region size, specified as 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.
MaxSize
— Maximum region size
size
(I
) (default) | two-element vector
Maximum region size, specified as 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 a scalar. Specify the
MiniBatchSize
argument to process a large collection of images.
Images are grouped into mini-batches and processed as a batch to improve computation
efficiency. Use a large mini-batch size to decrease processing time. Use a small
mini-batch size to use less memory.
ExecutionEnvironment
— Hardware resource
"auto"
(default) | "gpu"
| "cpu"
Hardware resource on which to run the detector, specified as one of these values:
"auto"
— Use a GPU if one 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 one of these values:
"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 you use 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 "auto"
and "mex"
options 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 available only 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).
The "mex"
acceleration does not support all layers. For a list
of supported layers, see Supported Layers (GPU Coder).
Output Arguments
bboxes
— Locations of detected objects
M-by-4 matrix
Locations of detected objects within the input image or images, returned as an M-by-4 matrix. M is the number of bounding boxes in an image.
Each row of bboxes
contains a four-element vector of the form
[x
y
width
height]. This vector specifies the upper left corner and size of that
corresponding bounding box in pixels.
scores
— Detection scores
M-by-1 vector
Detection confidence scores, returned as an M-by-1 vector. M is the number of bounding boxes in an image. The detection score values lie between 0 and 1. A higher score indicates greater confidence in the detection.
labels
— Labels for bounding boxes
M-by-1 categorical array
Labels for bounding boxes, returned as an M-by-1 categorical
array. M is the number of labels in an image. You define the class
names used to label the objects when you train the input
detector
.
detectionResults
— Detection results
3-column table
Detection results, returned as a 3-column table with the variable names Boxes, Scores, and Labels. The Boxes column contains M-by-4 matrices of M bounding boxes for the objects found in the image. Each row contains a bounding box as a 4-element vector in the format [x,y,width,height]. The format specifies the upper left corner location and size in pixels of the bounding box in the corresponding image.
Version History
Introduced in R2023a
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 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)