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/


2 Responses to “Tracking Multiple Objects Using a Webcam”
October 17th, 2008 saat: 3:21 am
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
October 20th, 2008 saat: 4:35 pm
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.
Leave a Reply