From: Kevin on
"Kevin " <kanwalrehsi(a)gmail.com> wrote in message <hnm2ep$e63$1(a)fred.mathworks.com>...
> ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <e3333450-bb73-46d5-9bd9-1f7fa5ee03a7(a)z35g2000yqd.googlegroups.com>...
> > Well one can't know everything, and this is one of those things I
> > don't know about. I've never had occasion to use them so I can't
> > really help you. Good luck though!
>
> No Probs ImageAnalyst.
> Thanks Anyway!

Anybody with suggestions

Thanks
From: Kevin on
Hello probably somebody may be able to help now?

Basically got this algorithm of 'Foreground Subtraction' off a website and I want to perform 'Background Subtraction' instead.

Could somebody please even advice me on how to go about this? Btw it uses gaussian bckground model

function foreground = extractForeground(video, significanceThreshold, frameSkip, returnLargestComponent)

% Apply default values if required
if (nargin < 2)|isempty(significanceThreshold), significanceThreshold = 14; end;
if (nargin < 3)|isempty(frameSkip), frameSkip = 4; end;
if (nargin < 4)|isempty(returnLargestComponent), returnLargestComponent = 0; end;
disp('finish reading...');
clear frames
%---------------------------------
if ischar(video)
% Load the video from an avi file.
avi = aviread(video);
pixels = double(cat(4,avi(1:frameSkip:end).cdata))/255;
clear avi
else
% Compile the pixel data into a single array
pixels = double(cat(4,video{1:frameSkip:end}))/255;
end;
% NOTE: The result of the cat operation above may be very large.
% If it will not fit into memory, try decreasing frameSelect.
% Another alternative is to decrease the video resolution.
disp('finish cat...');
%---------------------------------
% Convert to HSV color space:
nFrames = size(pixels,4);
for f = 1:nFrames
pixels(:,:,:,f) = rgb2hsv(pixels(:,:,:,f));
end;
% Generate Gaussian background model in hsv space for each pixel.
% use a smaller data range to ensure faster calculation
dataRange = [.45 .55];
[backgroundMean, backgroundDeviation] = hsvGaussModel(pixels,dataRange);
disp('finish Gaussian...');

model = backgroundMean;
%---------------------------------
% Do frame-by-frame differencing
foreground = cell(1,nFrames);

for f = 1:length(video)
% Find scaled deviation of this frame from background
deviation = sum(abs(rgb2hsv(double(video{f})/255) - backgroundMean), 3) * 85;

% Compare with threshold to generate labeling before applying morphological operations
label = double(imopen(imclose(deviation > significanceThreshold, strel('square',1)), strel('disk',2)));

% clean up: find largest connected component, etc.
if returnLargestComponent
label = fgRanked(label,1);
end;
foreground{f} = label;
clear temp
end;


%--------------------------------------------------------------------
% Fits a gaussian to the specified distribution, in a robust manner.
%
% Returns the mean and standard deviation of the input arguments
% using the specified portion of the data. Uses HSV color space
% for better shadow control.

function [mu,sigma] = hsvGaussModel(pixels,dataRange)

% statistics of s & v dimensions are simple.
[mu(:,:,2:3),sigma(:,:,2:3)] = fitGauss(pixels(:,:,2:3,:),dataRange,4);
hmu = polarMean(pixels(:,:,1,:)*2*pi,4);
h = zeros(size(pixels(:,:,1,:)));
nFrames = size(pixels,4);
for i = 1:nFrames
h(:,:,1,i) = mod(pixels(:,:,1,i)*2*pi-hmu+pi,2*pi);
end;
[mu(:,:,1),sigma(:,:,1)] = fitGauss(h,dataRange,4);
mu(:,:,1) = mod(mu(:,:,1)-pi+hmu,2*pi)/(2*pi);
sigma(:,:,1) = sigma(:,:,1)/(2*pi);
%--------------------------------------------------------------------
% Fits a gaussian to the specified distribution, in a robust manner.
%
% Returns the mean and standard deviation of the input arguments
% using the specified portion of the data

function [mu,sigma] = fitGauss(m,dataRange,dim)
x = -3:0.01:3;
y = exp(-x.^2);
y = y./sum(y);
yy = cumsum(y);
fdev = sqrt(sum(y.*x.^2));
xbd = min(find(yy > dataRange(1))):min(find(yy > dataRange(2)));
pdev = sqrt(sum(y(xbd).*x(xbd).^2)/sum(y(xbd)));

dimlen = size(m,dim);
if all(dataRange == [0 1])|(dim > length(size(m)))
% skip sorting step if we'll be using everything anyway
ms = m;
else
ms = sort(m,dim);
end;
dimind = round(dataRange*(dimlen-1)+1);
[refstr{1:length(size(m))}] = deal(':');
if (dim <= length(size(m)))
refstr{dim} = dimind;
end;
sref = struct('type','()','subs',{refstr});
bds = subsref(ms,sref);
if (dim <= length(size(m)))
sref.subs{dim} = dimind(1):dimind(2);
end;
data = subsref(ms,sref);
mu = mean(data,dim);
sigma = std(data,0,dim)*fdev/pdev;
%--------------------------------------------------------------------
% Returns the "mean" of a set of angular variables

function pm = polarMean(th,dim)
th = mod(th,2*pi);
x = cos(th);
y = sin(th);
if (nargin < 2)
pm = mod(atan2(mean(y),mean(x)),2*pi);
else
pm = mod(atan2(mean(y,dim),mean(x,dim)),2*pi);
end;
% end of polarMean
%--------------------------------------------------------------------
% Returns the foreground connected component in the binary image
% supplied that have the specified ranked size(s).

function fgr = fgRanked(bin,rank)
fg = bwlabel(bin,4);
maxfg = max(fg(:));
h = hist(fg(find(bin)),[1:maxfg]);
[sh,sr] = sort(-h);
if (rank < 1)|(rank > max(find(sh > 0)))
fgr = zeros(size(img))
else
fgr = (fg==sr(rank));
end;
%--------------------------------------------------------------------

Thanks
From: Thierry on
"Kevin " <kanwalrehsi(a)gmail.com> wrote in message <hnm0mr$e5l$1(a)fred.mathworks.com>...
> ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <9f5d6253-74bc-4def-a08c-8ff61c67bd2b(a)q21g2000yqm.googlegroups.com>...
> > Did you try a web search?
> > http://www.bing.com/search?q=GMM+background+subtraction&go=&form=QBLH&qs=n
> > I saw lots of good web sites with lots of good examples. If anyone
> > could help you I bet it would be one of the owners of those web sites.
>
> Yeh I did a google search and found a few good examples but they are not very clear. There are no documentation.
>
> Btw, what is the difference between mixture of gaussian models and Gaussian Mixture Model???
>
> I know how to start that analysing frame by frame but just not sure on how to apply gaussian mixture model?
>
> Please advice

Mixture of Gaussian models (MGM) and Gaussian Mixture Model (GMM) are differenta cronym for the same models. See at the following publication:

http://hal.archives-ouvertes.fr/docs/00/33/82/.../RPCS_2008.pdf

and fuzzy version of the GMM:

http://hal.archives-ouvertes.fr/hal-00336512_v1/

Best regards!
Thierry
From: Thierry on
See here for more explications on GMM:

http://hal.archives-ouvertes.fr/hal-00338206_v1/


Thierry