Main Content


Detect objects in monocular camera using YOLO v2 deep learning detector


The yolov2ObjectDetectorMonoCamera object contains information about you only look once version 2 (YOLO v2) object detector that is configured for use with a monocular camera sensor. To detect objects in an image captured by the camera, pass the detector to the detect object function.

When using the detect object function with a yolov2ObjectDetectorMonoCamera object, 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 Computing Requirements (Parallel Computing Toolbox).


  1. Create a yolov2ObjectDetector object by calling the trainYOLOv2ObjectDetector function with training data (requires Deep Learning Toolbox™).

    detector = trainYOLOv2ObjectDetector(trainingData,____);
  2. Create a monoCamera object to model the monocular camera sensor.

    sensor = monoCamera(____);
  3. Create a yolov2ObjectDetectorMonoCamera object by passing the detector and sensor as inputs to the configureDetectorMonoCamera function. The configured detector inherits property values from the original detector.

    configuredDetector = configureDetectorMonoCamera(detector,sensor,____);


expand all

This property is read-only.

Camera configuration, specified as a monoCamera object. The object contains the camera intrinsics, the location, the pitch, yaw, and roll placement, and the world units for the parameters. Use the intrinsics to transform the object points in the image to world coordinates, which you can then compare to the values in the WorldObjectSize property.

This property is read-only.

Range of object widths and lengths in world units, specified as a [minWidth maxWidth] vector or [minWidth maxWidth; minLength maxLength] matrix. Specifying the range of object lengths is optional.

Name of the classification model, specified as a character vector or string scalar. By default, the name is set to the heading of the second column of the trainingData table specified in the trainYOLOv2ObjectDetector function. You can modify this name after creating the yolov2ObjectDetectorMonoCamera object.

This property is read-only.

Trained YOLO v2 object detection network, specified as a DAGNetwork (Deep Learning Toolbox) object. This object stores the layers that are used within the YOLO v2 object detector.

This property is read-only.

Names of the object classes that the YOLO v2 object detector was trained to find, specified as a cell array of character vectors. This property is set by the trainingData input argument for the trainYOLOv2ObjectDetector function. Specify the class names as part of the trainingData table.

This property is read-only.

Size of anchor boxes, specified as an M-by-2 matrix, where each row is of form [height width]. This value specifies the height and width of M anchor boxes. This property is set by the AnchorBoxes property of the output layer in the YOLO v2 network.

The anchor boxes are defined when creating the YOLO v2 network by using the yolov2Layers function. Alternatively, if you create the YOLO v2 network layer-by-layer, the anchor boxes are defined by using the yolov2OutputLayer function.

Object Functions

detectDetect objects using YOLO v2 object detector configured for monocular camera


collapse all

Configure a YOLO v2 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.

Load a yolov2ObjectDetector object pretrained to detect vehicles.

detector = vehicleDetectorYOLOv2;

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 2–3 meters. The configured detector is a yolov2ObjectDetectorMonoCamera object.

vehicleWidth = [2 3];
detectorMonoCam = configureDetectorMonoCamera(detector,sensor,vehicleWidth);

Read in an image captured by the camera.

I = imread('carsinfront.png');

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,labels] = detect(detectorMonoCam,I);
I = insertObjectAnnotation(I,'rectangle',bboxes,scores,'AnnotationColor','g');

Display the labels for detected bounding boxes. The labels specify the class names of the detected objects.


Version History

Introduced in R2019a