Tracking Multiple Objects Using a Webcam

October 16th 2008 in Laboratory, Tracking Multiple Objects Using a Webcam, Webcam Motion Detection


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/

Share and Enjoy:
  • Twitter
  • Facebook
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Print

9 comments to...
“Tracking Multiple Objects Using a Webcam”
Avatar
Justin

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 ;)


Avatar
tebor

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.


Avatar
idan

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


Avatar
Idan

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


[...] 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 [...]


Avatar
Yehan

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


Avatar
Stephan Tanguay

Great work and really helpful tip regarding getColorBoundsRec()
I will definitely use that for some interesting web camera fun.
Its just what I was looking for, Thanks!

Stephan Tanguay’s last blog post..Vote For My First Webby Award!!!


Avatar
desperation to pee

ehh.. love it..


Avatar
Research – Tracking Colour In AS3!

[...] such method is shown above and can be found on Chris Tesos’ blog. This works by capturing the camera data to a BitmapData class. When you click it defines the [...]




required



required - won't be displayed


Your Comment:

CommentLuv Enabled

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

Equilibrium, the Human Mashup

This Friday -[Oct. 17th]- I will be part of SAO and the Portland Working Artists Network in presenting at Equilibrium, the Human Mashup, using the synergy of art and technology to revolutionize society. I am currently designing and developing an interactive art installation. A brief description of the project is below. I’m super excited to [...]

Share and Enjoy:
  • Twitter
  • Facebook
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Print
Equilibrium, the Human MashupPrevious Entry
Equilibrium The Human Mashup: A Follow UpNext Entry