OpenCV Python – Meanshift and Camshift
Meanshift and Camshift are powerful object tracking algorithms used in computer vision. They help track moving objects in video by following color distribution and object location over time.
In OpenCV Python, these methods are widely used for real-time tracking applications.
1. What are Meanshift and Camshift?
Meanshift
Meanshift is a tracking algorithm that:
- Finds the object in a probability distribution
- Moves a window toward the highest density area
- Tracks based on color histogram
Camshift (Continuously Adaptive Meanshift)
Camshift improves Meanshift by:
- Adapting window size automatically
- Handling object scale changes
- Improving tracking accuracy
2. Import OpenCV and NumPy
import cv2
import numpy as np
3. Read Video
cap = cv2.VideoCapture("video.mp4")
4. Select ROI (Region of Interest)
ret, frame = cap.read()
x, y, w, h = cv2.selectROI(frame, False)
track_window = (x, y, w, h)
5. Set Up Histogram for Tracking
roi = frame[y:y+h, x:x+w]
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, (0, 60, 32), (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)
6. Meanshift Tracking
term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
while True:
ret, frame = cap.read()
if not ret:
break
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
ret, track_window = cv2.meanShift(dst, track_window, term_crit)
x, y, w, h = track_window
img = cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.imshow("Meanshift Tracking", img)
if cv2.waitKey(30) & 0xFF == 27:
break
7. Camshift Tracking
while True:
ret, frame = cap.read()
if not ret:
break
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
ret, track_window = cv2.CamShift(dst, track_window, term_crit)
pts = cv2.boxPoints(ret)
pts = np.int32(pts)
img = cv2.polylines(frame, [pts], True, (0, 255, 0), 2)
cv2.imshow("Camshift Tracking", img)
if cv2.waitKey(30) & 0xFF == 27:
break
8. Difference Between Meanshift and Camshift
| Feature | Meanshift | Camshift |
|---|---|---|
| Window Size | Fixed | Adaptive |
| Accuracy | Moderate | High |
| Object Scale Change | No | Yes |
| Performance | Fast | Slightly slower |
9. Applications
Meanshift and Camshift are used in:
- Object tracking systems
- Video surveillance
- Motion analysis
- Robotics vision
- Gesture recognition
10. Common Mistakes
❌ Wrong ROI selection
✔ Solution:
-
Select object carefully using
cv2.selectROI()
❌ Poor tracking in complex background
✔ Solution:
- Improve lighting and color separation
11. Conclusion
Meanshift and Camshift in OpenCV Python are powerful tracking algorithms for real-time object tracking. Camshift improves upon Meanshift by adapting to object size and movement.
Once you master these, you can move to advanced tracking methods like optical flow and deep learning-based tracking.

0 Comments