"Michael Davis" <slithy@yahoo.com> wrote in message <hp7qns$gj4$1@fred.mathworks.com>...
> I'm using Online Gaussian for doing background extraction on a video stream (code fragment below). I have 2 questions about this:
>
> 1. I have a 3D matrix for the video frame (rows x cols x 3). I need to do a multipication on each pixel triplet in the matrix (r,g,b) [ probably better to change this to (y,u,v) but principle is the same ] to get the Mahalanobis distance at that pixel. Currently I'm looping across every pixel to do the calculation. Is that the most efficient approach?
>
> 2. The Mahalanobis distance calculation uses the inverse of the covariance matrix for the pixel ... but if the pixel value doesn't change, the covariance matrix is zero and so doesn't have an inverse. Does anyone know what is the correct thing to do in this case? I was thinking of just using the Euclidean distance for those pixels but I'm not sure if that's correct.
>
> Thanks!
>
> % Read current frame
> F = aviread(Filename, f);
>
> % do BG extraction
> % This calculates the square of the Mahalanobis Distance, so the threshold should be
> % set as no. of variances rather than no. of standard deviations:
>
> D = F  BGM;
>
> % Calculate Mahalanobis distance for each point separately
> for row = 1:size(F,1)
> for col = 1:size(F,2)
> v = BGV(row,col,:)(:);
> v = inv(diag(v)); % doesn't work if v is zero!
> d = D(row,col,:)(:);
> MahalanobisDist(row,col,:) = d' * v * d;
> end
> end
>
> G = F .* (MahalanobisDist > Threshold);
>
> % Write the frame
> addframe(AVI,G);
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.
If c is the full 3 x 3 covariance matrix, you can avoid finding its full inverse by writing the equivalent d'/c*d instead of d'*inv(c)*d.
For a pixel that has remained strictly constant in each color in the past with consequent zero covariances, calculating mahalanobis distance becomes meaningless. Any slight change would then compute as an infinite distance. That is inherent in the very definition of that concept. Your answer either has to be 'inf' or you must modify to some extent your definition of the distance you are computing. It is analogous to asking for the percentage increase in price of an item that was originally free.
As to the efficiency of your pixelbypixel computation, I suspect your nested forloops are about as efficient as you are going to get with matlab. 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. Because of the complicated nature of the mahalanobis computation, there would probably be little gained by trying vectorize such an "online" computation.
Roger Stafford
