Motion Detection In Flash (AS2 and AS3) and C#

Currently working on some motion detection with flash/c# and webcams right now. Here’s a basic overview of some motion detection source files and tricks. Most motion detection is based on snapshots and finding brightness of a pixel with all combined colors, then comparing that to previous snapshots to detect enough variance and thus movement. If you have a webcam hooked up, this sample in Flash AS3 highlights this well showing the camera on the left, then the brightness snapshots on the right. It also has an indicator to the amount of movement due to much brightness.


Here is a nice example of motion detection using various motion detection algorithms in C#. This is built on the very slick AForge.NET Computer Imaging Library.

If you ever wanted your own motion detection or recording it is all possible with the basics of checking brightness and snapshots in the most simple form checking how much change or variance their was to bright pixels or the count of bright pixels compared to previous snapshots.

// Calculate white pixels

private int CalculateWhitePixels( Bitmap image )
    int count = 0;
    // lock difference image
    BitmapData data = image.LockBits( new Rectangle( 0, 0, width, height ),
        ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed );
    int offset = data.Stride - width;
        byte * ptr = (byte *) data.Scan0.ToPointer( );
        for ( int y = 0; y < height; y++ )
            for ( int x = 0; x < width; x++, ptr++ )
                count += ( (*ptr) >> 7 );
            ptr += offset;

    // unlock image
    image.UnlockBits( data );
    return count;

Flash AS2

In Flash this is also possible here is a good article from Flash8 that explains cycling through each pixel to compare the image data and implement motion detection from a webcam.

For instance the basics here show how you can compare each pixel and the change in the brightness for each pixel:


//color of the current pixel in the current snapshot

//red channel

//green channel

//blue channel

nl=Math.sqrt(nr*nr + ng*ng + nb*nb)

//color of the same pixel in the previous snapshot

//red channel

//green channel

//blue channel

bl=Math.sqrt(br*br + bg*bg + bb*bb);

//difference in brightness between now and before

//there was a change in this pixel

Flash AS3

Here is a link to grab a conversion of the AS2 Flash motion detection above to AS3.

Source of AS3 motion detection here.

Grant Skinner has done some interesting things with motion detection with Flash and webcams in the past in the incomplet gallery.

C# or other hardware accelerated capable kits are faster but AS3 and Flash with the new AVM2 virtual machineĀ  should be about 10 times faster than AS2 as much of the improvement in performance and the virtual machine is on iteration speed increases such as loops (i.e. pixel loop).

About these ads

9 Responses to “Motion Detection In Flash (AS2 and AS3) and C#”

  1. Gaspy Says:

    Nice post.
    Motion detection is relatively easy (made some experiments myself).
    What I couldn’t do reliably in Flash is image recognition and motion tracking.
    Think about the cool things one could do with a webcam if you could do decent face recognition in Actionsctript. I a pretty nice demo of using hand gestures captured by a webcam to control Google Earth, but I think it was a controlled environment (black background, etc.)

  2. drawk Says:

    Hey Gaspy,

    Yeh many of these tests are in AS2 as well so a bit slow to respond. mr doob has some really great webcam interactions that look good and are fast ( I agree facial recognition would be absolutely sweet, you might be able to do this with a server involved and process the images on the back end… .. thinking…

  3. Motion Detection in Flash and C# at flash und so… Says:

    [...] interessant ist das Motion Detection-Experiment mit von drawk, welches auf dieser Idee basiert. [...]

  4. Top Posts « Says:

    [...] Motion Detection In Flash (AS2 and AS3) and C# Currently working on some motion detection with flash/c# and webcams right now. Here’s a basic overview of some […] [...]

  5. Dan Zen Says:

    I have some classes in AS2 and AS3 that make it easy to capture motion –

  6. drawk Says:

    That was pretty sweet Dan good work.

  7. Research links « Masters Group Project 2008 Says:

    [...] Motion Detection In Flash (AS2 and AS3) and C# Ā« [ draw.logic ] [...]

  8. Symmetri Developer Blog :: Webcam fun with Flash :: March :: 2008 Says:

    [...] present data back to the Flash client that can then be used for UI interaction or something similar. draw.logic blog has a decent little rundown of webcam capture and simple motion detection using Actionscript, and [...]

  9. Justin Windle Says:

    Thanks for the post.

    I’ve put together a class which helps you not only detect but track webcam motion in AS3:

    It’s still work in progress, and may need to be adjusted for certain environments.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: