All projects

Michigan Tech · Sensing and Processing in Robotics

Simple Object Tracking

Tracks two apples rolling away from the camera using color and shape filters, keeping each apple's identity as they meet and swap sides. No learned detector and no built-in OpenCV trackers.

The tracker's output at real speed. Each apple keeps its label and trail as they meet and change sides.

Problem

Two apples are thrown across a floor and roll away from the camera. The program has to find both in every frame and track them without ever mixing up which is which, even when they touch and cross paths. The scene works against it: a bookshelf in the background is full of red book spines, and the apples shrink to a fraction of their starting size as they roll away. The tracker had to be built from first principles, with no trained detector and none of OpenCV’s built-in trackers.

Approach

Each frame goes through three stages: find apple-colored pixels, keep only apple-shaped blobs, then match those blobs to the two tracks.

  • A Gaussian blur smooths out speckle, then the frame is converted from RGB to HSV. Hue and saturation describe color separately from brightness, so shadows on the apples don’t change their measured color. Pixels inside the tuned hue, saturation, and value ranges become a black-and-white mask of apple-colored pixels.
  • An open removes leftover specks, a close fills the small holes left by shiny highlights on the apples, and a dilate grows the blobs back over the apples’ darker sides, which had dropped out of the mask.
  • Each remaining blob’s outline is measured for area, circularity, aspect ratio, extent, and solidity. Only blobs that pass every check count as apples.
  • Two tracks, Apple 1 and Apple 2, predict each apple’s next position from its motion and match it to the nearest apple blob near that prediction.

Every threshold was tuned live on the video with a slider panel, then saved as constants once only the apples survived.

OpenCV slider window with the tuned values: hue 115 to 145 and 145 to 179, saturation 127 to 255, value 45 to 180, kernel 5, dilate 3, and the area, circularity, aspect ratio, extent, and solidity limits

The tuning panel with the final values: color ranges at the top, then the cleanup and shape limits.

How it came together

The apples start close to the camera and large, then shrink until they’re smaller than some of the background blobs around them. That made area a weak filter on its own: the upper limit had to allow the large apples at the start, and the lower limit couldn’t reject the small ones at the end. Circularity carried the load instead. The book spines and other noise are tall and narrow, while the apples stay round at every size.

A debug view made the tuning readable. Every rejected blob is drawn in red and labeled with the filter it failed (for example circ0.45), so each slider could be adjusted until the non-apple outlines turned red and the apples stayed green.

Tracker in debug view, frame 279: the two apples boxed and labeled with their trails behind them, and red boxes on the bookshelf labeled with failed filters such as circ0.34 and small

Debug view near the end of the video. The bookshelf blobs are rejected, mostly for low circularity, while both apples are still tracked.

When the apples come together, the mask sees one wide, irregular blob that fails every shape filter. A tracker that simply took the nearest blob would lose both apples or swap their labels. Instead, when the two predicted positions are close and one bounding box covers both, the tracker treats the apples as merged, ignores the oblong blob, and lets both tracks coast along their predicted motion until two round blobs reappear. Through the crossing in the test video, the labels never swapped.

Key decisions

  • Color and shape instead of a learned detector. Every decision is explainable and tunable, and it runs in real time with no training data.
  • Filter early with tight color ranges. The HSV mask removed most of the noise and false detections before any shape check ran, which made it the strongest step in the pipeline.
  • Coast through merges instead of guessing. When the evidence is ambiguous, the tracker waits for two clear blobs and doesn’t force a match.

What went wrong

The motion prediction assumes each apple keeps a constant velocity. At one point an apple hit a surface and changed direction, so the prediction pointed the wrong way and the tracker briefly reported a false merge with the background. It recovered on its own and the labels stayed correct, but it shows where a constant-velocity model breaks down.

What I’d do differently

  • A Kalman filter in place of the constant-velocity prediction: it handles sudden changes in direction better, and its search area grows naturally while a track is coasting.
  • Optimal assignment (the Hungarian algorithm) instead of closest-first matching, which matters once there are more than two objects.
  • Thresholds that adapt: the current values were tuned for this one video, so different lighting or a different object would need retuning.

The code for this project was written with AI assistance. I set the goal, checked the tracking against the video, and wrote the course report this page draws from. The test video was provided with the assignment.