【视频检测】基于LK金字塔光流法实现视频目标跟踪定位附matlab代码

本文介绍了利用Harris角点和光流技术进行智能视频分析中的运动目标跟踪。通过MATLAB代码展示了如何实现Lucas-Kanade逆仿射跟踪算法,对多个模板进行初始化并更新,同时监控像素误差。实验结果展示了跟踪过程中模板的位置和像素误差变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 简介

高性能计算机的普及、高性价比摄像头的广泛使用以及对智能视频分析日益增长的需求,许多性能优异的跟踪算法不断涌现.智能视频分析主要包括三个方面:运动目标检测、图像序列中的运动目标跟踪以及目标行为的认知分析¨J.运动物体有多种局部特征可以用来进行跟踪,常用的特征包括轮廓、边缘、兴趣点、纹理、形状、颜色等,其中,最易提取的局部特征是点特征.本文选用Harris角点作为跟踪对象,其具有旋转不变性和对光照变化不敏感的特性,适合跟踪.运动场可以用来描述真实世界中物体的三维运动,而光流场是运动场在二维图像平面上的投影.1981年Horn和Schunck将二维速度场和灰度联系起来,提出著名的亮度恒定假设J,建立了光流约束方程​.此后,光流技术得到广泛关注.

2 部分代码

