OpenCV Video Object Tracking (MeanShift, CamShift)

In the field of computer vision, video object tracking is a very important task.

Video object tracking is widely used in many fields such as surveillance, autonomous driving, and human-computer interaction.

OpenCV provides a variety of object tracking algorithms, among which MeanShift and CamShift are two classic and commonly used algorithms. This article will explain in detail the principles, implementation steps, and how to use them in OpenCV.

1. MeanShift Algorithm

1.1 Algorithm Principle

The MeanShift algorithm is a density-based non-parametric clustering algorithm. It was initially used for image segmentation and later introduced into the field of object tracking. Its core idea is to iteratively calculate the centroid of the target region and move the window center to the centroid position, thereby achieving object tracking.

The basic steps of the MeanShift algorithm are as follows:

  1. Initialize window: In the first frame of the video, manually or automatically select a target region as the initial window.
  2. Calculate centroid: In the current window, calculate the centroid of the target region (i.e., the mean of pixel points).
  3. Move window: Move the window center to the centroid position.
  4. Iterate: Repeat steps 2 and 3 until the window center no longer changes or the maximum number of iterations is reached.

1.2 Implementation in OpenCV

In OpenCV, the MeanShift algorithm is implemented through thecv2.meanShift()function. The following is a simple example code:

Example

import cv2
import numpy as np

# Read video
cap = cv2.VideoCapture('video.mp4')

# Read the first frame
ret, frame = cap.read()

# Set initial window (x, y, width, height)
x, y, w, h = 300, 200, 100, 50
track_window = (x, y, w, h)

# Set ROI (Region of Interest)
roi = frame[y:y+h, x:x+w]

# Convert to HSV color space
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)

# Create mask and calculate histogram
mask = cv2.inRange(hsv_roi, np.array((0., 60., 32.)), np.array((180., 255., 255.)))
roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0, 180])
cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)

# Set termination criteria
term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)

while True:
    ret, frame = cap.read()
    if not ret:
        break

    # Convert to HSV color space
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # Calculate back projection
    dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)

    # Apply MeanShift algorithm
    ret, track_window = cv2.meanShift(dst, track_window, term_crit)

    # Draw tracking result
    x, y, w, h = track_window
    img2 = cv2.rectangle(frame, (x, y), (x+w, y+h), 255, 2)
    cv2.imshow('MeanShift Tracking', img2)

    if cv2.waitKey(30) & 0xFF == 27:
        break

cap.release()
cv2.destroyAllWindows()

1.3 Advantages and Disadvantages

Advantages:

  • Simple and easy to implement, with high computational efficiency.
  • Insensitive to changes in the object's shape and size.

Disadvantages:

  • Poor handling capability for fast motion or occlusion of the target.
  • The window size is fixed and cannot adapt to changes in target size.

2. CamShift Algorithm

2.1 Algorithm Principle

The CamShift (Continuously Adaptive MeanShift) algorithm is an improved version of MeanShift. It tracks targets better by adaptively adjusting the window size. Based on MeanShift, the CamShift algorithm adds adjustments to the window size and orientation, enabling it to adapt to changes in the target's size and rotation in the video.

The basic steps of the CamShift algorithm are as follows:

  1. Initialize window: Same as MeanShift, select the initial window in the first frame of the video.
  2. Calculate centroid: In the current window, calculate the centroid of the target region.
  3. Move window: Move the window center to the centroid position.
  4. Adjust window size and orientation: Adjust the window according to the target's size and orientation.
  5. Iterate: Repeat steps 2 to 4 until the window center no longer changes or the maximum number of iterations is reached.

2.2 Implementation in OpenCV

In OpenCV, the CamShift algorithm is implemented through thecv2.CamShift()function. The following is a simple example code:

Example

import cv2
import numpy as np

# Read video
cap = cv2.VideoCapture('video.mp4')

# Read the first frame
ret, frame = cap.read()

# Set initial window (x, y, width, height)
x, y, w, h = 300, 200, 100, 50
track_window = (x, y, w, h)

# Set ROI (Region of Interest)
roi = frame[y:y+h, x:x+w]

# Convert to HSV color space
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)

# Create mask and calculate histogram
mask = cv2.inRange(hsv_roi, np.array((0., 60., 32.)), np.array((180., 255., 255.)))
roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0, 180])
cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)

# Set termination criteria
term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)

while True:
    ret, frame = cap.read()
    if not ret:
        break

    # Convert to HSV color space
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # Calculate back projection
    dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)

    # Apply CamShift algorithm
    ret, track_window = cv2.CamShift(dst, track_window, term_crit)

    # Draw tracking result
    pts = cv2.boxPoints(ret)
    pts = np.int0(pts)
    img2 = cv2.polylines(frame, [pts], True, 255, 2)
    cv2.imshow('CamShift Tracking', img2)

    if cv2.waitKey(30) & 0xFF == 27:
        break

cap.release()
cv2.destroyAllWindows()

2.3 Advantages and Disadvantages

Advantages:

  • Can adapt to changes in the target's size and orientation.
  • Good robustness to shape changes and rotation of the target.

Disadvantages:

  • Handling capability for fast motion or occlusion of the target is still limited.
  • Computational complexity is slightly higher than MeanShift.

Comparison of MeanShift and CamShift

MeanShift and CamShift are two classic object tracking algorithms, and both have ready-made implementations in OpenCV.

The MeanShift algorithm is simple and efficient, suitable for scenarios where the target size and orientation do not change much. The CamShift algorithm, by adaptively adjusting the window size and orientation, can better handle changes in target size and orientation. In practical applications, you can choose the appropriate algorithm according to specific needs.

FeatureMeanShiftCamShift
Window sizeFixed sizeAdaptively adjusts size and orientation
Applicable scenariosScenarios with fixed target sizeScenarios where target size and orientation change
Computational complexityLowerHigher
Real-time performanceBetterSlightly worse
Other extensions