16
 правок
Изменения
MOT sample
### Элемент с максимальной вероятностью
## Восстановление положения на промежуточных кадрах
===== Multiple Object Tracking Tutorial =====
 function MultipleObjectTrackingExample()
 // Create objects used for reading video and displaying the results.
 videoObjects = setupVideoObjects('atrium.mp4');
 // Create objects used for detecting objects in the foreground of the video.
 minBlobArea = 400; % Minimum blob size, in pixels, to be considered as a detection
 detectorObjects = setupDetectorObjects(minBlobArea);
===== Create the Multi-Object Tracker =====
 tracker = multiObjectTracker(...
    'FilterInitializationFcn', @initDemoFilter, ...
    'AssignmentThreshold', 30, ...
    'DeletionThreshold', 22, ...
    'ConfirmationThreshold', [6 10] ...
    );
===== Define a Kalman Filter =====
  function filter = initDemoFilter(detection)
    // Initialize a Kalman filter for this example.
    // Define the initial state.
    state = [detection.Measurement(1); 0; detection.Measurement(2); 0];
    // Define the initial state covariance.
    stateCov = diag([50, 50, 50, 50]);
    // Create the tracking filter.
    filter = trackingKF('MotionModel', '2D Constant Velocity', ...
        'State', state, ...
        'StateCovariance', stateCov, ...
        'MeasurementNoise', detection.MeasurementNoise(1:2,1:2) ...
        );
    end
  // Count frames to create a sense of time.
  frameCount = 0;
  while hasFrame(videoObjects.reader)
     // Read a video frame and detect objects in it.
     frameCount = frameCount + 1;                                // Promote frame count
     frame = readFrame(videoObjects.reader);                     // Read frame
     [detections, mask] = detectObjects(detectorObjects, frame); // Detect objects in video frame
     // Run the tracker on the preprocessed detections.
     confirmedTracks = updateTracks(tracker, detections, frameCount);
     // Display the tracking results on the video.
     displayTrackingResults(videoObjects, confirmedTracks, frame, mask);
  end
===== Create Video Objects =====
    function videoObjects = setupVideoObjects(filename)
        // Initialize video I/O
        // Create objects for reading a video from a file, drawing the tracked
        // objects in each frame, and playing the video.
        // Create a video file reader.
        videoObjects.reader = VideoReader(filename);
        // Create two video players: one to display the video,
        // and one to display the foreground mask.
        videoObjects.maskPlayer  = vision.VideoPlayer('Position', [20, 400, 700, 400]);
        videoObjects.videoPlayer = vision.VideoPlayer('Position', [740, 400, 700, 400]);
    end
===== Create Detector Objects =====
    function detectorObjects = setupDetectorObjects(minBlobArea)
        // Create System objects for foreground detection and blob analysis
        // The foreground detector segments moving objects from the
        // background. It outputs a binary mask, where the pixel value of 1
        // corresponds to the foreground and the value of 0 corresponds to
        // the background.
        detectorObjects.detector = vision.ForegroundDetector('NumGaussians', 3, ...
            'NumTrainingFrames', 40, 'MinimumBackgroundRatio', 0.7);
        // Connected groups of foreground pixels are likely to correspond to
        // moving objects.  The blob analysis System object finds such
        // groups (called 'blobs' or 'connected components') and computes
        // their characteristics, such as their areas, centroids, and the
        // bounding boxes.
        detectorObjects.blobAnalyzer = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
            'AreaOutputPort', true, 'CentroidOutputPort', true, ...
            'MinimumBlobArea', minBlobArea);
    end
===== Detect Objects =====
    function [detections, mask] = detectObjects(detectorObjects, frame)
        // Expected uncertainty (noise) for the blob centroid.
        measurementNoise = 100*eye(2);
        // Detect foreground.
        mask = detectorObjects.detector.step(frame);
        // Apply morphological operations to remove noise and fill in holes.
        mask = imopen(mask, strel('rectangle', [6, 6]));
        mask = imclose(mask, strel('rectangle', [50, 50]));
        mask = imfill(mask, 'holes');
        // Perform blob analysis to find connected components.
        [~, centroids, bboxes] = detectorObjects.blobAnalyzer.step(mask);
        // Formulate the detections as a list of objectDetection objects.
        numDetections = size(centroids, 1);
        detections = cell(numDetections, 1);
        for i = 1:numDetections
            detections{i} = objectDetection(frameCount, centroids(i,:), ...
                'MeasurementNoise', measurementNoise, ...
                'ObjectAttributes', {bboxes(i,:)});
        end
    end
===== Display Tracking Results =====
    function displayTrackingResults(videoObjects, confirmedTracks, frame, mask)
        % Convert the frame and the mask to uint8 RGB.
        frame = im2uint8(frame);
        mask = uint8(repmat(mask, [1, 1, 3])) .* 255;
        if ~isempty(confirmedTracks)
            // Display the objects. If an object has not been detected
            // in this frame, display its predicted bounding box.
            numRelTr = numel(confirmedTracks);
            boxes = zeros(numRelTr, 4);
            ids = zeros(numRelTr, 1, 'int32');
            predictedTrackInds = zeros(numRelTr, 1);
            for tr = 1:numRelTr
                // Get bounding boxes.
                boxes(tr, :) = confirmedTracks(tr).ObjectAttributes{1}{1};
                // Get IDs.
                ids(tr) = confirmedTracks(tr).TrackID;
                if confirmedTracks(tr).IsCoasted
                    predictedTrackInds(tr) = tr;
                end
            end
            predictedTrackInds = predictedTrackInds(predictedTrackInds > 0);
            // Create labels for objects that display the predicted rather
            // than the actual location.
            labels = cellstr(int2str(ids));
            isPredicted = cell(size(labels));
            isPredicted(predictedTrackInds) = {' predicted'};
            labels = strcat(labels, isPredicted);
            // Draw the objects on the frame.
            frame = insertObjectAnnotation(frame, 'rectangle', boxes, labels);
            // Draw the objects on the mask.
            mask = insertObjectAnnotation(mask, 'rectangle', boxes, labels);
        end
        // Display the mask and the frame.
        videoObjects.maskPlayer.step(mask);
        videoObjects.videoPlayer.step(frame);
    end
 end
=== Основные трудности ===
4. [https://arxiv.org/pdf/1510.07945.pdf Learning Multi-Domain Convolutional Neural Networks for Visual Tracking]
5. [https://www.mathworks.com/help/driving/examples/multiple-object-tracking-tutorial.html Multiple object tracking tutorial]
