差幀法是在靜止背景下實作運動物體追蹤的一種簡單的方法,主要利用兩張二值影像的差值來實作對運動物體的追蹤,本文基于插幀法,通過適當的處理得到兩個運動物體分布的區域,從而實作對雙運動物體的追蹤,
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);
最后配上一張簡陋的效果圖:
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/293837.html
標籤:其他
上一篇:機器學習訓練集、測驗集、預測集資料扁平化(包括相對、絕對目錄;os、pandas、opencv庫的部分函式使用)
