Showing posts with label approximate median. Show all posts
Showing posts with label approximate median. Show all posts

Monday, June 28, 2010

Mathematics - Mean & Median Background subtraction

In Mean Background subtraction or mean filter background subtraction the background is the mean of the previous n frames

( Σ i=0 to n-1 (It- i(x,y)) )/ n

In Median background subtraction the background is the median of the previous n frames

median{ I t-i (x,y) }


i ∈ { 0,1,2,3, ... n-1 }


The mathematics that i used in approximate median background subtraction was:

if It (x,y) > B t(x,y) ⇒ B t(x,y) + 1

if It (x,y) < B t(x,y) ⇒ B t(x,y) - 1



This method will eventually converge where half of the pixels are greater than the background and another half less than the background.

Results can be found here:

Result

Saturday, May 22, 2010

medium complexity background subtraction using approximate median



In this method the previous N frames of video are buffered, and the background is calculated as the median of buffered frames. The problem is the background is cleared of all objects after few frames to show a cleared background.

Median filtering has been shown to be very robust and to have performance comparable to higher complexity methods. However, storing and processing many frames of video (as is often required to track slower moving objects) requires an often prohibitively large amount of memory. This can be alleviated somewhat by storing and processing frames at a rate lower than the frame rate— thereby lowering storage and computation requirements at the expense of a slower adapting background.

The approximate median method works as such: if a pixel in the current frame has a value larger than the corresponding background pixel, the background pixel is incremented by 1. Likewise, if the current pixel is less than the background pixel, the background is decremented by one. In this way, the background eventually converges to an estimate where half the input pixels are greater than the background, and half are less than the background—approximately the median (convergence time will vary based on frame rate and amount movement in the scene.)




for i = 2:length(source)

fr = source(i).cdata;
fr_bw = rgb2gray(fr); % convert frame to grayscale

fr_diff = abs(double(fr_bw) - double(bg_bw)); % avoid negative overflow

for j=1:width
for k=1:height

if ((fr_diff(k,j) > thresh))
fg(k,j) = fr_bw(k,j);
else
fg(k,j) = 0;
end

if (fr_bw(k,j) > bg_bw(k,j))
bg_bw(k,j) = bg_bw(k,j) + 1;
elseif (fr_bw(k,j) <>
bg_bw(k,j) = bg_bw(k,j) - 1;
end

end
end

figure(1),subplot(3,1,1),imshow(fr)
subplot(3,1,2),imshow(uint8(bg_bw))
subplot(3,1,3),imshow(uint8(fg))
end