matlab实现混合高斯模型运动物体检测算法
clear all;
Source_Video = VideoReader('viptraffic.avi'); % read the video for processing
Out_Back = VideoWriter('GMM_Background'); Out_Back.FrameRate = 30; open(Out_Back);
Out_Dect = VideoWriter('GMM_Foreground'); Out_Dect.FrameRate = 30; open(Out_Dect);
% ----------------------- 帧尺寸变量 -----------------------
height = Source_Video.Height; width = Source_Video.Width;
% --------------------- 模型变量 -----------------------------------
C = 3; % 混合高斯模型个数(typically 3-5)
D = 2.5; % 正偏差阈值
alpha = 0.01; % 学习率 (between 0 and 1) (from paper 0.01)
thres = 0.75; % 前景阈值 (0.25 or 0.75 in paper)
sd_init = 6; % initial standard deviation (for new components) var = 36 in paper
w = zeros(height,width,C); % initialize weights array mean = zeros(height,width,C); % pixel means
sd = zeros(height,width,C); % pixel standard deviations u_diff = zeros(height,width,C); % difference of each pixel from mean
p = alpha/(1/C); % initial p variable (used to update mean and sd)
rank = zeros(1,C); % rank of components (w/sd)
S = strel('square', 3); % sturcture unit for mophology
% --------------------- initialize component means and weights ---------
pixel_depth = 8; % 8-bit resolution pixel_range = 2^pixel_depth -1; % pixel range
for i = 1 : height for j = 1 : width for k = 1 : C
mean(i,j,k) = rand * pixel_range; % means random (0-255)
w(i,j,k) = 1 / C; % weights uniformly dist
sd(i,j,k) = sd_init; % initialize to sd_init
end end end
% --------------------- process frames -----------------------------------
while(hasFrame(Source_Video))
fr = readFrame(Source_Video);
fr_bw = rgb2gray(fr); % convert frame to grayscale
fg = zeros(height, width); % temporary foreground for each frame
bg_bw = zeros(height, width); % temporary background for each frame
% 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(i,j,k)) <= D * sd(i,j,k)) % pixel matches component |fr_bw(i,j) - u(i,j,m)| <= 2.5\\sigma(i,j,m)
match = 1; % variable to signal component match
% update weights, mean, sd, p
w(i,j,k) = (1 - alpha) * w(i,j,k) + alpha; p = alpha / w(i,j,k);
mean(i,j,k) = (1 - p) * mean(i,j,k) + p * double(fr_bw(i,j));
sd(i,j,k) = sqrt((1 - p) * (sd(i,j,k)^2) + p * ((double(fr_bw(i,j)) - mean(i,j,k)))^2);
else % pixel doesn't match component
w(i,j,k) = (1 - alpha) * w(i,j,k); % weight slighly decreases
end
end
% normalize and update the weights in each step w_vect = squeeze(w(i,j,:));
w(i,j,:) = w(i,j,:) / sum(w_vect);
% calculate component rank
rank = w(i,j,:) ./ sd(i,j,:); % if no components match, create new component if (match == 0)
% replace the the distribution with lowest confidence [min_confid, min_confid_index] = min(rank); mean(i,j,min_confid_index) = double(fr_bw(i,j)); sd(i,j,min_confid_index) = sd_init; w(i,j,min_confid_index) = min(w(i,j,:));
% calculate moving object for foreground fg(i,j) = fr_bw(i,j);
end
% calculate mixtures of Gaussian for the background confid = w(i,j,:) ./ sd(i,j,:);
[con_sorted, w_index] = sort(confid, 'descend'); w_sum = 0; for k = 1 : C
w_sum = w_sum + w(i,j,k);
bg_bw(i,j) = bg_bw(i,j) + mean(i,j,w_index(k)) * w(i,j,k); if w_sum >= thres
break; end end
end end
% use morphology to file object fg = 255 * (fg > 0); fg = imdilate(fg, S);
% display frame, background and moving object figure(1);
subplot(3,1,1), imshow(fr);
subplot(3,1,2), imshow(uint8(bg_bw)); subplot(3,1,3), imshow(uint8(fg));
% write the background and moving object to videos writeVideo(Out_Back, uint8(bg_bw)); writeVideo(Out_Dect, uint8(fg)); end
% close video structure close(Out_Back); close(Out_Dect); …… 此处隐藏:1623字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [学前教育]MC9S12XS256RMV1 xs128芯片手册4
- [学前教育]安东尼语录经典语录
- [学前教育]e级gps控制测量技术设计书
- [学前教育]苏教版2022-2022学年八年级下学期期末
- [学前教育]装修公司推广 营销
- [学前教育]家政服务合同(完整版)
- [学前教育]湖北省2016届高三联考语文试题
- [学前教育]爱立信无涯学习系统LTE题库1-LTE基础知
- [学前教育]揭秘大众柴油车作弊软件原理
- [学前教育]人才流失原因及对策分析
- [学前教育]房屋建筑施工工程劳务分包合同
- [学前教育]国际贸易实务试卷A卷09.6
- [学前教育]校园废品回收活动计划方案书范文格
- [学前教育]电大成本会计试题及答案
- [学前教育]大学物理实验 华南理工出版社 绪论答案
- [学前教育]爱丁堡产后抑郁量表
- [学前教育]液压冲击的危害、产生原因与防止方法(
- [学前教育]学生工作总结高一学生期中考试总结_020
- [学前教育]人民医院医疗废物管理规章制度大全
- [学前教育]阳光维生素的巨大抗癌潜能阅读题答案.d
- 马云在云锋基金江苏论坛闭幕式的发言
- 试论小学体育教育中的心理健康教育-教
- 语文A版一年级下册《语文乐园一》教学
- 2021四川大学物理化学考研真题经验参考
- [人教A版]2015-2016学年高中数学 第二
- 终端网点销售返利协议书
- 江苏省2015年眼科学主治医师青光眼考试
- 2017年部编人教版八年级语文上册教案
- 十一中学七年级英语上册Unit7Howmuchar
- 以赛促教的创新性实验教学机制建设实践
- 平凉市崆峒区2015七年级下生物期末试题
- 琶洲(地块五)A、B塔楼1、2#塔吊基础
- 一级医院工作制度与人员岗位职责
- 2018北京西城区高三二模理科数学试题及
- 炒股密码线技术 - 图文
- 职高学生生涯发展辅导教案
- 语文人教版四年级上册8 世界地图引出的
- 最新最新人教版二年级上册全册数学教案
- 2017高考英语全国2卷精彩试题(有问题
- 普通心理学笔记




