• 大小: 5.76MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-09-22
  • 语言: Matlab
  • 标签:

资源简介

matlab官方所提供的基于卡尔曼滤波的目标跟踪算法demo,核心部分使用了计算机视觉工具箱,本人将其简化并加入汉语注释,使其更加通俗易懂,运行环境为matlab2014

资源截图

代码片段和文件信息

clc;

frame            = [];  
detectedLocation = [];  %检测到的位置
trackedLocation  = [];  %跟踪到的位置
label            = ‘‘;  %球的标签
  
motionModel           = ‘ConstantAcceleration‘;       %运动模式
initialLocation       = ‘Same as first detection‘;    %初始化位置
initialEstimateError  = 1E5 * ones(1 3);             %初始化估算误差
motionNoise           = [25 10 1];                  %运动噪声
measurementNoise      = 25;                           %测量噪声
segmentationThreshold = 0.05;                         %分割阈值

videoReader = vision.VideoFileReader(‘movie.avi‘);
videoPlayer = vision.VideoPlayer(‘Position‘ [100100500400]);
foregroundDetector = vision.ForegroundDetector(...                     %前景色探测器
  ‘NumTrainingframes‘ 10 ‘InitialVariance‘ segmentationThreshold);
blobAnalyzer = vision.BlobAnalysis(‘AreaOutputPort‘ false ...        
    ‘MinimumBlobArea‘ 70 ‘CentroidOutputPort‘ true);
accumulatedImage      = 0;                            %累积的图片
accumulatedDetections = zeros(0 2);                  %累积的探测值
accumulatedTrackings  = zeros(0 2);                  %累积的踪迹
isTrackInitialized = false;

while ~isDone(videoReader)
    frame = step(videoReader);                        
    grayImage = rgb2gray(frame);                      %图像灰度化
    foreground = step(foregroundDetector grayImage);
    detectedLocation = step(blobAnalyzer foreground);
    if isempty(detectedLocation)
        isobjectDetected = false;
    else
        isobjectDetected = true;
        detectedLocation = detectedLocation(1 :);
    end
    
    if ~isTrackInitialized
        if isobjectDetected
            if strcmp(initialLocation ‘Same as first detection‘)
                initialLocation = detectedLocation;
            end
            
            kalmanFilter = configureKalmanFilter(motionModel ...
            initialLocation initialEstimateError ...
            motionNoise measurementNoise);
        
            isTrackInitialized = true;
            trackedLocation = correct(kalmanFilter detectedLocation);
            label = ‘Initial‘;
        else
            trackedLocation = [];
            label = ‘‘;
        end
    else
        if isobjectDetected
            predict(kalmanFilter);
            trackedLocation = correct(kalmanFilter detectedLocation);
            label = ‘Corrected‘;
        else
            trackedLocation = predict(kalmanFilter);
            label = ‘Predicted‘;
        end   
    end
    
    accumulatedImage = max(accumulatedImage frame);
    accumulatedDetections = [accumulatedDetections; detectedLocation];
    accumulatedTrackings  = [accumulatedTrackings; trackedLocation];
    
    combinedImage = max(repmat(foreground [113]) frame);
    
    if ~isempty(trackedLocation)
        shape = ‘circle‘;
        region = trackedLocation;
        region(: 3) = 5;
        combinedImage = insertobjectAnnotation(combinedImage shape ...
          region {label} ‘Color‘ ‘red‘

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-06-11 10:47  kalman目标跟踪\
     文件        3424  2016-04-30 09:50  kalman目标跟踪\demoForTracking.m
     文件    23405568  2012-05-12 06:37  kalman目标跟踪\movie.avi
     文件        8704  2016-06-11 10:47  kalman目标跟踪\Thumbs.db

评论

共有 条评论