clc
clear
FrameNum=100;
StartFrame=1;
BackGround=rgb2gray(read(VideoReader('traffic.avi'), 1));
for k=StartFrame+1:1:FrameNum
CurrentFrame=rgb2gray(read(VideoReader('traffic.avi'), k));
FormerFrame=rgb2gray(read(VideoReader('traffic.avi'), k-1));
FD=abs(CurrentFrame-FormerFrame); %帧间差分
%T=threshold(FD);
T=0.17;
bw=im2bw(FD,T);
a=0.1; %背景更新因子,控制更新速度
CurrentBack=double(BackGround).*bw+(a.*double(CurrentFrame)+(1-a).*double(BackGround)).*(1-bw); %背景更新
BackGround=CurrentBack;
FD1=abs(CurrentFrame-uint8(BackGround));
%T2=threshold(FD1);
bw2=im2bw(FD1,T);
SE=[1,1,1;1,1,1;1,1,1];
bwrode=imerode(bw2,SE);
bwdi=imdilate(bw2,SE);
bwdi2=bwmorph(bwrode,'dilate',3);
bwdi3=imopen(bw2,SE);
提取码:872o
© 著作权归作者所有