Detect objects using Faster R-CNN object detector configured for monocular camera
detects objects within image
bboxes = detect(
I using a Faster R-CNN (regions with
convolutional neural networks) object detector configured for a monocular camera. The
locations of objects detected are returned as a set of bounding boxes.
When using this function, use of a CUDA®-enabled NVIDIA® GPU is highly recommended. The GPU reduces computation time significantly. Usage of the GPU requires Parallel Computing Toolbox™. For information about the supported compute capabilities, see GPU Support by Release (Parallel Computing Toolbox).
[___] = detect(___,
detects objects within the rectangular search region specified by
[___] = detect(___,
specifies options using one or more
Name,Value pair arguments. For
detect(detector,I,'NumStongestRegions',1000) limits the number
of strongest region proposals to 1000.
Configure a Faster R-CNN object detector for use with a monocular camera mounted on an ego vehicle. Use this detector to detect vehicles within an image captured by the camera.
fasterRCNNObjectDetector object pretrained to detect vehicles.
detector = vehicleDetectorFasterRCNN;
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); monCam = monoCamera(intrinsics,height,'Pitch',pitch);
Configure the detector for use with the camera. Limit the width of detected objects to a typical range for vehicle widths: 1.5–2.5 meters. The configured detector is a
vehicleWidth = [1.5 2.5]; detectorMonoCam = configureDetectorMonoCamera(detector,monCam,vehicleWidth);
Read in an image captured by the camera.
I = imread('carsinfront.png'); imshow(I)
Detect the vehicles in the image by using the detector. Annotate the image with the bounding boxes for the detections and the detection confidence scores.
[bboxes,scores] = detect(detectorMonoCam,I); I = insertObjectAnnotation(I,'rectangle',bboxes,scores,'Color','g'); imshow(I)
detector— Faster R-CNN object detector configured for monocular camera
I— Input image
Input image, specified as an H-by-W-by-C-by-B numeric array of images Images must be real, nonsparse, grayscale or RGB image.
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 to
B: The 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
rescale function. The size of this input image should 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. Consider whether you used the
property during training to modify the size of training images.
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
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.
comma-separated pairs of
the argument name and
Value is the corresponding value.
Name must appear inside quotes. You can specify several name and value
pair arguments in any order as
NumStrongestRegions— Maximum number of strongest region proposals
2000(default) | positive integer |
Maximum number of strongest region proposals, specified as the comma-separated pair consisting
'NumStrongestRegions' and a
positive integer. Reduce this value to speed up
processing time at the cost of detection accuracy.
To use all region proposals, specify this value as
SelectStrongest— Select strongest bounding box
Select the strongest bounding box for each detected object, specified as the comma-separated
pair consisting of
'SelectStrongest' and either
true — Return the
strongest bounding box per object. To select these
detect calls the
function, which uses nonmaximal suppression to
eliminate overlapping bounding boxes based on
their confidence scores.
selectStrongestBboxMulticlass(bbox,scores, ... 'RatioType','Min', ... 'OverlapThreshold',0.5);
false — Return all
detected bounding boxes. You can then create your
own custom operation to eliminate overlapping
MinSize— Minimum region size
Minimum region size that contains a detected object, specified as the comma-separated pair consisting of
'MinSize' and a [height width] vector. Units are in pixels.
MinSize is the smallest object that the trained
detector can detect.
MaxSize— Maximum region size
I) (default) | [height width] vector
Maximum region size that contains a detected object, specified as the comma-separated pair consisting of
'MaxSize' and a [height width] vector. Units are in pixels.
To reduce computation time, set this value to the known maximum region size for the objects being detected in the image. By default,
'MaxSize' is set to the height and width of the input 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 images. 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
ExecutionEnvironment— Hardware resource
Hardware resource on which to run the detector, specified as the comma-separated pair
'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 Support by Release (Parallel Computing Toolbox).
'cpu' — Use the
bboxes— Location of objects detected
Location of objects detected within the input image or images, returned as an M-by-4 matrix or a B-by-1 cell array. M is the number of bounding boxes in an image, and B is the number of M-by-4 matrices when the input contains an array of images.
Each row of
bboxes contains a four-element vector of the
height]. This vector specifies the upper left corner and size
of that corresponding bounding box in pixels.
scores— Detection scores
Detection confidence scores, returned as an M-by-1 vector or a B-by-1 cell array. M is the number of bounding boxes in an image, and B is the number of M-by-1 vectors when the input contains an array of images. A higher score indicates higher confidence in the detection.
labels— Labels for bounding boxes
Labels for bounding boxes, returned as an M-by-1 categorical array or a
B-by-1 cell array. M is the number of
labels in an image, and B is the number of
M-by-1 categorical arrays when the input contains an
array of images. You define the class names used to label the objects when you
train the input
detectionResults— Detection results
Detection results, returned as a 3-column table with 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.