Computer vision · OpenCV

Tracking a green object by colour

A single OpenCV script follows a green object through a video using rules, with no learned model. Each frame is brightened and blurred, then masked in HSV around pure green. Contours that are too small are discarded. Each remaining contour is wrapped in a convex hull and its centroid is computed from image moments. The script then smooths the centroid path and plots it together with the per-frame speed.

  • Python
  • OpenCV
  • NumPy
  • Matplotlib
  • HSV thresholding
  • Image moments

Status: early learning project, a single script. The input video, the annotated output and the plots are committed to the repo.

Frame 450 of tracked_output.mp4: a blurred, brightened scene with the tracked green object outlined by a blue convex hull, a red centroid dot and a centre-coordinate label.
Frame 450 of tracked_output.mp4, as written by the script (1280×592, 30 fps, 915 frames).

01What it does

Frame 450 of the original test_video.mp4 before any processing.
Input: frame 450 of test_video.mp4.
Frame 150 of tracked_output.mp4 with the convex hull and centroid drawn on the object.
Output: frame 150 of tracked_output.mp4.

The script reads test_video.mp4 and writes tracked_output.mp4 with the overlays drawn on the processed (brightened and blurred) frames. While it runs, it also shows the tracking view and the binary mask in two windows. At the end it saves object_path.png and velocity_plot.png.

02How it works

  1. Contrast and brightness

    convertScaleAbs applies g = αf + β with α = 1.5 and β = 40.

  2. Blur

    A 15×15 Gaussian blur removes speckle noise before thresholding.

  3. HSV mask

    BGR (0,255,0) is converted to HSV, which gives a hue of 60. The mask keeps hue 60 ± 35 with saturation and value both between 100 and 255.

  4. Contours

    findContours with RETR_EXTERNAL finds the outer contours. Any contour with an area of 290 px² or less is discarded.

  5. Convex hull

    convexHull closes gaps left by pixels the mask missed. The hull is drawn in blue.

  6. Centroid

    The image moments give cx = m10/m00 and cy = m01/m00. These coordinates are added to the trajectory and labelled on the frame.

  7. Smoothing

    A moving average with a window of 21 smooths the x and y series (np.convolve, mode valid).

  8. Speed

    The script computes the Euclidean step between consecutive smoothed centroids, in pixels per frame.

vi = √((xi − xi−1)² + (yi − yi−1)²)  [px / frame]; multiply by fps (30) for px / s

The README describes speed as fps × distance. The code leaves out the fps factor, so the plotted axis is in pixels per frame.

03Results

Two plots from object_path.png. Left: smoothed object trajectory in pixel coordinates with the y axis inverted. Right: object speed in pixels per frame against frame number.
object_path.png from the repo, downscaled. Left: smoothed trajectory (top-down view, y axis inverted). Right: speed against frame number.
QuantityValueWhere it comes from
Video1280×592, 30 fps, 915 framesmetadata of tracked_output.mp4
Hue window60 ± 35 (OpenCV 0–180 scale)get_limits(green)
Minimum contour area290 px²code constant, tuned by hand
Smoothing window21 samplescode constant

The repo has no ground-truth positions, so the tracking accuracy is not measured. The plots show the path and speed of the smoothed centroid only.

04Run it locally

The README does not list commands. The script reads test_video.mp4 from the working directory.

# clone and install
git clone https://github.com/Avinashistoocool/Object-tracking-with-color.git
cd Object-tracking-with-color
pip install opencv-python numpy matplotlib
# writes tracked_output.mp4, object_path.png, velocity_plot.png; press q to stop early
python color_based_tracking.py

05Limitations

06Further reading