<span style="color:#333333"><span style="background-color:rgba(0, 0, 0, 0.03)"><code><span style="color:#afafaf">%</span> Start with clean Matlab Workspace</code><code>clear all; close all; clc</code><code>% Compile the fast Lucas Kanade c-code tracking</code><code><span style="color:#afafaf">%</span> mex LucasKanadeInverseAffine.c -v</code><code>% Load a Traffic Movie, frames <span style="color:#ca7d37">in</span> stack with 3th dimension time</code><code><span style="color:#afafaf">%</span></code><code><span style="color:#afafaf">%</span> (Only differences between frames are stored, <span style="color:#ca7d37">for</span> 3x smaller .mat filesize.</code><code><span style="color:#afafaf">%</span> Integrate to get the approximated original movie back)</code><code>load('TTdemo_packed_movie'); Vmovie=uint8(cumsum(single(Vmovie),3)+128);</code><code>% Get the first movie frame</code><code>I = double(Vmovie(:,:,1))*(1/255);</code><code>% Show the movie frame</code><code>figure, imshow(I,[])</code><code>% Make a struct to store templates</code><code>TemplateData=struct;</code><code>% Select the coordinates of 2 templates</code><code><span style="color:#afafaf">%</span> rect=getrect; TempPos1=round([rect(2) rect(2)+rect(4);rect(1) rect(1)+rect(3);]);</code><code><span style="color:#afafaf">%</span> rect=getrect; TempPos2=round([rect(2) rect(2)+rect(4);rect(1) rect(1)+rect(3);]);</code><code>TempPos1=[086,111;100,134];</code><code>TempPos2=[054,091;224,256];</code><code>TempPos3=[093,103;285,312];</code><code>TempPos4=[154,171;250,266];</code><code>% Pad the select templates with extra boundary pixels. These boundary</code><code><span style="color:#afafaf">%</span> pixels are not used <span style="color:#ca7d37">for</span> the actual template tracking. But to get</code><code><span style="color:#afafaf">%</span> more reliable image derivatives.</code><code>b=5;</code><code>padding=[-b,b;-b,b];</code><code>TempPos1=TempPos1+padding;</code><code>TempPos2=TempPos2+padding;</code><code>TempPos3=TempPos3+padding;</code><code>TempPos4=TempPos4+padding;</code><code>​</code><code>% -Set initial parameters of the templates</code><code><span style="color:#afafaf">%</span> -Set padded template image.</code><code><span style="color:#afafaf">%</span> </code><code><span style="color:#afafaf">%</span> Backwards affine Transformation Matrix is used <span style="color:#ca7d37">in</span> Lucas Kanade Tracking</code><code><span style="color:#afafaf">%</span> with 6 parameters</code><code><span style="color:#afafaf">%</span> M    = [ 1+p(1) p(3)   p(5); </code><code><span style="color:#afafaf">%</span>          p(2)   1+p(4) p(6); </code><code><span style="color:#afafaf">%</span>          0      0      1];</code><code><span style="color:#afafaf">%</span></code><code>center=[TempPos1(1,1)+TempPos1(1,2)-1 TempPos1(2,1)+TempPos1(2,2)-1]/2;</code><code>TemplateData(1).p=[0 0 0 0 center(1) center(2)];</code><code>TemplateData(1).image=I(TempPos1(1,1):TempPos1(1,2),TempPos1(2,1):TempPos1(2,2));</code><code><span style="color:#afafaf">%</span> This weight <span style="color:#ca7d37">function</span> is used <span style="color:#ca7d37">in</span> the LK-Hessian and multiplied with the </code><code><span style="color:#afafaf">%</span> error between <span style="color:#ca7d37">in</span> image and template. And is used to exclude unreliable pixels form</code><code><span style="color:#afafaf">%</span> the template tracking.</code><code>TemplateData(1).weight=im2double(imread('weight1.png'));</code><code>​</code><code>​</code><code>center=[TempPos2(1,1)+TempPos2(1,2)-1 TempPos2(2,1)+TempPos2(2,2)-1]/2;</code><code>TemplateData(2).p=[0 0 0 0 center(1) center(2)];</code><code>TemplateData(2).image=I(TempPos2(1,1):TempPos2(1,2),TempPos2(2,1):TempPos2(2,2));</code><code>TemplateData(2).weight=im2double(imread('weight2.png'));</code><code> </code><code>center=[TempPos3(1,1)+TempPos3(1,2)-1 TempPos3(2,1)+TempPos3(2,2)-1]/2;</code><code>TemplateData(3).p=[0 0 0 0 center(1) center(2)];</code><code>TemplateData(3).image=I(TempPos3(1,1):TempPos3(1,2),TempPos3(2,1):TempPos3(2,2));</code><code>TemplateData(3).weight=im2double(imread('weight3.png'));</code><code>​</code><code>center=[TempPos4(1,1)+TempPos4(1,2)-1 TempPos4(2,1)+TempPos4(2,2)-1]/2;</code><code>TemplateData(4).p=[0 0 0 0 center(1) center(2)];</code><code>TemplateData(4).image=I(TempPos4(1,1):TempPos4(1,2),TempPos4(2,1):TempPos4(2,2));</code><code>TemplateData(4).weight=im2double(imread('weight4.png'));</code><code>% LK Tracking Options (other options default)</code><code>Options.TranslationIterations=30;</code><code>Options.AffineIterations=0;</code><code>Options.RoughSigma=3;</code><code>Options.FineSigma=1.5;</code><code>% Make a colormap</code><code>cmap=hot(256);</code><code>% Matrix to store squared pixel error between template and ROI <span style="color:#ca7d37">in</span></code><code><span style="color:#afafaf">%</span> movieframe after template tracking.</code><code>T_error=zeros(size(Vmovie,3), length(TemplateData));</code><code>% Loop through the movie frames</code><code>for i=1:size(Vmovie,3)</code><code>    % Get the a movie frame</code><code>    I = double(Vmovie(:,:,i))*(1/255);</code><code>    </code><code>    % Do the tracking for all templates, using Lucas Kanade Inverse Affine</code><code>    for t=1:length(TemplateData)</code><code>        [TemplateData(t).p,ROIimage,T_error(i,t)]=LucasKanadeInverseAffine(I,TemplateData(t).p,TemplateData(t).image,TemplateData(t).weight,Options);</code><code>            </code><code><span style="color:#afafaf">%</span> % Weights update, see paper <span style="color:#dd1144">"Robust template tracking with drift correction"</span></code><code><span style="color:#afafaf">%</span></code><code><span style="color:#afafaf">%</span> % Constant used <span style="color:#ca7d37">for</span> the weight <span style="color:#ca7d37">function</span> [0..1], with a lower value</code><code><span style="color:#afafaf">%</span> % the weight <span style="color:#ca7d37">function</span> will be less depended on the current error between</code><code><span style="color:#afafaf">%</span> % template and image (more average of itterations) <span style="color:#ca7d37">then</span> with a higher</code><code><span style="color:#afafaf">%</span> % value.</code><code><span style="color:#afafaf">%</span> alpha = 0.1;</code><code><span style="color:#afafaf">%</span>         <span style="color:#ca7d37">if</span>(i>1)</code><code><span style="color:#afafaf">%</span>             TemplateData(t).E(:,:,i)=(1-alpha)*TemplateData(t).E(:,:,i-1)+alpha*abs(ROIimage-TemplateData(t).image);</code><code><span style="color:#afafaf">%</span>         <span style="color:#ca7d37">else</span></code><code><span style="color:#afafaf">%</span>             TemplateData(t).E(:,:,i)=abs(ROIimage-TemplateData(t).image);</code><code><span style="color:#afafaf">%</span>         end</code><code><span style="color:#afafaf">%</span>         <span style="color:#ca7d37">if</span>(i>5)</code><code><span style="color:#afafaf">%</span>             TemplateData(t).weight=double(TemplateData(t).E(:,:,i)<=median(TemplateData(t).E(:,:,4:i),3)*1.4826); </code><code><span style="color:#afafaf">%</span>         end</code><code>    end</code><code>    </code><code>    % Show the movie frame</code><code>    if(i==1), figure, handle_imshow=imshow(I); hold on</code><code>    else</code><code>        for t=1:length(TemplateData), delete(h(t)); end; </code><code>        set(handle_imshow,'Cdata',I); </code><code>    end</code><code>​</code><code>    % Show the location of the templates in the movie frame</code><code>    for t=1:length(TemplateData)</code><code>        h(t)=plot(TemplateData(t).p(6),TemplateData(t).p(5),'go','MarkerFaceColor',cmap(round(t*255/length(TemplateData))+1,:)); </code><code>        drawnow</code><code>    end</code><code>​</code><code>end</code><code>% Show the squared pixel errors between template and ROI during the movie frames</code><code>figure,</code><code>subplot(2,2,1),plot(T_error(:,1)); title('Pixel^2 error template 1')</code><code>subplot(2,2,2),plot(T_error(:,2)); title('Pixel^2 error template 2')</code><code>subplot(2,2,3),plot(T_error(:,3)); title('Pixel^2 error template 3')</code><code>subplot(2,2,4),plot(T_error(:,4)); title('Pixel^2 error template 3')</code><code>​</code></span></span>

3 仿真结果

4 参考文献

[1]肖军. "基于光流法的运动目标检测与跟踪算法." 东北大学学报:自然科学版 37.6(2016):5.

博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。

部分理论引用网络文献,若有侵权联系博主删除。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

matlab科研助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值