Header Image
Back to Portfolio

OpenCV Ball Tracker

Computer Vision with Raspberry Pi

The Problem

The objective was to create a ball-tracking algorithm that would a) detect the presence of specific-colored balls in the camera's field of view and b) pan and tilt the mechanism to keep the ball in the center of the frame.

Our Solution

First, we needed a way to detect the colored balls. We achieved this by converting each frame of the video input into the HSV color space. Through some testing, we were able to find the upper and lower bounds for the colors we wanted to detect.

Next, we wrote some code to find the X and Y coordinates for the center of the ball and trace a contour on the video feed. All of this was achieved using the OpenCV library in Python, loaded onto a Raspberry Pi 4.

DETECTION
Object Detection
TRACKING
Object Tracking

The detection code successfully detected a tennis ball in the video feed. Now, it was time to actuate the two servo motors that the camera was attached to.

For smooth tracking, we implemented PID control. By calculating the error between the center of the tracking object and the center of the camera frame, we could use PID control to tilt and pan the camera smoothly. The most time-consuming part was finding the appropriate KP, KI, and KD constants.

SETUP
Camera Setup

Here are some videos of the object detection in action:

Download our lab report.

Back to Portfolio