matlab实现混合高斯模型运动物体检测算法
更新时间:2024-01-04 17:16:01 阅读量: 教育文库 文档下载
- 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);
正在阅读:
适合感恩节发的祝福语 感恩大家祝福的语句09-12
中华人民共和国赠与法11-06
2008-09-1管理学试卷及答案09-22
暑期学校首期教学工作总结07-09
2019最新PEP人教版小学六年级英语下册:A-Lets try03-15
学校放假通知03-31
德尔格在线产品手册04-12
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 高斯
- 物体
- 算法
- 混合
- 模型
- 检测
- 实现
- 运动
- matlab
- 高分子化学答案(熊联明版) - 图文
- 山西省煤炭管理条例
- 情商最高的三大星座,不服不行!
- 五年级上学期数学思维体操期末复习题(10秋)
- 工商管理(本科)现代管理专题论文
- 溪坦学校廉洁从教目标责任书
- 2014年房产经纪人之七问七答每日一讲(10月20日)
- 浙江省丽水市2015年中考地理试题(WORD版,有答案) - 图文
- 阅读与口语表达课题
- 环山公路绿化景观改造提升项目二期工程施工组织设计
- 谈现代化建筑工程施工管理及创新
- 物理化学实验思考题及答案
- 用典型归类复习排列组合中的重复问题
- 线性代数14-15期末练习题(1)
- 2007-2008学年第一学期传热学B及答案
- 解剖学试题-消化系统考题及答案
- 2018年张家界市中考物理全真模拟试题6详细答案
- 二年级上册《识字3》教学设计
- 水泥砖项目可行性研究报告(发改立项备案+2013年最新案例范文)详细编制方案
- 威海市文登区生活垃圾焚烧发电投资建设项目可行性研究报告-广州中撰咨询