資源簡(jiǎn)介
使用混合高斯背景建模方法,進(jìn)行視頻中運(yùn)動(dòng)物體的檢測(cè)。
視頻采用matlab自帶的視頻。
適用于背景靜止的視頻。
會(huì)用方框框出運(yùn)動(dòng)物體,閾值可自行調(diào)節(jié)。
含實(shí)驗(yàn)報(bào)告。課程實(shí)驗(yàn),僅供參考。

代碼片段和文件信息
close?all;
clear?all;
clc;
%運(yùn)動(dòng)物體檢測(cè)-混合高斯背景建模
%視頻采用matlab提供的?visiontraffic.avi?atrium.avi?singleball.avi
source=VideoReader(‘singleball.avi‘);
numframes=source.NumberOfframes;
%----frame?size?variables------------------------
fr=read(source1);%read?in?1st?frame?as?background?frame
fr_bw=rgb2gray(fr);%convert?background?to?greyscale
fr_size=size(fr);
width=source.Width;
height=source.Height;
fg=zeros(heightwidth);%to?save?the?foreground?pixels
bg_bw=zeros(heightwidth);%to?update?background
%----mog?variables------------------------------
C=3;%number?of?Gaussian?components
M=3;%number?of?background?components
D=2.5;%positive?deviation?threshold
alpha=0.01;
thresh=0.25;%foreground?threshold(0.25?or?0.75?in?paper)
sd_init=6;%initial?standard?deviation(for?new?components)
w=zeros(heightwidthC);%initialize?weights?array
sd=zeros(heightwidthC);%pixel?means
sd=zeros(heightwidthC);%pixel?standard?deviations
u_diff=zeros(heightwidthC);%difference?of?each?pixel?from?mean
p=alpha/(1/C);%initial?p?variable(used?to?update?mean?and?sd)
rank=zeros(1C);%rank?of?components(w/sd)
%----initialize?component?means?and?weights----
pixel_depth=8;??%8-bit?resolution
pixel_range=2^pixel_depth-1;%pixel?range(??of?possible?values)
for?i=1:height
????for?j=1:width
????????for?k=1:C
????????????mean(ijk)=rand*pixel_range;??%means?random?(0-255)
????????????w(ijk)=1/C;%weights?uniformly?dist
????????????sd(ijk)=sd_init;%initialize?to?sd_init
????????end
????end
end
%----process?frams------------------------------
for?n=1:numframes
????fr=read(sourcen);
????fr_bw=rgb2gray(fr);%convert?frame?to?grayscale
????
????%calculate?difference?of?pixel?values?from?mean
????for?m=1:C
????????u_diff(::m)=abs(double(fr_bw)-double(mean(::m)));
????end
????
????%update?gaussian?components?for?each?pixel
????for?i=1:height
????for?j=1:width
????????match=0;
????????for?k=1:C
????????????if(abs(u_diff(ijk))<=D*sd(ijk))%pixel?matches?component
????????????????match=1;
????????????????%update?weightsmeansdp
????????????????w(ijk)=(1-alpha)*w(ijk)+alpha;
????????????????p=alpha/w(ijk);
????????????????mean(ijk)=(1-p)*mean(ijk)+p*double(fr_bw(ij));
????????????????sd(ijk)=sqrt((1-p)*sd(ijk)^2+p*((double(fr_bw(ij))-mean(ijk)))^2);
????????????else%pixel?does?not?match?component
????????????????w(ijk)=(1-alpha)*w(ijk);%weight?slightly?decreases
????????????end
????????end
????????
????????w(ij:)=w(ij:)./sum(w(ij:));%the?sum?of?gaussian?models?for?each?pixel
????????
????????bg_bw(ij)=0;
????????for?k=1:C
????????????bg_bw(ij)=bg_bw(ij)+mean(ijk)*w(ijk);%update?background
????????????if(bg_bw(ij)>thresh)
????????????????k=k-1;
????????????????M=k;
????????????end
????????end
????????
????????%if?no?components?matchcreate?new?component
????????if(match==0)
????????????[min_wmin_w_index]=min(w(ij:));
????????????mean(ijmin_w_index)=double(fr_b
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????5391??2016-12-26?15:21??MotionDetect.m
?????文件?????1277604??2017-01-12?12:09??課程設(shè)計(jì).docx
- 上一篇:PSK解調(diào)Multisim仿真工程
- 下一篇:kriging工具箱
評(píng)論
共有 條評(píng)論