Serengeti logo BLACK white bg w slogan

Python Book of Recipes #6 – Cooking up Color-Based Object Tracking: A Fun Computer Vision Recipe

Boško Savić, Senior Software Developer

In our previous recipe, we showed the importance of color spaces in computer vision tasks such as image processing, object detection, segmentation, and tracking. By harnessing the power of color, we'll unlock a fascinating way to track objects in video streams with remarkable accuracy.


  • OpenCV (to handle image processing)
  • NumPy (for array operations)
  • Tkinter (GUI toolkit for Python)

Our recipe revolves around tracking a specific object, such as a green ball, in real-time. The underlying concept is to isolate the desired object by defining lower and upper color boundaries, allowing our software to distinguish it from the rest of the scene. By continuously analyzing video frames, we can precisely track the object's position and trace its movement over time.

STEP 1: Preparing Workspace

  • Set up the necessary Python environment with OpenCV, NumPy and Tkinter.

STEP 2: Cooking Color-Based color tracking

  • Initialize default color boundaries.
  • With Tkinter, we create an interactive GUI layout featuring sliders to customize color boundaries.
  • The video capture process begins, and each frame undergoes resizing, blurring and converting to HSV color space.
image 1

The blurring operation helps to reduce noise and smooth out variations in the image, resulting in a cleaner and more uniform representation of the objects within the frame.

By blurring the image before converting to the HSV color space, we create a preprocessed representation more suitable for subsequent operations like color thresholding or edge detection.

  • Using the updated color boundaries, we construct a mask that highlights the desired object in the frame.
      image 2
      1. cv2.inRange() function is used to create binary mask from HSV image based on specified lower and upper color boundaries. The pixels within the specified color range are set to white (255), while pixels outside the range are set to black (0).
      2.  cv2.errode() function performs morphological erosion on the mask. Erosion is a morphological operation that erodes away the boundaries of the white regions in the   mask. It helps to remove small noise or spurious detections in the mask. The iterations=2 parameter specifies the number of times erosion is applied.
      3. cv2.dilate() function performs morphological dilation on the eroded mask. Dilation is the opposite of erosion, where it expands the white regions in the mask. This helps to fill in gaps or holes in the mask and restore the size and shape of the detected object. Like erosion, the iterations=2 parameter controls the number of dilation iterations.

      By combining the steps of thresholding (cv2.inRange()), erosion (cv2.erode()) and dilation (cv2.dilate()), the code effectively creates a binary mask that isolates the green-colored objects within the HSV image.

        image 3
        • After applying morphological operations to enhance the mask, we locate contours and extract the object's center and radius. If the object meets the size criteria, we draw a circle around it and mark its centroid on the frame. To visualize the object's trajectory, we maintain a collection of tracked points which are updated and connected with lines.
        image 4

        You can watch the video test at the link and the full code can be found here.


        With Python and Tkinter as our main ingredients, and OpenCV as our secret sauce, we've whipped up an impressive recipe for real-time object tracking. By harnessing the power of color-based segmentation and computer vision techniques, we've created an interactive application that can track objects with finesse. Whether it's for surveillance, augmented reality, or simply satisfying your curiosity, this code provides a solid foundation to dive deeper into the captivating field of computer vision. So, grab your aprons, fire up your coding stations, and get ready to embark on a captivating journey into the world of real-time object tracking!

        Let's do business

        The project was co-financed by the European Union from the European Regional Development Fund. The content of the site is the sole responsibility of Serengeti ltd.