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 surveillance, autonomous driving, human-computer interaction, and other fields.
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 these two algorithms in OpenCV.
1. MeanShift algorithm
1.1 Algorithm principle
The MeanShift algorithm is a density-based non-parametric clustering algorithm, originally used for image segmentation and later introduced into the field of object tracking. Its core idea is to achieve object tracking by iteratively computing the centroid of the target region and moving the window center to the centroid position.
The basic steps of the MeanShift algorithm are as follows:
- Initialize the window: in the first frame of the video, manually or automatically select a target region as the initial window.
- Compute the centroid: in the current window, compute the centroid of the target region (i.e., the mean of the pixel points).
- Move the window: move the window center to the centroid position.
- 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 the cv2.meanShift() function. The following is a simple example code:
Example
import
cv2
import
numpy
as
np
Read the video
cap
=
cv2.
VideoCapture
(
'video.mp4'
)
Read the first frame
ret
,
frame
=
cap.
read
(
)
Set the 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 a mask and compute the 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 the 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
)
Compute the back projection
dst
=
cv2.
calcBackProject
(
[
hsv
]
,
[
0
]
,
roi_hist
,
[
0
,
180
]
,
1
)
Apply the MeanShift algorithm
ret
,
track_window
=
cv2.
meanShift
(
dst
,
track_window
,
term_crit
)
Draw the 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 Pros and Cons
Advantages:
- Simple and easy to implement, with high computational efficiency.
- Insensitive to changes in the target's shape and size.
Disadvantages:
- Poor at handling fast movement 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. Built on MeanShift, CamShift adds adjustment of the window size and orientation, enabling it to adapt to changes in the size and rotation of the target in the video.
The basic steps of the CamShift algorithm are as follows:
- Initialize the window: Same as MeanShift, select the initial window in the first frame of the video.
- Compute the centroid: In the current window, compute the centroid of the target region.
- Move the window: move the window center to the centroid position.
- Adjust the window size and orientation: Adjust the window according to the target's size and orientation.
- 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 can be applied through the cv2.CamShift() function. The following is a simple example code:
Example
import
cv2
import
numpy
as
np
Read the video
cap
=
cv2.
VideoCapture
(
'video.mp4'
)
Read the first frame
ret
,
frame
=
cap.
read
(
)
Set the 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 a mask and compute the 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 the 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
)
Compute the back projection
dst
=
cv2.
calcBackProject
(
[
hsv
]
,
[
0
]
,
roi_hist
,
[
0
,
180
]
,
1
)
Apply the CamShift algorithm
ret
,
track_window
=
cv2.
CamShift
(
dst
,
track_window
,
term_crit
)
Draw the 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 Pros and Cons
Advantages:
- Able to adapt to changes in the target's size and orientation.
- Robust to changes in the target's shape and rotation.
Disadvantages:
- Still limited in handling fast movement or occlusion of the target.
- Computational complexity is slightly higher than MeanShift.
Comparison of MeanShift and CamShift
MeanShift and CamShift are two classic target tracking algorithms, and both have ready-made implementations in OpenCV.
The MeanShift algorithm is simple and efficient, suitable for scenarios where the target's size and orientation change little, while 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.
| Feature | MeanShift | CamShift |
|---|---|---|
| Window size | Fixed size | Adaptively adjusts size and orientation |
| Applicable scenarios | Scenarios where the target size is fixed | Scenarios where the target size and orientation change |
| Computational complexity | Lower | Higher |
| Real-time performance | Good | Slightly worse |