该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
function x = Wh
%初始化背景所需要的帧数
N=15;%背景更新参数
mov = aviread('smoke.AVI');%读取AVI视频文件
%获取视频帧数
dimension = size(mov);
frameQTY = dimension(2);
M=mov;
tmFr = rgb2gray(frame2im(mov(1)));%变成灰度图
s = size(tmFr);
scrsz = get(0,'ScreenSize');%获取屏幕大小
h1 = figure('position', [1,1,s(2)*2,s(1)*2]);
cdataSum = int16(zeros(s));
sedisk = strel('rectangle',[2,2]);
sedisk2 = strel('rectangle',[3,2]);
count = zeros(s);
for c1 = 1:frameQTY;
colorFrC = M(c1);%获取第i帧
frC = rgb2gray(frame2im(colorFrC));
frC = im2frame(frC, gray(256));
f = int16(frC.cdata);
if( c1<= N)
%前N帧用于背景建模
movie(h1, frC, 1, 15, [0,s(1),0,0]);%播放第i帧
cdataSum = cdataSum + f;
else
%开始运动检测,首先计算出背景
if(c1==N+1)cdB = cdataSum/N;
else colorFrC1 = M(c1-N);%获取第c1-N帧
frC1 = rgb2gray(frame2im(colorFrC1));
frC1 = im2frame(frC1, gray(256));
f1 = int16(frC1.cdata);
cdataSum=cdataSum+f-f1;%背景更新
cdB=cdataSum/N;
cdB = int16(cdB);
end
f2 = abs( f - cdB );
f2 = uint8(f2);
bwf = f2;
bw = find( f2 < 45 );
bwf(bw) = 0;
bw = find( f2 >= 45 );
bwf(bw) = 255;
cdB = uint8(cdB);
f3 = imdilate(bwf, sedisk);
f3 = imerode(f3, sedisk);
noSmall = bwareaopen( f3, 150 );%删除小面积
%标签矩阵
L = bwlabel( noSmall, 4 );
tagged( :,: ) = frC.cdata;
if any( L(:) )
stats = regionprops(L,'boundingbox','filledArea');
for label = 1:length(stats)
box = stats(label);
if( box.FilledArea < 200 || box.FilledArea/(s(1)*s(2)) > 0.3 )
continue;
end
box = box.BoundingBox;
left=round(box(1));
top=round(box(2));
right=round(box(1)+box(3)-1);
bottom=round(box(2)+box(4)-1);
if( abs(top - bottom)/abs(right - left) > 4 || abs(right - left)/abs(top - bottom) > 4 )
continue;
end
tagged(top,left:right)=1; %画矩形框%
tagged(bottom,left:right)=1;
tagged(top:bottom,left)=1;
tagged(top:bottom,right)=1;
end
end
ff = [frC.cdata, cdB;f3, tagged];%正常 背景 差分 跟踪
ff = im2frame(ff,gray(256));
movie(h1, ff, 1, 15, [0,0,0,0]);%播放第i帧
end% end of if-else
end%--播放结束
end