clear all
close all
clc
clear data
disp('input video');
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%程序说明:读取并显示视频(只能读一定压缩各式的 avi视频)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
avi = aviread('SampleVideo.avi'); %读取视频
video = {avi.cdata};
for a = 1:length(video)
imagesc(video{a}); %显示一帧帧图片
axis image off
drawnow;
end;
disp('output video');
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%程序说明:目标检测
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%处理视频
if ischar(video)
% Load the video from an avi file.
avi = aviread(video); %加载视频
pixels = double(cat(4,avi(1:2:end).cdata))/255;
clear avi
else
% Compile the pixel data into a single array
pixels = double(cat(4,video{1:2:end}))/255;
clear video
end
nFrames = size(pixels,4);
for f = 1:nFrames
pixel(:,:,f) = (rgb2gray(pixels(:,:,:,f))); %将RGB图像转换为GRAY图像
end
rows=240; %需要跟视频匹配
cols=320; %
nrames=f;
figure;
for l = 2:nrames
d(:,:,l)=(abs(pixel(:,:,l)-pixel(:,:,l-1)));
k=d(:,:,l);
bw(:,:,l) = im2bw(k, .2); %将灰度图转换为二值图
bw1=bwlabel(bw(:,:,l));
imshow(bw(:,:,l))
%%%显示矩形框
hold on %显示矩形框
cou=1;
for h=1:rows
for w=1:cols
if(bw(h,w,l)>0.5)
toplen = h;
if (cou == 1)
tpln=toplen;
end
cou=cou+1;
break
end
end
end
disp(toplen);
coun=1;
for w=1:cols
for h=1:rows
if(bw(h,w,l)>0.5)
leftsi = w;
if (coun == 1)
lftln=leftsi;
coun=coun+1;
end
break
end
end
end
disp(leftsi);
disp(lftln);
widh=leftsi-lftln;
heig=toplen-tpln;
widt=widh/2;
disp(widt);
heit=heig/2;
with=lftln+widt;
heth=tpln+heit;
wth(l)=with;
hth(l)=heth;
disp(heit);
disp(widh);
disp(heig);
rectangle('Position',[lftln tpln widh heig],'EdgeColor','r');
disp(with);
disp(heth);
plot(with,heth, 'r*');
%%%drawnow用于刷新屏幕,因为Matlab程序不会马上把图像画到figure上,
%%%这时,要想实时看到图像的每一步变化情况,需要使用这个语句。
drawnow;
hold off
end
评论0