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: https://www.raspberrypi.org/downloads/ 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 "bdtct.py" 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 bdtct.py

2. Run the following command:

sudo python bdctc.py

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 bdtct.py .

Step 3: Understanding the Algorithm

Open the bdtct.py file in text editor.

The bdtct.py 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

2 People Made This Project!


  • Stone Concrete and Cement Contest

    Stone Concrete and Cement Contest
  • DIY Summer Camp Contest

    DIY Summer Camp Contest
  • Classroom Science Contest

    Classroom Science Contest

10 Discussions


Question 1 year ago

How can I make this work using picamera? Thank you!


Question 1 year ago on Step 3

Traceback (most recent call last):
File "/home/pi/program/obj.py", line 1, in
import cv2
ImportError: No module named 'cv2'


Question 1 year ago

hello there.
your project is realy awesome. but i ran into a problem when i start the bdctc.p file
in the terminal i type:
sudo python bdctc.py

after a fex seconds i get this error back:
OpenCV Error: Assertion failed ((scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F)) in cvtColor, file /home/pi/opencv-, line 3999
Traceback (most recent call last):
File "bdctc.py", line 50, in
hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
cv2.error: /home/pi/opencv- error: (-215) (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) in function cvtColor

when i run the program in pyhton2.7 the hue comp opens but the window is empty.
can you help me plzz. thank you

Traceback (most recent call last):

File "C:\Users\Apisit\Desktop\python\bdtct.py", line 2, in <module>

import cv2.cv as cv

ImportError: No module named cv

2 replies

If you didnt get error for import cv2 but got an error for importing cv2.cv, it means that you have installed opencv3 and not opencv2. There s not a big difference between the two and only needs modification at 2 places


2 years ago

hello man, thanks for sharing knowledge.

how many frames per seconds could it take, i.e. what is the maximum speed of motion that could be captured.


2 years ago

thank you very much
i want a tutorial steps please
can you help me ?


3 years ago

what did you use in to simulate it


4 years ago

Wow. Thank you for the instructions , it really helped me in completing me final year Engineering project . :)