matlab调用摄像头运用差帧法实现双物体跟踪
Posted weixin_53190157
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了matlab调用摄像头运用差帧法实现双物体跟踪相关的知识,希望对你有一定的参考价值。
差帧法是在静止背景下实现运动物体追踪的一种简单的方法,主要利用两张二值图像的差值来实现对运动物体的追踪。本文基于插帧法,通过适当的处理得到两个运动物体分布的区域,从而实现对双运动物体的追踪。
vid = videoinput('winvideo', 1, 'YUY2_640x480');
set(vid, 'TriggerRepeat', 1000);
% 设置摄像头的属性(具体参数可以查阅百度)
set(vid, 'FramesPerTrigger', 1);
set(vid, 'ReturnedColorspace', 'rgb');
vid.FrameGrabInterval =2; %抓取时间间隔
vid_src=getselectedsource(vid); %add
set(vid,'Tag','motion '); %add
set(gcf,'doublebuffer','on'); %add
%set(vid, 'FrameGrabInterval', 5);%also OK
%start the video aquisition here
start(vid);
若不知电脑摄像头的参数可用下列代码查询:
% win_info=imaqhwinfo('winvideo');
% d_win_info=imaqhwinfo('winvideo',1);
% d_win_info.SupportedFormats
%初始化,为作出运动轨迹做准备
i=1;
t=1;
while(vid.FramesAcquired<=100) %100为拍摄的总帧数,可根据需要自行调整
% Get the snapshot of the current frame
%data = getsnapshot(vid);
data=getdata(vid,2);%从摄像头中截取2张图片
diff_im=imabsdiff(data(:,:,:,2),data(:,:,:,1));%将两张图片做差
% Convert the resulting grayscale image into a binary image.
diff_im = im2bw(diff_im,0.18);
if sum(diff_im(:,:))==0 %若无运动物体则继续while循环
continue;
end
% Remove all those pixels less than 300px
bw = bwareaopen(diff_im,300); %从二值图像中删除小于300个像素点的连通区域
rows = size(bw, 1); %pixels的第1维即为视频画面的行数
cols = size(bw, 2);
%寻找两个上下左右边界
for row=1:rows %上部分边界
for col=1:cols
if diff_im(row,col)>0.5
top1=row;
break;
end
end
if diff_im(row,col)>0.5
break;
end
end
for row=top1:rows
for col=1:cols
if row==rows
button1=row;
break;
break;
else
if diff_im(row,col)>0.5&&(sum(diff_im(row+1,:))==0)
button1=row;
break;
end
end
end
if row==480||diff_im(row,col)>0.5&&sum(diff_im(row+1,:))==0
break;
end
end
for col=1:cols
for row=top1:button1
if diff_im(row,col)>0.5
right1=col;
end
end
end
for col=cols:-1:1
for row=top1:button1
if diff_im(row,col)>0.5
left1=col;
end
end
end
%下部分边界
top2=0;
for row=button1+1:rows
for col=1:cols
if diff_im(row,col)>0.5
top2=row;
break;
end
end
if diff_im(row,col)>0.5
break;
end
end
if top2
for row=top2:rows
for col=1:cols
if row==rows
button2=row;
else if diff_im(row,col)>0.5&&sum(diff_im(row+1,:))==0
button2=row;
break;
break;
end
end
end
end
for col=1:cols
for row=top2:button2
if diff_im(row,col)>0.5
right2=col;
end
end
end
for col=cols:-1:1
for row=top2:button2
if diff_im(row,col)>0.5
left2=col;
end
end
end
end
wd1 = right1-left1;
hg1 = button1-top1;
widt = wd1/2;
heit = hg1/2;
cenx1 = left1+widt;
ceny1 = top1+heit;
% 显示并标记
figure(1);
% Display the image
imshow(data(:,:,:,2))%add(:,:,:,2)
hold on
if wd1>0&&hg1>0
rectangle('Position',[left1 top1 wd1 hg1], 'EdgeColor', 'r', 'LineWidth', 2);
plot(cenx1,ceny1, 'm-.s','MarkerSize',7, 'LineWidth', 1)
if cenx1>0&&ceny1>0
x(i)=cenx1;
y(i)=ceny1;
i=i+1;
end
end
if ~isempty(top2)&&top2 %经实验可知若图像不动,top2为空,以下操作会出现语法错误
wd2 = right2-left2;
hg2 = button2-top2;
widt = wd2/2;
heit = hg2/2;
cenx2 = left2+widt;
ceny2 = top2+heit;
% 显示并标记
figure(1);
% Display the image
imshow(data(:,:,:,2))%add(:,:,:,2)
hold on
if wd2>0&&hg2>0
rectangle('Position',[left2 top2 wd2 hg2], 'EdgeColor', 'r', 'LineWidth', 2);
plot(cenx2,ceny2, 'm-.s','MarkerSize',7, 'LineWidth', 1)
if cenx2>0&&ceny2>0
m(t)=cenx2;
n(t)=ceny2;
t=t+1;
end
end
end
end
hold on
plot(x,y);
plot(m,n);
hold off
% 关闭摄像头
stop(vid);
% Flush all the image data stored in the memory buffer.
flushdata(vid);
最后配上一张简陋的效果图:
以上是关于matlab调用摄像头运用差帧法实现双物体跟踪的主要内容,如果未能解决你的问题,请参考以下文章