资源简介

混合高斯背景建模,可以把连续帧图片前景与背景区分开来

资源截图

代码片段和文件信息

clc;
clear all;
I=imread(‘0000.jpg‘);                                                                       % 读入第一帧作为背景帧
fr_bw = rgb2gray(I);     
[heightwidth] = size(fr_bw);                                                               %求每帧图像大小
C=3;                                                                                        % 单高斯模型的个数(通常为3-5)
u0 = zeros(heightwidthC);
for i= 1:C-1
    u0(::i) = double(I(::i));%255*double(I(::i))./double(sum(I3)+(1e-10));
end
u0(::C)=double(fr_bw); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%设置初始参数%%%%%%%%%%%%%%%%%%%%%%
M = 3;                                                                                      % 代表背景的模型个数
T = 5;
D = 2.5;                                                                                    % 偏差阈值
alpha = 0.001;                                                                              % 学习率
thresh = 0.5;                                                                               % 前景阈值
sd0 = 15;                                                                                   % 初始化标准差
w = ones(heightwidthC)/C;                                                                 % 初始化权重矩阵
sd = sd0*ones(heightwidthC);                                                              % 像素标准差
p = alpha./w;                                                                               % 初始化p变量,用来更新均值和标准差
rank = zeros(1C);                                                                          %各个高斯分布的优先级(w/sd)
frame_num=200;                                                                              %帧数
for n = 1:frame_num
    if n<10
        a=‘000‘;
    end
    if n<100&&n>=10
        a=‘00‘;
    end
    if 100<=n
        a=‘0‘;
    end

    frame=strcat(anum2str(n)‘.jpg‘);
    I1=imread(frame);                                                                       % 依次读入各帧图像
    fr_bw0 =rgb2gray(I1);
%     fr_bw0=medfilt2(fr_bw0[3 3]);
    h=fspecial(‘average‘);
    fr_bw0=imfilter(fr_bw0h);
    for i= 1:C-1
        fr_bw(::i) = double(I1(::i));%255*double()./double(sum(I13)+(1e-10));
    end
    fr_bw(::C)=double(fr_bw0);
    for i= C+1:M
        fr_bw(::i) = double(fr_bw0);
    end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 计算新像素与高斯模型均值的绝对距离%%%%%%%%%%%%%
    u_diff = abs(double(fr_bw) - double(u0));
     
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 更新高斯模型的参数%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    match0=abs(u_diff)<=max(D*sdT);                                                        %像素与高斯模型匹配则标注为1,否则为0
    match=max(match0[]3);                                                                 %判断是否有高斯分布与图像匹配
    p=alpha./w;                                                                             %更新p
    wmatch1=(1-alpha)*w.*match0 + alpha.*match0;                                            %若更新匹配部分的w
    wmatch0 = (1-alpha)*w.*(1-match0);                                                      %若更新不匹配部分的w
 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        5072  2012-01-08 05:23  GMM.m

评论

共有 条评论