From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Mahalanabois Distance / Calculating Online Gaussian on a video stream
Date: Sat, 3 Apr 2010 22:48:21 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 55
Message-ID: <hp8gjl$okf$>
References: <hp7qns$gj4$> <hp88k5$ele$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1270334901 25231 (3 Apr 2010 22:48:21 GMT)
NNTP-Posting-Date: Sat, 3 Apr 2010 22:48:21 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2297837
Xref: comp.soft-sys.matlab:623217

Hi Roger,

Thanks for your comments, they were very helpful:

> If I understand what you are doing correctly, the computation at each pixel is entirely dependent on the past 
> history at that particular pixel only and unrelated to that of any other pixel.

That is correct. I was afraid that it wasn't going to be possible to run it more efficiently, as it takes quite a long time to run on my PC ...

>   I am puzzled by one aspect of your code, Michael.  When you write
>  v = inv(diag(v));
> you are ignoring any cross correlation that might be present between the three colors this way.
> You ought to be dealing with a complete 3 x 3 color covariance matrix if you are doing true mahalanobis 
> distance computation.

Thanks for pointing that out. I have written a new function to update the model, which now allows me to store the full 3x3 covariance matrix rather than just the variances for each colour (see below). So now I'll update the rest of the program to use the full covariance matrix as you suggest (and leave it to run overnight ...)


function [ M V ] = update_model(oldM, oldV, F, n)
% Parameters:
% oldM    - current model: means
% oldV    - current model: variances
% F       - new frame (N x M x 3)
% n       - no. of frames used to learn the Gaussian pdf
% Return values:
% M    - new model: means (N x M x 3)
% V    - new model: variances (N x M x 3 x 3)
% For each pixel in F (3 values), we calculate the mean and the 3x3 covariance
% matrix. These are used to update the old models and the updated models are
% returned as M and V

[ rows cols colour ] = size(F);

% alpha is the learning rate
alpha_t = 1 / n;
alpha_t1 = 1 - alpha_t;

% Update means
M = (alpha_t1 .* oldM) + (alpha_t .* F);

% Update variances
mF = F - M;
for r = 1:rows
   for c = 1:cols
      pixel = mF(r,c,:)(:);
      new_covar = pixel * pixel'; % 3 x 3 covariance matrix
      old_covar = reshape(oldV(r,c,:,:),[ 3 3 ]);
      V(r,c,:,:) = (alpha_t1 .* old_covar) + (alpha_t .* new_covar);