Track balls by color and shape with camera and OpenCV

Hello. I have an exam that consists on detecting an object with a camera. I explain it a little bit:
With the mouse you have to select the object and then the programme have to make the tracking of that object. The code have to take into account colors and shapes. I’m working with OpenCV.
I’m new to processing and to all these types of programmes and I don’t know how this works. If anyone could help me I would be very happy. Thank you.

1 Like

That page has a bunch of demos and a ton of info on it.

What object are you trying to track? A certain playing card? A black and white pattern? Something in a scene that isn’t there usually (IE background subtraction)? A face? A thing of a certain hue? What?

1 Like

Thank you for answering fast. I have to track 3 objects (not at the same time, but the code has to detect all of them). The first 2 of them are balls, one is bigger than the other, and these balls are of different color. One is orange, and the other is green. I think that the code to detect them will be the same, but with changes in the size and color. The other object is a blue marker. The problem is that I don’t know how to detect an object’s shape and color and keep the track of it. Thank you for the link, I’ll check it right now.

I believe there is a OpenCV library available through the contribution manager in the PDE. You can also try the blob library and maybe the BoofCV library. The first two things I will do is to look at the available online docs and the provided examples.

You could make this more interesting by implementing all by yourself. The trick is this:

  • You have access to every pixel in the screen
  • You can store every coordinate of all pixels of certain color
  • You calculate the center of the blob of pixels
  • From the center, you can find min/max along the x and y to define the boundaries
  • You do this operation for each frame
  • Don’t forget to keep track of the centers, radius (in case of balls) an their colors

Kf

1 Like

If you want to get into the details of simple blob tracking a bit more without using OpenCV, you can also watch this Blob Detection tutorial

which develops this Blob Tracking example sketch:

In addition, there is a second library for Processing that is commonly used to track objects: BoofCV. It also has object tracking examples:

2 Likes