资源简介
使用混合高斯背景建模方法,进行视频中运动物体的检测。
视频采用matlab自带的视频。
适用于背景静止的视频。
会用方框框出运动物体,阈值可自行调节。
含实验报告。课程实验,仅供参考。
代码片段和文件信息
close all;
clear all;
clc;
%运动物体检测-混合高斯背景建模
%视频采用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
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5391 2016-12-26 15:21 MotionDetect.m
文件 1277604 2017-01-12 12:09 课程设计.docx
- 上一篇:PSK解调Multisim仿真工程
- 下一篇:kriging工具箱
评论
共有 条评论