views:

693

answers:

3

Hi,

I am looking for a fairly simple image comparison method in AS3. I have taken an image from a web cam (with no subject) passed it in to bitmap data, then a second image is taken (this time with a subject) to compare this data, from these two images I would like to create a mask from the pixels that match on both bitmaps. I have been scratching my head for a while, and I am not really making any progress. Could any one point me in the right direction for pixel comparison method, something like getPixel32()

Cheers

Jono

+3  A: 

use compare to create a difference between the two and then use treshold to extract the parts that interest you.

edit: actually it is pretty straight forward. the trick is to apply the threshold multiple times per channel using the mask parameter (otherwise the comparison only makes little sense, since 0x010000 (which is almost black) is consider greater than 0x0000FF (which is anything but black)). here's how:

var dif:BitmapData;//your original bitmapdata
var mask:BitmapData = new BitmapData(dif.width, dif.height, true, 0);
const threshold:uint = 0x20;
for (var i:int = 0; i < 3; i++) 
    mask.threshold(dif, dif.rect, new Point(), ">", threshold << (i * 8), 0xFF000000, 0xFF << (i * 8));

this creates a transparent mask. then the threshold is applied for all three channels, setting the alpha channel to fully opaque where the channels value exceeds the threshold value (you might wanna decrease it).

you can isolate the foreground object ("the guy in front of the webcam") by copying the alpha channel from the mask to the current video image.

greetz

back2dos

back2dos
Using the compare() method returns some very weird results (lots of different colors), then the threshold does remove parts of the bitmap but seems to be quite erratic...could you be a legend and elaborate slightly on the threshold
Jono
@Jono: I would like to, but I don't fully understand what you wanna do. what do you mean by "create a mask"? the better you explain what you're up to, the likelier you are to get a satisfying answer ;)
back2dos
Sorry I can be a little vague. :) So you're infront of your computer (with a webcam) Move out of the webcams POV (take a snapshot) save as bitmapdata then bitmap. Then get back in front of the webcam's POV (take another) save as bitmapdata then bitmap. Use the compare/threshold to work out the difference between the bitmap data. This hopefully would create a mask to mask the live webcam feed, this will update every couple of frames to keep the processing levels down. So effectively its a live background replacement tool...does that help.let me know if you want to see my code or more detail:)
Jono
@Jone: ok. post updated.
back2dos
Nicely done mate, were cooking with gas now. Thanks a bunch!Now all I need to figure out is converting raw bitmap data to true black!
Jono
A: 

Hey,

Almost there, but I cannot grasp where the actual comparing takes place. Might I take a look at the code too?

Jos
For future reference: stackoverflow is not a forum. If you want an answer to be expanded (within the scope of the question), ask for it in a comment. To answer your question: the actual comparing takes place using the compare method. I linked the reference, which is actually quite selfexplanatory.
back2dos
A: 

one of the problems here is that you want to find if a pixel has ANY change to it, and if it does then to convert that pixel to another color (for masking). Unfortunately, a webcam's quality isn't great so even if your scene does not change at all the bitmapdata coming from the webcam will change slightly. Therefor, when your subject steps into frame...you will get pixel changes for the subject...but also noise in other areas due to lighting changes or camera quality. What you'll need to do is write a function that analyzes the result of a bitmapdaya.compare() for change in area's larger than _ to determine if there is enough change to warrant an actual object being there. That will help remove noise and make your mask more accurate.

simon