www.gusucode.com > IPCV_Eval_Kit_R2019a_0ce6858工具箱matlab程序源码 > IPCV_Eval_Kit_R2019a_0ce6858/code/demo_files/I4_08_1_myTrackSingleObject.m

    clear all;clc;close all;imtool close all

%% 摦夋偺昞帵 (僗僋儕僾僩)
videoReader = vision.VideoFileReader('singleball.mp4');
sz = get(0,'ScreenSize');
videoPlayer = vision.VideoPlayer('Position', [10,sz(4)-500,500,400]);
while ~isDone(videoReader)
  frame = step(videoReader);  % 1僼儗乕儉撉傒庢傝
  step(videoPlayer, frame);   % 價僨僆1僼儗乕儉昞帵
end

%% 摦夋偺昞帵 (傾僾儕働乕僔儑儞)
implay('singleball.mp4');

%% 儃乕儖傪僩儔僢僉儞僌 丗僇儖儅儞僼傿儖僞乕 %%%%%%%%%%%%%%%%%%%
% 塣摦曽掱幃傪梡偄梊應偟偨傝丄岆嵎傪娷傓娤應抣傪曗惓
% 僇儖儅儞僼傿儖僞乕偲丄儃乕儖偺僙僌儊儞僥乕僔儑儞偺丄僷儔儊乕僞愝掕乮M師尦偺捈岎嵗昗宯偵懳墳乯
% 僇儖儅儞僼傿儖僞乕偺僐儞僗僩儔僋僞傪梡偄傞偙偲偱嵶偐偔儌僨儖偺掕媊摍壜擻
param.motionModel           = 'ConstantVelocity';  % 埵抲悇掕偵梡偄傞塣摦曽掱幃丗堦掕懍搙偱堏摦偡傞偲偟偰丄師偺埵抲傪悇掕丅摍壛懍搙儌僨儖傕壜
param.initialEstimateError  = 1E5 * ones(1, 2);    % 偦傟偧傟僩儔僢僉儞僌弶婜偺埵抲瑩x偺悇掕抣偵懳偡傞暘嶶 (惓婯暘晍)
param.motionNoise           = [25, 10];            % 塣摦曽掱幃偵懳偡傞岆嵎偺暘嶶 (埵抲丄懍搙)
param.measurementNoise      = 25;                  % 専弌偝傟偨埵抲偵懳偡傞岆嵎偺暘嶶 (惓婯暘晍)
param.segmentationThreshold = 0.05;
  
%% 僔僗僥儉僆僽僕僃僋僩嶌惉
videoReader = vision.VideoFileReader('singleball.mp4');
sz = get(0,'ScreenSize');
videoPlayer = vision.VideoPlayer('Position', [180,sz(4)-490,500,400]);
foregroundDetector = vision.ForegroundDetector('NumTrainingFrames', 10, 'InitialVariance', param.segmentationThreshold);
blobAnalyzer = vision.BlobAnalysis('AreaOutputPort', false, 'MinimumBlobArea', 70, 'CentroidOutputPort', true);   % 拞怱揰偺嶼弌

isTrackInitialized    = false;
trackedPositions = [0 0 0];
position = [];

%% 僐儅憲傝儃僞儞昞帵
a=true;
sz = get(0,'ScreenSize');
figure('MenuBar','none','Toolbar','none','Position',[20 sz(4)-100 100 70])
uicontrol('Style', 'pushbutton', 'String', '師偺僼儗乕儉',...
        'Position', [20 20 80 40],'Callback', 'a=false;');

