www.pudn.com > GaussBackground.rar > GaussBackground.m
function [CDM,Tao,Hist] = GaussBackground(GaussImage)
% 对图像序列进行高斯背景分析,去除背景和噪声,
% 参见文章“Automatic Temporal Segmentation for Content-Based Video Coding”。
% Arguments:
% CDM:去除背景图像序列的变化检测模板
% 执行示范:[CDM,Tao,Hist] = GaussBackground(GaussImage);
% 计算直方图
% Hist = Histgram(GaussImage);
Hist = HistgramInt(GaussImage);
% 对于每一个T,计算适应准则MAEt,找到最小MAEt对应的T,作为背景检测阈值
cols = size(Hist,2);
[MinMAEt,pB] = MAE(Hist,abs(Hist(1,257)));
Tao = Hist(1,257);
for i=258:cols
[MAEt,pB] = MAE(Hist,abs(Hist(1,i)));
if ((MAEt < MinMAEt) && (pB>0))
MinMAEt = MAEt;
Tao = Hist(1,i);
end
end
% 生成变化检测模板CDM
rows = size(GaussImage,1);
cols = size(GaussImage,2);
CDM = zeros(rows,cols);
for i = 1:rows
for j = 1:cols
if (abs(GaussImage(i,j)) > Tao)
CDM(i,j)=1;
end
end
end
% figure;imshow(GaussImage);
% figure;imshow(CDM);
%%
function [MAEt,pB] = MAE(Hist,T)
% 内部函数,适应准则MAEt。
% 计算p(B)
pB = 0;
cols = size(Hist,2);
for i = 1:cols
if (abs(Hist(1,i)) <= T)
pB = pB + Hist(2,i);
end
end
MAEt = 0;
if (pB > 0)
%计算theta(B)
TmpthetaB = 0;
for i = 1:cols
if (abs(Hist(1,i)) <= T)
TmpthetaB = TmpthetaB + power(Hist(1,i),2) * Hist(2,i);
end
end
thetaB2 = TmpthetaB / pB;
%计算适应准则MAE(T),其实SumLevel可以不考虑,因为对所有的T都一样。
for i = 1:cols
%计算p(dB)
pdB = 1 / power(2 * pi * thetaB2,0.5) * exp(-1 * 0.5 * power(Hist(1,i),2) / thetaB2);
% MAEt = MAEt + Hist(2,i)*abs(pB * pdB - Hist(2,i));
MAEt = MAEt + abs(pB * pdB - Hist(2,i));
end
end
%%
function [Hist] = HistgramInt(DifImage)
% 内部函数,返回图像的整数直方图。
cols = size(DifImage,1)*size(DifImage,2);
DifImage = reshape(round(DifImage),1,cols);
MaxV = max(DifImage);
MinV = min(DifImage);
Hist = zeros(2,511);
for i = 1:511
Hist(1,i) = i-256;
end
for i = 1:cols
CurrV = round(double(DifImage(1,i)) + double(256));
Hist(2,CurrV) = Hist(2,CurrV) +1;
end
%%
function [Hist] = Histgram(DifImage)
% 内部函数,返回图像的实数直方图。
cols = size(DifImage,1)*size(DifImage,2);
DifImage = reshape(DifImage,1,cols);
MaxV = max(DifImage); % 在屏幕上只能看到小数点后4位,简便起见
MinV = min(DifImage);
StepV = double(MaxV - MinV) / 510;
Hist = zeros(2,511);
for i = 1:511
Hist(1,i) = roundn((i-1) * StepV + MinV,-4);
end
for i = 1:cols
CurrV = ceil(double(DifImage(1,i) - MinV)/StepV);
Hist(2,CurrV+1) = Hist(2,CurrV+1) +1; %CurrV+1 是为了防止出现CurrV=0的情况
end