Object Tracking based on Colors

Object Tracking based on Colors

2 minutes, 49 seconds Read

Object Tracking based on Colors are tasks that are important and challenging such as video surveillance and vehicle navigation.

Image processing is a method of extracting some useful information by converting images into digital information by performing some operations on it.

Introduction Object Tracking based on Colors

Here we track objects in real-time and we apply some preprocessing steps on each video frame in order to track RGB-colored objects. To detect colored objects we subtract RGB color components from the grayscale image and remove undesirable noise from the image, then remove noise from the image we used a filter.

To detect objects accurately system removes all unwanted objects. Then we convert the grayscale image to a binary image. We used blob analysis methodology to detect the RGB-colored objects. Finally, System will display RGB colored Objects in rectangular boxes using the bounding box methodology. This system tracks all red, blue, and green colored objects and draws a bounding box around them. This system can be used for tracking various colored objects.

Installing Libraries:-

pyautogui: pip install pyautogui

HSV Value:-

  • HSL and HSV are alternative representations of the RGB color model, designed in the 1970s by computer graphics researchers to more closely align with the way human vision perceives color-making attributes
  • HSV Color Space. The HSV color space (hue, saturation, value) is often used by people who are selecting colors (e.g., of paints or inks) from a color wheel or palette, because it corresponds better to how people experience color than the RGB color space.

BGR to HSV:-

#dst = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)

hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)

Minimum Enclosing Circle:-

#((x, y), radius) = cv2.minEnclosingCircle(countourArea)

((x, y), radius) = cv2.minEnclosingCircle(c)

Moments to find the center of the Area:-

Image moments help you to calculate some features like the center of mass of the object, the area of the object, etc.

#var = cv2.moments(contourArea)

M = cv2.moments(c)

center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

Drawing Circle:-

# cv2.circle(src, (x,y), int(radius),colour,thickness)

 cv2.circle(frame, (int(x), int(y)), int(radius),  (0, 255, 255), 2)

#cv2.circle(frame, center, 5, (0, 0, 255), -1)

cv2.circle(frame, center, 5, (0, 0, 255), -1)

Block Diagram:-

Object Tracking based on Colors

Source Code:-

import imutils

import cv2

redLower = (157, 93, 203)

redUpper = (179, 255, 255)


while True:

(grabbed, frame) = camera.read()

frame = imutils.resize(frame, width=600)

blurred = cv2.GaussianBlur(frame, (11, 11), 0)

hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)

mask = cv2.inRange(hsv, redLower, redUpper)

mask = cv2.erode(mask, None, iterations=2)

mask = cv2.dilate(mask, None, iterations=2)

cnts = cv2.findContours(mask.copy(),   cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2] center = None

if len(cnts) > 0:

c = max(cnts, key=cv2.contourArea)

((x, y), radius) = cv2.minEnclosingCircle(c)

M = cv2.moments(c)

center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

if radius > 10:

cv2.circle(frame, (int(x), int(y)), int(radius),

(0, 255, 255), 2)

cv2.circle(frame, center, 5, (0, 0, 255), -1)


if radius > 250:











cv2.imshow("Frame", frame)

key = cv2.waitKey(1) & 0xFF

if key == ord("q"):





  • The system tracks colored objects with good accuracy.
  • Allows for tracking objects in live video.


  • Low Accuracy in low-resolution camera footage
  • Low Accuracy in poor lighting conditions

Similar Posts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.