Copy link to clipboard
Copied
Fairly new coder here so apologies if there are glaring mistakes in my logic. I am having trouble getting the difference value of current and previous bitmapData frames in this motion detection script. I am using Soulwire's (blog.soulwire.co.uk/code/actionscript-3/webcam-motion-detection-tracking) code for the motion detection but am having problems implementing a little bit of code that I think should work for grabbing motion weight. The object is to read and differentiate between left and right swipes of the hand- movement from one side to the other yields a negative to positive or positive to negative number. Here's the track() function and the var _weight is what I'm looking for:
public function track() : void
{
_now.draw(_src, _mtx);
_now.draw(_old, null, null, BlendMode.DIFFERENCE);
_now.applyFilter(_now, _now.rect, new Point(), _col);
_now.applyFilter(_now, _now.rect, new Point(), _blr);
_now.threshold(_now, _now.rect, new Point(), '>', 0xFF333333, 0xFFFFFFFF);
_old.draw(_src, _mtx);
//simple weight gathering though prob need some time smoothing
var totalPix:int = _old.width * _old.height;
var index:int=totalPix+1;
var useIndex: int;
var xPix:int;
var yPix:int;
while (--index > 0){
useIndex = index-1;
xPix = useIndex % _old.width;
yPix = int(useIndex/_old.width);
if (Math.abs((_old.getPixel(xPix, yPix)>>16)-(_now.getPixel(xPix, yPix)>>16)) >30){
_weight += (((index/4) % _old.width) == 0 ? ((index-1)/4 % _old.width):((index/4) %_old.width)-(_old.width/2));
}
index -= 4;
}
For some reason it's giving me a 0 value for (_now.getPixel(xPix,yPix)>>16). I am wondering if this is because _old is drawn over _now? Thanks for any help.
Copy link to clipboard
Copied
where is getPixel, _now and _old being defined? You could always trace the values you're getting from your variables , and verify that everything is being passed as it should.
Copy link to clipboard
Copied
Thanks for the response. Yeah I have been tracing away. One thing I found out is that if when I set weight to _old.getPixel I receive normal values. When I pass _now.getPixel, I just get 0's except when theres is movement at 0,0, the very top right corner. Thats curious. Im going to try rearranging the order I'm drawing and see if something changes.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now