1 简介
高性能计算机的普及、高性价比摄像头的广泛使用以及对智能视频分析日益增长的需求,许多性能优异的跟踪算法不断涌现.智能视频分析主要包括三个方面:运动目标检测、图像序列中的运动目标跟踪以及目标行为的认知分析¨J.运动物体有多种局部特征可以用来进行跟踪,常用的特征包括轮廓、边缘、兴趣点、纹理、形状、颜色等,其中,最易提取的局部特征是点特征.本文选用Harris角点作为跟踪对象,其具有旋转不变性和对光照变化不敏感的特性,适合跟踪.运动场可以用来描述真实世界中物体的三维运动,而光流场是运动场在二维图像平面上的投影.1981年Horn和Schunck将二维速度场和灰度联系起来,提出著名的亮度恒定假设J,建立了光流约束方程.此后,光流技术得到广泛关注.
2 部分代码
% Start with clean Matlab Workspaceclear all; close all; clc% Compile the fast Lucas Kanade c-code tracking% mex LucasKanadeInverseAffine.c -v% Load a Traffic Movie, frames in stack with 3th dimension time%% (Only differences between frames are stored, for 3x smaller .mat filesize.% Integrate to get the approximated original movie back)load('TTdemo_packed_movie'); Vmovie=uint8(cumsum(single(Vmovie),3)+128);% Get the first movie frameI = double(Vmovie(:,:,1))*(1/255);% Show the movie framefigure, imshow(I,[])% Make a struct to store templatesTemplateData=struct;% Select the coordinates of 2 templates% rect=getrect; TempPos1=round([rect(2) rect(2)+rect(4);rect(1) rect(1)+rect(3);]);% rect=getrect; TempPos2=round([rect(2) rect(2)+rect(4);rect(1) rect(1)+rect(3);]);TempPos1=[086,111;100,134];TempPos2=[054,091;224,256];TempPos3=[093,103;285,312];TempPos4=[154,171;250,266];% Pad the select templates with extra boundary pixels. These boundary% pixels are not used for the actual template tracking. But to get% more reliable image derivatives.b=5;padding=[-b,b;-b,b];TempPos1=TempPos1+padding;TempPos2=TempPos2+padding;TempPos3=TempPos3+padding;TempPos4=TempPos4+padding;% -Set initial parameters of the templates% -Set padded template image.% % Backwards affine Transformation Matrix is used in Lucas Kanade Tracking% with 6 parameters% M = [ 1+p(1) p(3) p(5); % p(2) 1+p(4) p(6); % 0 0 1];%center=[TempPos1(1,1)+TempPos1(1,2)-1 TempPos1(2,1)+TempPos1(2,2)-1]/2;TemplateData(1).p=[0 0 0 0 center(1) center(2)];TemplateData(1).image=I(TempPos1(1,1):TempPos1(1,2),TempPos1(2,1):TempPos1(2,2));% This weight function is used in the LK-Hessian and multiplied with the % error between in image and template. And is used to exclude unreliable pixels form% the template tracking.TemplateData(1).weight=im2double(imread('weight1.png'));center=[TempPos2(1,1)+TempPos2(1,2)-1 TempPos2(2,1)+TempPos2(2,2)-1]/2;TemplateData(2).p=[0 0 0 0 center(1) center(2)];TemplateData(2).image=I(TempPos2(1,1):TempPos2(1,2),TempPos2(2,1):TempPos2(2,2));TemplateData(2).weight=im2double(imread('weight2.png')); center=[TempPos3(1,1)+TempPos3(1,2)-1 TempPos3(2,1)+TempPos3(2,2)-1]/2;TemplateData(3).p=[0 0 0 0 center(1) center(2)];TemplateData(3).image=I(TempPos3(1,1):TempPos3(1,2),TempPos3(2,1):TempPos3(2,2));TemplateData(3).weight=im2double(imread('weight3.png'));center=[TempPos4(1,1)+TempPos4(1,2)-1 TempPos4(2,1)+TempPos4(2,2)-1]/2;TemplateData(4).p=[0 0 0 0 center(1) center(2)];TemplateData(4).image=I(TempPos4(1,1):TempPos4(1,2),TempPos4(2,1):TempPos4(2,2));TemplateData(4).weight=im2double(imread('weight4.png'));% LK Tracking Options (other options default)Options.TranslationIterations=30;Options.AffineIterations=0;Options.RoughSigma=3;Options.FineSigma=1.5;% Make a colormapcmap=hot(256);% Matrix to store squared pixel error between template and ROI in% movieframe after template tracking.T_error=zeros(size(Vmovie,3), length(TemplateData));% Loop through the movie framesfor i=1:size(Vmovie,3) % Get the a movie frame I = double(Vmovie(:,:,i))*(1/255); % Do the tracking for all templates, using Lucas Kanade Inverse Affine for t=1:length(TemplateData) [TemplateData(t).p,ROIimage,T_error(i,t)]=LucasKanadeInverseAffine(I,TemplateData(t).p,TemplateData(t).image,TemplateData(t).weight,Options); % % Weights update, see paper "Robust template tracking with drift correction"%% % Constant used for the weight function [0..1], with a lower value% % the weight function will be less depended on the current error between% % template and image (more average of itterations) then with a higher% % value.% alpha = 0.1;% if(i>1)% TemplateData(t).E(:,:,i)=(1-alpha)*TemplateData(t).E(:,:,i-1)+alpha*abs(ROIimage-TemplateData(t).image);% else% TemplateData(t).E(:,:,i)=abs(ROIimage-TemplateData(t).image);% end% if(i>5)% TemplateData(t).weight=double(TemplateData(t).E(:,:,i)<=median(TemplateData(t).E(:,:,4:i),3)*1.4826); % end end % Show the movie frame if(i==1), figure, handle_imshow=imshow(I); hold on else for t=1:length(TemplateData), delete(h(t)); end; set(handle_imshow,'Cdata',I); end % Show the location of the templates in the movie frame for t=1:length(TemplateData) h(t)=plot(TemplateData(t).p(6),TemplateData(t).p(5),'go','MarkerFaceColor',cmap(round(t*255/length(TemplateData))+1,:)); drawnow endend% Show the squared pixel errors between template and ROI during the movie framesfigure,subplot(2,2,1),plot(T_error(:,1)); title('Pixel^2 error template 1')subplot(2,2,2),plot(T_error(:,2)); title('Pixel^2 error template 2')subplot(2,2,3),plot(T_error(:,3)); title('Pixel^2 error template 3')subplot(2,2,4),plot(T_error(:,4)); title('Pixel^2 error template 3')
3 仿真结果
4 参考文献
[1]肖军. "基于光流法的运动目标检测与跟踪算法." 东北大学学报:自然科学版 37.6(2016):5.
博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。
部分理论引用网络文献,若有侵权联系博主删除。