MeanShift and CamShift Tracking with OpenCV

MeanShift and CamShift Tracking with OpenCV

In this article, I am going to discuss MeanShift and CamShift Tracking with OpenCV. Please read our previous article where we discussed Optical Flow Coding with OpenCV.

MeanShift and CamShift Tracking with OpenCV

MeanShift is a color histogram-based iterative technique for tracking objects. It works by repeatedly adjusting a window around the original object position, looking for the region with the maximum density of colors that is similar to the color distribution of the item.

The MeanShift Algorithm Proceeds as Follows:

Calculation of Histograms: During the initialization phase, a histogram is built to reflect the color distribution of the target object in the first frame. The color values of a certain color system, such as the Hue-Saturation-Value (HSV) color space, are commonly used to generate this histogram.

Back Projection: A back projection image is formed in each consecutive frame by mapping the color values of the frame to the histogram. This backprojection picture highlights regions with colors that are similar to the target item.

Iterative Shifting: The MeanShift technique moves the search window iteratively depending on the backprojection image’s center of mass. The search window is shifted toward locations with higher color density, iteratively refining the position of the item.

Using OpenCV to Implement MeanShift Tracking:

For MeanShift tracking, OpenCV offers a handy method named cv2.meanShift(). Here’s an example of how to use OpenCV to achieve MeanShift tracking:

import cv2

# Load the video
cap = cv2.VideoCapture('video.mp4')

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

# Define the initial window for tracking
x, y, width, height = 100, 100, 150, 150
track_window = (x, y, width, height)

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

# Create a region of interest (ROI) based on the track window
roi = hsv_frame[y:y+height, x:x+width]

# Calculate the histogram of the ROI
roi_hist = cv2.calcHist([roi], [0], None, [180], [0, 180])

# Normalize the histogram
cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)

# Set the termination criteria for the MeanShift algorithm
term_criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)

while True:
# Read the next frame
ret, frame = cap.read()

if not ret:
break

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

# Backproject the histogram onto the frame
dst = cv2.calcBackProject([hsv_frame], [0], roi_hist, [0, 180], 1)

# Apply the MeanShift algorithm to track the object
ret, track_window = cv2.meanShift(dst, track_window, term_criteria)

# Draw the tracked object on the frame
x, y, w, h = track_window
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

# Display the frame
cv2.imshow('MeanShift Tracking', frame)

# Exit loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# Release the video capture
cap.release()

# Close all OpenCV windows
cv2.destroyAllWindows()

CamShift (Continuously Adaptive MeanShift) is a MeanShift algorithm modification that allows for scale and rotation invariant object tracking. It modifies the size and orientation of the tracking window dynamically based on the form and distribution of the object.

CamShift works similarly to MeanShift, but instead of utilizing a fixed-size window, it adjusts the window size and orientation based on the object’s aspect ratio and degree of rotation.

Using OpenCV to Implement CamShift Tracking:

For CamShift tracking, OpenCV provides the cv2.CamShift() method. Here’s an example of how to use OpenCV to achieve CamShift tracking:

import cv2

# Load the video
cap = cv2.VideoCapture('video.mp4')

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

# Define the initial window for tracking
x, y, width, height = 100, 100, 150, 150
track_window = (x, y, width, height)

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

# Create a region of interest (ROI) based on the track window
roi = hsv_frame[y:y+height, x:x+width]

# Calculate the histogram of the ROI
roi_hist = cv2.calcHist([roi], [0], None, [180], [0, 180])

# Normalize the histogram
cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM

# Set the termination criteria for the CamShift algorithm
term_criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)

while True:
# Read the next frame
ret, frame = cap.read()

if not ret:
break

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

# Backproject the histogram onto the frame
dst = cv2.calcBackProject([hsv_frame], [0], roi_hist, [0, 180], 1)

# Apply the CamShift algorithm to track the object
ret, track_window = cv2.CamShift(dst, track_window, term_criteria)

# Draw the tracked object on the frame
pts = cv2.boxPoints(ret)
pts = np.int0(pts)
cv2.polylines(frame, [pts], True, (0, 255, 0), 2)

# Display the frame
cv2.imshow('CamShift Tracking', frame)

# Exit loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# Release the video capture
cap.release()

# Close all OpenCV windows
cv2.destroyAllWindows()

In the code above, we take the same steps as in MeanShift tracking. The distinction is that the cv2.CamShift() method is used instead of cv2.meanShift(), and the rotated bounding box is then drawn using cv2.polylines().

OpenCV provides strong Object Tracking algorithms such as MeanShift and CamShift. They enable machines to follow objects and adjust to changes in location, size, and rotation based on their color distributions. These algorithms provide quick and effective techniques for tracking objects in a variety of applications such as surveillance, robotics, and augmented reality.

Using OpenCV’s capabilities, developers may simply create MeanShift and CamShift tracking, allowing machines to monitor objects of interest in video sequences. Object tracking algorithms will continue to improve as computer vision advances, combining deep learning techniques and handling increasingly complicated tracking circumstances. So, use OpenCV to harness the power of MeanShift and CamShift tracking and uncover the potential for precise and accurate object tracking in your computer vision applications.

In the next article, I am going to discuss the Overview of Various Tracking API Methods. Here, in this article, I try to explain MeanShift and CamShift Tracking with OpenCV. I hope you enjoy this MeanShift and CamShift Tracking with OpenCV article. Please post your feedback, suggestions, and questions about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *