We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
background subtraction problem (Read 919 times)
background subtraction problem
Jun 25th, 2008, 1:47am
 
Hi,

I'm doing my first processing project where I have to capture 1 frame (which is the background which acts as the reference frame) and a 2nd frame (which is the same background with but with a face on top of it, in 1 layer.)

I want to track down the difference between those 2 images (which is basically only the face) and print that face on a black background. I looked into the background substraction example but all I got is an 'haunted' see-through image where the colors are messed up.

Probably there is already an example written like that but I'm unable to find it.

If any of you guys have an idea how to do it, I'm happy to hear!
Re: background subtraction problem
Reply #1 - Jul 4th, 2008, 12:24pm
 
Hi The Background Subtraction example in processing does almost what you want. The problem - as you probably realized - is that the areas which have changed since you took a snapshot of the background will not appear normal, but weird inverted and subtracted with the pixels behind it.

What you need to do is:
1. convert the background image and current image to greyscale - just take average of R, G, B components (technically 0.30R + 0.59G + 0.11B is more accurate RGB->greyscale intensity, but doesn't matter too much).

2. do the background subtraction- Areas that have not changed will be black, areas that have changed will not be black (theyll be weird inverted and subtracted etc,)

3. on the resulting image do a little blur to remove noise if you have any - you probably will (you can find one here http://incubator.quasimondo.com/processing/superfast_blur.php)

4. on the resulting image do a threshold (i.e. if pixel is less than 50, make it black, if pixel is greater than of equal to 50 make it white).

5. do another tiny blur on the resulting image to soften the edges if you want

6. apply the resulting image as a matte on your original image (if the matte is black, don't show the original image, if the matte is white, show the original image).

and thats it! You can use this technique to overlay your foreground image (e.g. face) over another background - its called difference matting.

You will need to play around with blur amounts depending on the noise you have, and also the threshold amount for step 4. You could also use a soft-threshold instead, e.g. if pixel is less than 50 make it black, if its greater than 80 make it white, if its inbetween 50-80 make it grey, with the intensity depending on where it is between 50-80 etc.

You can do most of the steps mentioned above all in one pass (except for the blurs of course)...

hope that helps!
Re: background subtraction problem
Reply #2 - Jul 4th, 2008, 2:44pm
 
Thanks memo, I've been wondering this same thing myself Smiley
Page Index Toggle Pages: 1