Introduction: Raspberry Pi Ball Tracking

(Note: This is an advanced tutorial, it is not intended for a linux beginner.)

In this tutorial, I will demonstrate how to track table tennis balls using OpenCV on Raspberry Pi. It can be used to track any circular object as long as it can be detected well from its background. This can be helpful in ball tracking robots and similar projects.

Things you need:

1. Raspberry Pi (with latest Raspbian installed, you can get it from here: Follow installation instructions on the same site)

2. USB Webcam (test one that Rasppi supports)

3. A linux host (preferably Linux Mint/Ububtu ) I have tested with Linux Mint 17.

4. Experience with debian systems

Step 1: Initial Setup


1. Make Rpi connections: Keyboard, Mouse, Webcam, ethernet or wifi dongle, Display via HDMI, Power

2. Power on your Rpi

3. Boot into raspbian GUI on your Rpi (Raspbian boot to GUI)

4. Open terminal and install OpenCV (Follow Trevor Appleton's excellent guide to verify that OpenCV is installed correctly Install OpenCV on Rpi)

5. Copy the file "" to home folder in your rpi either by using scp or by using a flash drive.


Step 2: Run the Code (a.k.a the Fun Part)

1. In your rpi's terminal navigate to folder where you copied

2. Run the following command:

sudo python

That should open 5 windows like in above picture

3. Bring a table tennis ball(use a yellow one if possible) in front of your webcam.

5. The ball should be tracked in the window "tracking". If not, adjust the sliders in the windows "HueComp", "SatComp", "ValComp" respectively such that only the region of the table tennis ball appears white in the "closing" window (See the above picture for reference). You may need to experiment a little to get this working. Note down the values of the sliders for which it works for you, you can later edit them in .

Step 3: Understanding the Algorithm

Open the file in text editor.

The code is doing following:

1. Taking in the input by "cap = cv2.VideoCapture(-1)"

2. Re-sizing the video frame to smaller size of 320x240, so that our rpi can put out more frames per second.

3. Creating visual elements like windows "HueComp", "SatComp", "ValComp" with respective min and max sliders.

4. Converting the input from BGR to HSV system "hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)"

5. Splitting the hue, saturation and value components.

6. Thresholding each component according to threshold range defined by respective min and max sliders to obtain a binary thresholded image (see above picture)

7. Logically AND the thresholded hue, saturarion, value components together to get a rough binary image in which only the table tennis ball's pixels are white, rest everything is black. (see above picture)

8. Smoothing the ANDed image "closing = cv2.GaussianBlur(closing,(5,5),0)"

9. Using HoughCircles to detect circles in the image

10. Drawing detected circles on the original input frame.

Step 4: Conclusion

If you are on this page that means everything worked well. Now get yourself a cup of coffee. :D

Pi/e Day Contest

Participated in the
Pi/e Day Contest