%% 摦夋傪堦僼儗乕儉偢偮張棟
%      "師偺僼儗乕儉" 偺儃僞儞偱丄僐儅憲傝
while (a) && ~isDone(videoReader)

  frame = step(videoReader);     % 1僼儗乕儉撉傒庢傝

  % 儃乕儖 (慜宨)偺専弌丒拞怱揰専弌
  foregroundMask   = step(foregroundDetector, frame);
  detectedLocation = step(blobAnalyzer, foregroundMask);
  isObjectDetected = ~isempty(detectedLocation);

    if ~isTrackInitialized   % 僩儔僢僉儞僌巒傑偭偰偄側偄偲偒
      if isObjectDetected      % 嵟弶偵儃乕儖傪専弌偟偨偲偒
        % 儃乕儖偑嵟弶偵専弌偝傟偨帪丄僇儖儅儞僼傿儖僞乕傪嶌惉
        kalmanFilter = configureKalmanFilter(param.motionModel, ...
          detectedLocation, param.initialEstimateError, ...             %専弌偝傟偨応強傪弶婜埵抲偵愝掕
          param.motionNoise, param.measurementNoise);

        isTrackInitialized = true;
        trackedLocation = correct(kalmanFilter, detectedLocation);
        label = 'Initial';
      else   % 儃乕儖偑傑偩尒偮偐偭偰偄側偄応崌
        trackedLocation = [];label = '';
      end

    else    % 僩儔僢僉儞僌拞偺応崌 (僇儖儅儞僼傿儖僞偱僩儔僢僉儞僌)
      if isObjectDetected    % 儃乕儖偑専弌偝傟偨応崌
        predict(kalmanFilter);  % 夋憸僲僀僘摍偵傛傞埵抲専弌岆嵎傪丄梊應抣偱掅尭(correction)
        trackedLocation = correct(kalmanFilter, detectedLocation);
        label = 'Corrected';
      else  % 僩儔僢僉儞僌拞偵儃乕儖偑尒偮偐傜側偐偭偨応崌
        trackedLocation = predict(kalmanFilter); % 儃乕儖埵抲傪梊應
        label = 'Predicted';
      end
    end

  % 摦夋昞帵
  % 専弌偝傟偨偲偒丄専弌偝傟偨埵抲偵惵廫帤儅乕僋
  if isObjectDetected
    combinedImage = insertMarker(frame, detectedLocation, 'Color','blue');
  else
    combinedImage = frame;
  end
  
  % 僩儔僢僋(専弌or梊應)偝傟偰偄傞偲偒丄応強偵愒娵傪廳偹彂偒
  if ~isempty(trackedLocation)
    position = trackedLocation;
    position(:, 3) = 5;           % 墌偺敿宎
    combinedImage = insertObjectAnnotation(combinedImage, 'circle', position, {label}, 'Color', 'red');
    %combinedImage = insertMarker(combinedImage, trackedLocation, 'Color','red');
  end
  
  % 夁嫀偺揰偵椢娵
  combinedImage = insertShape(combinedImage, 'Circle', trackedPositions, 'Color', 'green');
  trackedPositions = [trackedPositions; position];

  step(videoPlayer, combinedImage);   % 價僨僆1僼儗乕儉昞帵
  
  while (a) 
    drawnow;   % 僾僢僔儏儃僞儞偺僀儀儞僩偺妋擣
  end;
  a = true;
end % while
  
%%
release(videoReader);
release(videoPlayer);
release(foregroundDetector);
release(blobAnalyzer);

%% 廔椆










%% 婳愓傪昞帵偡傞応崌
% [while 儖乕僾偺慜偵丄]
%   accumulatedImage      = 0;
%   accumulatedDetections = zeros(0, 2);
%   accumulatedTrackings  = zeros(0, 2);
% [while 儖乕僾偺嵟屻偵]
%   accumulatedImage      = max(accumulatedImage, frame);
%   accumulatedDetections ...
%          = [accumulatedDetections; detectedLocation];
%   accumulatedTrackings  ...
%          = [accumulatedTrackings; trackedLocation];
% [while 儖乕僾偺嵟屻偵]
%   figure; imshow(accumulatedImage/2+0.5); hold on;
%   plot(accumulatedDetections(:,1), ...     %専弌偝傟偨埵抲偵崟亄報
%        accumulatedDetections(:,2), 'k+');
%   plot(accumulatedTrackings(:,1), ...      %僩儔僢僉儞僌寢壥傪愒娵偲捈慄偱昞帵
%        accumulatedTrackings(:,2), 'r-o');
%   legend('Detection', 'Tracking');

%% vision.DeployableVideoPlayer傪巊偆応崌
% videoPlayer = vision.DeployableVideoPlayer('Location', [10,sz(4)-500]);

% Copyright 2014 The MathWorks, Inc.