memo
Ex Member
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!