We will see how to track an object based on colors. To do this I used the OpenCV Mean-shift algorithm. This way we can keep track of the history of the object. In my example I used a bottle of mouthwash, we’ll take the label as a reference.

We can do this in 2 steps:

  1. Take the photo and run a histogram
  2. Track object and Mean-shift

1. Take the photo and run a histogram

We must first call the video and for this, we use the function of OpenCV cv2.VideoCapture. In this case, I used the first frame and marked the label as ROI (Region of interest).

To evaluate and better understand the part on HSV I recommend that you read this article: Detecting colors HSV color space OpenCV with python or this Object detection using HSV Color space but for the moment I show you the code:

video = cv2.VideoCapture("mouthwash.avi")

_, first_frame = video.read()
x = 300
y = 305
width = 100
height = 115
roi = first_frame[y: y + height, x: x + width]
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
roi_hist = cv2.calcHist([hsv_roi], [0], None, [180], [0, 180])
roi_hist = cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)

2. Track object and Mean-shift

Having found a way to identify the object, we need to keep track of the positions and memorize them. For this purpose, we use the meanShift OpenCV function. You can learn more about tutorial meanshift.

In the code below I show how this can be called and used in real conditions.

  _, frame = video.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    mask = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)

    _, track_window = cv2.meanShift(mask, (x, y, width, height), term_criteria)
    x, y, w, h = track_window

Conclusion

We have seen how to do color-based object tracking with OpenCV functions. I recommend that you download the code and do some exercises to better understand how it works

Object detection and Object tracking

The object tracking and recognition method works but I advise you to evaluate my course Object Detection (Opencv & Deep Learning) to have a more effective and safer system based on AI.