Tracking Multiple Objects Using a Webcam


Tracking Multiple Objects Using a Webcam from chris teso on Vimeo.

In continuing exploration into motion tracking using flash and a webcam I’ve created an application that enables you to track multiple objects based on color. The holy grail of the application is of course to track multiple objects without specifying color. However, this is a good step forward and actually opens up many other avenues for use. Color, after all, is ubiquitous.

The truly useful part of the code is getColorBoundsRect. If you’re unfamiliar with this function, it determines a rectangular region that either fully encloses all the pixels of a specified color within a BitmapData object, or fully encloses all pixels that do not include the color. The function returns a rectangle around the color area. You can then reference this rectangles x and y respectively.

var areaColor:Rectangle = SomeBitmapData.getColorBoundsRect( 0xFFFFFFFF, _colorArray[i].color, true );

The function is rather finicky about the depth of where the object is. Since it’s only searching for a certain RGB value it tends to lose it if the lighting or the object gets too small in the camera’s depth of field. To overcome this you could search for similar colors. Soulwire has written a sweet Color utility class that can aid you in this endeavor. However, searching for a range of colors will also come with it’s challenges, as the more range of color you search on the tougher it will be to pinpoint the exact object you want to track.

Check out the demo here: Tracking Multiple Objects Using a Webcam [webcam obv required]

Music: My Morning Jacket - I’m Amazed

Permalink: http://www.christeso.com/index.php/lab/tracking-multiple-objects-using-a-webcam/


6 Responses to “Tracking Multiple Objects Using a Webcam”

  • 1 Justin Says:

    Hi Chris,

    This looks really good mate, and if you can provide fairly distinctive colours it seems to respond really well. The demo looks sweet too :)

    I’m sorry to hear you’re having trouble with the performance of getColorBoundsRect; I’ve found it to be pretty fast, as with most of the native BitmapData methods. I guess if you’re checking for several colours in one frame it could bottleneck, perhaps a possible refinement would be to check one colour each frame, then use easing to interpolate the movement of each colour. So with 10 colours, colour 1 will get updated every 10th frame, but you could store a buffer of its past locations and on the 9 frames that it isn’t being checked just guess its future position, then corrected when it’s turn to update comes around. It would require a bit of vector math, but may be much faster as you’d be dealing with just a few mathematical operations rather than pixel analysis.

    Or even simpler, just straight up easing, something like:

    // Where updateMarker is a tag which cycles through all trackers, one per frame
    trackPoints[ updateMarker ].targetPos = new Point( colours[ updateMarker ].x, colours[ updateMarker ].y );
    // For all other points
    for (var i:int = 0; i < numTrackers; i++)
    {
    // Discard the one just updated
    if ( i == updateMarker ) continue;

    // Ease all others
    trackPoints[i].x += (trackPoints[i].targetPos.x - trackPoints[i].x) / numTrackers;
    trackPoints[i].y += (trackPoints[i].targetPos.y - trackPoints[i].y) / numTrackers;
    }

    So are you tracking the colour within areas where movement is already being detected? Or is this straight up colour detection on the raw camera image?

    Nice to see the research is coming along well ;)

  • 2 tebor Says:

    Justin,

    At the time I commented on your blog I was having performance issues with getColorBoundsRect, but not since. A lot changes in a couple hours :)

    That is a great idea for optimizing performance however.

    To answer your question I’m tracking raw cam image. I also have another version that just tracks color on the motion area.

  • 3 idan Says:

    Hello,

    I’m working on a school project that needs to track different color balls using a webcam and do some basic analysis on them, is there somewhere I can see the source for what you’ve created here?

    Thanks a lot.

    Idan

  • 4 Idan Says:

    Hi,

    I’m working on trying to create my own version of your movie, I was woundering what do you put in this array:

    _colorArray[i].

    how do you give flash a tollerance for colors?

    Thanks

    Idan

  • 5 Show and Tell Pdx | Chris Teso - Director of Interactive : Flash Designer Developer : Portland Photographer Says:

    [...] Without Interfaces” at Show and Tell Pdx. I showed off some Flash motion detection, Flash color detection and even demo’d a little voice speech recognition in Flash. Some of you asked for more info [...]

  • 6 Yehan Says:

    Hello,
    Great job !

    I’m a beginner and i try to do some tracking with webcam. I would know how can i detect one colour and use the _x and the _y to move a Movie Clip.

    I don’t know how get the _y and the _x from the GetColoursBoundsRect ?
    How do you do that thing on your example ?

    Thanks

    Yann

Leave a Reply

Twitter Users!
Enter your personal information in the form or sign in with your Twitter account by clicking the button below.