Detecting and Tracking a Red Ball From Live Video

I’m just getting started with OF. I think its really a pretty amazing framework. Thanks to everyone involved.

So anyway, on to my question. I am looking for a really nice way to track a red ball from the live video coming in from a web cam. Imagine you are standing in front of the camera holding a red ball in your hands moving it back and forth and up and down. I am looking for some ideas about how to best track the movements of the ball.

I’ve looked around the forums and looked through Google but just haven’t found anything exactly like this. Would anyone out there be so kind as to point me in the right direction?


You could probably just get away with a colour based detection method by the sounds of it.

Do you actually need ‘tracking’ or just ‘detection’? If there is only one ball and you probably don’t need to concern yourself with ‘tracking’ per say. A simple frame by frame detection process would probably suffice.

Also, it’s probably best to use HSV or YUV colourspaces as opposed to RGB since they have a dedicated ‘brightness’ channel which you can discard making it more robust to lighting changes.


Thanks for the response mhax. For right now it would only be one ball. But I could easily imagine wanting to follow more than one.

I guess I’m not certain of the difference in terms of “detection” vs. “tracking” but I can say that what I would like to be able to do is follow the position of the ball around such that I have something like a bounding rectangle and a center point of the ball at any give point. Also I would like it to really be accurate and track fast movements.

If I just use color detection, what would happen if there were other red objects in the camera’s field? Would this give me the accuracy I need? If so, how would I even start going about detecting that? (I’m really really new to this)

The end goal I am trying to achieve is to be able to use this ball as some type of marker that I would be replacing with random objects. Like one time the ball will switch to be a kitten and the user would see themselves holding a kitten.

Well detection is simply detecting a specific object in a given frame, whereas tracking implies detection but also maintaining some kind of state.

For example, you could have a face detection program analyse a video and it would put boxes round all the faces, frame by frame. But it couldn’t tell you about how the faces in frame n relate to those in frame n+1. There wouldn’t be any ‘temporal correspondences’. This is what tracking is; being able to say object A moved from here to there, while object B moved from there to there.

While tracking is clearly more complicated than just detection, depending on the requirements of your program, simple tracking techniques can be used at the expense of more advanced reasoning such as occlusion handling (one thing being obscured by another).

Yes, using just colour based tracking alone may cause false positive results if there are other objects of the same colour as the ball. There are plenty of other object detection methods, but there is always going to be a trade off in terms of complexity and robustness.

I would have a look at OpenCV which deals with computer vision and try and get hold of the book (published by O’Reilly) which you might be able to find floating round the net. There are some examples included in the opencv library that would probably get a good place to start.

Thank you for the clarification of terms. Makes sense.

For now I would like to just start with some simple object “detection” from a live video stream. I can try out the color detection and see how that works, but I’m a bit at a loss of where to start with that.

I took a look at the O’Reilly book. It looks like a great book, however its pretty advanced. I’m kind of hoping for a more simple example using OpenFrameworks to detect a single colored ball. I would guess that someone out there has done this, I just can’t seem to find it. Or maybe I am not sure what exactly to look for.

If anyone could point to some type of code for this, it would be very much appreciated.


Let me throw this out there. Is there anyway way to detect a blob based on a color? I think maybe that is what I am really looking to do. In this way I would be able to know where my blob is at any point in time and manipulate it as I wanted.

see my post here:

there is code on my setpixel class website.

hope it helps!

take care,

Thanks Zach! I’ll try and play around with it over the weekend. If I get something working I’ll share it here.

Dude. It works! When I get a demo of what I want to do with it, I’ll share it. Thanks so much for the help!