Motion detection using bitmaps and filters

After playing around with blob detection and somehow not getting it fast enough a neat little solution came up from Emil Korngold. Using the built in Adobe function ‘getColorBoundsRect’ for the BitmapData object is so much faster looking for ‘Blobs’. (Thanks! I had a look at that when starting and then forgot all about it. Sometimes the simple things are just too far away).
To get a usable result when detecting motion it is good to apply filters and thresholds. This reduces noise and errors when looking for the bounds of a given color.  This example is based on the ‘Focus’ demo files provided by Papervision3D.

The interesting part of the code:

private function loop(e:Event):void
{
    //bitmap containing difference between last & this frame
    newBmd.draw(input);
    newBmd.applyFilter(newBmd,bounds,topLeft,filter);
    newBmd.draw(oldBmd,null,null,BlendMode.DIFFERENCE);
    newBmd.threshold(newBmd,bounds,topLeft,"<",0xFFAAAAAA,0xFFFFFFFF); //remember bitmap for next time round
    oldBmd.draw(input);
    oldBmd.applyFilter(oldBmd,bounds,topLeft,filter);
    //now get bounds of a given color...
    var newBound:Rectangle = newBmd.getColorBoundsRect(m,c,true);
    //now use the newBound to move something...
}