Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Manufacturing Technology >> Manufacturing process

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

Step 1: Initial Setup

  1. Connect the Pi to keyboard, mouse, webcam, Ethernet or Wi‑Fi dongle, HDMI display, and power.
  2. Power on and boot into the Raspbian GUI.
  3. Open a terminal and install OpenCV. Follow Trevor Appleton’s comprehensive guide to confirm the installation (Install OpenCV on Raspberry Pi).
  4. Transfer bdtct.py to the Pi’s home directory via scp or a USB flash drive.

Step 2: Run the Tracking Code

  1. In the Pi terminal, navigate to the folder containing bdtct.py.
  2. Execute the script:
    sudo python bdtct.py
  3. Five windows should appear: the original video, a “tracking” view, and three HSV adjustment panels.
  4. Present a table tennis ball (yellow preferred) to the webcam.
  5. 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:

  1. Capture video from the default camera: cap = cv2.VideoCapture(-1)
  2. Resize frames to 320×240 to increase frame‑rate on the Pi.
  3. Create interactive windows with min/max sliders for Hue, Saturation, and Value.
  4. Convert each frame from BGR to HSV: hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV).
  5. Split HSV into separate channels.
  6. Threshold each channel based on slider ranges to produce binary masks.
  7. Combine the three masks with a logical AND to isolate ball pixels.
  8. Apply Gaussian blur to smooth the result.
  9. Use cv2.HoughCircles to detect circular shapes.
  10. Draw the detected circles back onto the original frame for visualization.

For deeper insight, see the full project documentation.

Manufacturing process

  1. Basketball: From Naismith’s 1891 Invention to Modern Production Techniques
  2. The Ultimate Guide to Bowling Ball Design and Performance
  3. The Evolution and Craftsmanship of American Football: From Ancient Games to Modern NFL Standards
  4. Golf: A Global Sport With Rich Heritage & Modern Appeal
  5. DIY Solar‑Powered Weather Station with Raspberry Pi
  6. Neon: A Dynamic Open/Closed Makerspace Sign Powered by Raspberry Pi 2 – El Paso, TX
  7. Line Tracking Sensor with Raspberry Pi – Simple KY‑033 Door/Line Detector
  8. Build a Professional Raspberry Pi Universal Remote with LIRC
  9. Real‑Time Pan/Tilt Face Tracking on a Raspberry Pi – Practical Guide
  10. Installing a Raspberry Pi Camera in a Birdhouse – Step‑by‑Step Guide