Advanced Raspberry Pi Table Tennis Ball Tracking with OpenCV
NOTE: This is an advanced tutorial, not suitable for Linux beginners.
In this guide we’ll show how to detect and track table tennis balls—or any clearly visible circular object—using OpenCV on a Raspberry Pi. The technique is valuable for robotics projects, sports analytics, or hobbyist experimentation.
Prerequisites
- Raspberry Pi running the latest Raspbian (download at https://www.raspberrypi.org/downloads/)
- USB webcam compatible with the Pi
- Linux host (Linux Mint 17, Ubuntu, or similar) for development and transfer of files
- Familiarity with Debian‑based systems
Step 1: Initial Setup
- Connect the Pi to keyboard, mouse, webcam, Ethernet or Wi‑Fi dongle, HDMI display, and power.
- Power on and boot into the Raspbian GUI.
- Open a terminal and install OpenCV. Follow Trevor Appleton’s comprehensive guide to confirm the installation (Install OpenCV on Raspberry Pi).
- Transfer
bdtct.pyto the Pi’s home directory viascpor a USB flash drive.
Step 2: Run the Tracking Code
- In the Pi terminal, navigate to the folder containing
bdtct.py. - Execute the script:
sudo python bdtct.py - Five windows should appear: the original video, a “tracking” view, and three HSV adjustment panels.
- Present a table tennis ball (yellow preferred) to the webcam.
- Adjust the “HueComp”, “SatComp”, and “ValComp” sliders until the ball’s pixels are the sole white region in the “closing” window. Record these slider values for future use by editing
bdtct.py.
Step 3: Understanding the Algorithm
Open bdtct.py in your favorite editor to see the following workflow:
- Capture video from the default camera:
cap = cv2.VideoCapture(-1) - Resize frames to 320×240 to increase frame‑rate on the Pi.
- Create interactive windows with min/max sliders for Hue, Saturation, and Value.
- Convert each frame from BGR to HSV:
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV). - Split HSV into separate channels.
- Threshold each channel based on slider ranges to produce binary masks.
- Combine the three masks with a logical AND to isolate ball pixels.
- Apply Gaussian blur to smooth the result.
- Use
cv2.HoughCirclesto detect circular shapes. - Draw the detected circles back onto the original frame for visualization.
For deeper insight, see the full project documentation.
Manufacturing process
- Basketball: From Naismith’s 1891 Invention to Modern Production Techniques
- The Ultimate Guide to Bowling Ball Design and Performance
- The Evolution and Craftsmanship of American Football: From Ancient Games to Modern NFL Standards
- Golf: A Global Sport With Rich Heritage & Modern Appeal
- DIY Solar‑Powered Weather Station with Raspberry Pi
- Neon: A Dynamic Open/Closed Makerspace Sign Powered by Raspberry Pi 2 – El Paso, TX
- Line Tracking Sensor with Raspberry Pi – Simple KY‑033 Door/Line Detector
- Build a Professional Raspberry Pi Universal Remote with LIRC
- Real‑Time Pan/Tilt Face Tracking on a Raspberry Pi – Practical Guide
- Installing a Raspberry Pi Camera in a Birdhouse – Step‑by‑Step Guide