%% Tracking Pedestrians from a Moving Car
%
% This example shows how to track pedestrians using a camera mounted in a
% moving car.
%
% Copyright 2014 The MathWorks, Inc.
%% Overview
% This example shows how to perform automatic detection and tracking of
% people in a video from a moving camera. It demonstrates the flexibility
% of a tracking system adapted to a moving camera, which is ideal for
% automotive safety applications. Unlike the stationary camera
% example, <motion-based-multiple-object-tracking.html The Motion-Based
% Multiple Object Tracking>, this example contains several additional
% algorithmic steps. These steps include people detection, customized
% non-maximum suppression, and heuristics to identify and eliminate false
% alarm tracks. For more information please see
% <matlab:helpview(fullfile(docroot,'toolbox','vision','vision.map'),'multipleObjectTracking') Multiple Object Tracking>.
%
% This example is a function with the main body at the top and helper
% routines in the form of
% <matlab:helpview(fullfile(docroot,'toolbox','matlab','matlab_prog','matlab_prog.map'),'nested_functions') nested functions>
% below.
function PedestrianTrackingFromMovingCameraExample()
% Create system objects used for reading video, loading prerequisite data file, detecting pedestrians, and displaying the results.
videoFile = 'vippedtracking.avi';
scaleDataFile = 'pedScaleTable.mat'; % An auxiliary file that helps to determine the size of a pedestrian at different pixel locations.
obj = setupSystemObjects(videoFile, scaleDataFile);
% Create an empty array of tracks.
tracks = initializeTracks();
% ID of the next track.
nextId = 1;
% Set the global parameters.
option.ROI = [1,240,1280,480]; % A rectangle [x, y, w, h] that limits the processing area to ground locations.
option.scThresh = 0.3; % A threshold to control the tolerance of error in estimating the scale of a detected pedestrian.
option.gatingThresh = 0.9; % A threshold to reject a candidate match between a detection and a track.
option.gatingCost = 100; % A large value for the assignment cost matrix that enforces the rejection of a candidate match.
option.costOfNonAssignment = 10; % A tuning parameter to control the likelihood of creation of a new track.
option.timeWindowSize = 16; % A tuning parameter to specify the number of frames required to stabilize the confidence score of a track.
option.confidenceThresh = 2; % A threshold to determine if a track is true positive or false alarm.
option.ageThresh = 8; % A threshold to determine the minimum length required for a track being true positive.
option.visThresh = 0.6; % A threshold to determine the minimum visibility value for a track being true positive.
% Detect people and track them across video frames.
cont = ~isDone(obj.reader);
while cont
frame = readFrame();
[centroids, bboxes, scores] = detectPeople();
predictNewLocationsOfTracks();
[assignments, unassignedTracks, unassignedDetections] = ...
detectionToTrackAssignment();
updateAssignedTracks();
updateUnassignedTracks();
deleteLostTracks();
createNewTracks();
displayTrackingResults();
% Exit the loop if the video player figure is closed by user.
cont = ~isDone(obj.reader) && isOpen(obj.videoPlayer);
end
%% Auxiliary Input and Global Parameters of the Tracking System
% This tracking system requires a data file that contains information that
% relates the pixel location in the image to the size of the bounding box
% marking the pedestrian's location. This prior knowledge is stored in a
% vector |pedScaleTable|. The n-th entry in |pedScaleTable| represents the
% estimated height of an adult person in pixels. The index |n| references
% the approximate Y-coordinate of the pedestrian's feet.
%
% To obtain such a vector, a collection of training images were taken from
% the same viewpoint and in a similar scene to the testing environment. The
% training images contained images of pedestrians at varying distances
% from the camera. Using the
% <matlab:helpview(fullfile(docroot,'toolbox','vision','vision.map'),'visionTrainingImageLabeler'); trainingImageLabeler>
% app, bounding boxes of the pedestrians in the images were manually
% annotated. The height of the bounding boxes together with the location of
% the pedestrians in the image were used to generate the scale data file
% through regression. Here is a helper function to show the algorithmic steps to
% fit the linear regression model:
% <matlab:edit(fullfile(matlabroot,'toolbox','vision','visiondemos','helperTableOfScales.m')) |helperTableOfScales.m|>
%
%
% There is also a set of global parameters that can be tuned to optimize
% the tracking performance. You can use the descriptions below to learn out
% how these parameters affect the tracking performance.
%
% * |ROI| : Region-Of-Interest in the form of
% [x, y, w, h]. It limits the processing area to
% ground locations.
% * |scThresh| : Tolerance threshold for scale estimation.
% When the difference between the detected scale
% and the expected scale exceeds the tolerance,
% the candidate detection is considered to be
% unrealistic and is removed from the output.
% * |gatingThresh| : Gating parameter for the distance measure. When
% the cost of matching the detected bounding box
% and the predicted bounding box exceeds the
% threshold, the system removes the association
% of the two bounding boxes from tracking
% consideration.
% * |gatingCost| : Value for the assignment cost matrix to
% discourage the possible tracking to detection
% assignment.
% * |costOfNonAssignment| : Value for the assignment cost matrix for
% not assigning a detection or a track. Setting
% it too low increases the likelihood of
% creating a new track, and may result in track
% fragmentation. Setting it too high may result
% in a single track corresponding to a series of
% separate moving objects.
% * |timeWindowSize| : Number of frames required to estimate the
% confidence of the track.
% * |confidenceThresh| : Confidence threshold to determine if the
% track is a true positive.
% * |ageThresh| : Minimum length of a track being a true positive.
% * |visThresh| : Minimum visibility threshold to determine if
% the track is a true positive.
%% Create System Objects for the Tracking System Initialization
% The |setupSystemObjects| function creates system objects used for reading
% the video frames and the scale data file, to detect people, and display
% results.
%
% For the people detector, we set the 'ClassificationThreshold' to be higher
% than the default value in order to remove false detections. We also
% disable the 'MergeDetections' option in order to perform customized, more
% robust non-maximum suppression that uses the scale data file described above.
%
% The |pedScaleTable| vector, which is stored in the scale data file,
% encodes our prior knowledge of the target and the scene. Once you have the
% regressor trained from your samples, you can compute the expected height
% at every possible Y-position in the image. These values are stored in the
% vector. T