Track objects with Camshift using OpenCVIn the realm of computer vision, object tracking plays a pivotal role in various applications such as surveillance, automotive safety systems, and augmented reality. One robust and widely used algorithm for object tracking is CamShift (Continuously Adaptive Mean Shift) in conjunction with the OpenCV library. In this article, we will delve into the intricacies of CamShift and explore how it can be employed to effectively track objects in real-time. Understanding CamShift AlgorithmThe CamShift (Continuously Adaptive Mean Shift) algorithm is a powerful computer vision technique used for object tracking in videos or image sequences. It builds upon the Mean Shift algorithm and adds the ability to adaptively adjust the size and orientation of the tracking window, making it suitable for objects that can undergo changes in scale and rotation. The beauty of CamShift lies in its continuous adaptation. After each iteration, the algorithm adjusts the size and orientation of the tracking window based on the mean shift vector. This adaptive process ensures that the algorithm can handle changes in the object's scale and rotation, making it particularly useful for scenarios where the target object undergoes such transformations. Working Structure of CamShift:The CamShift algorithm is based on color information and operates in the color space of the images or frames. It uses a histogram of color values within a specified region of interest (ROI) to track the object of interest. Here's how the CamShift algorithm works: - Initialization: The algorithm starts by selecting an initial region of interest (ROI) around the object to be tracked in the first frame. This ROI serves as the tracking window.
- Histogram Calculation: The color histogram of the selected ROI is computed in a color space such as HSV (Hue, Saturation, Value). The histogram represents the distribution of colors within the ROI.
- Back Projection: In each subsequent frame, the histogram of the ROI is back-projected onto the entire frame. This back-projection creates a probability map that highlights regions with colors similar to those in the ROI histogram.
- Mean Shift Iteration: The mean shift algorithm is applied to the back-projected probability map. Mean shift calculates the centroid of the probability distribution and shifts the tracking window to this new centroid.
Requirements:OpenCV Installation: OpenCV is a popular computer vision library that provides the tools necessary for image and video processing, including object tracking with CamShift. You need to install OpenCV to use its functions and capabilities. Install OpenCV using the following command: - pip install opencv-python
Integrated Development Environment (IDE): You'll need an environment where you can write, run, and debug your Python code. There are several options available, such as Visual Studio Code, PyCharm, Jupyter Notebook, and more. Choose the one you're most comfortable with. Importing of Libraries: - import cv2
- import numpy as np
Implementation of Code:- import numpy as np
- import cv2 as cv
-
- # Read the input video
- cap = cv.VideoCapture('sample.mp4')
-
- # Take first frame of the video
- ret, frame = cap.read()
-
- # Setup initial region of tracker
- x, y, width, height = 400, 440, 150, 150
- track_window = (x, y, width, height)
-
- # Set up the Region of Interest for tracking
- roi = frame[y:y + height, x:x + width]
-
- # Convert ROI from BGR to HSV format
- hsv_roi = cv.cvtColor(roi, cv.COLOR_BGR2HSV)
-
- # Perform masking operation
- mask = cv.inRange(hsv_roi, np.array((0., 60., 32.)), np.array((180., 255., 255)))
-
- roi_hist = cv.calcHist([hsv_roi], [0], mask, [180], [0, 180])
- cv.normalize(roi_hist, roi_hist, 0, 255, cv.NORM_MINMAX)
-
- # Setup the termination criteria, either 15 iterations or move by at least 2 points
- term_crit = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 15, 2)
-
- while(1):
- ret, frame = cap.read()
-
- # Resize the video frames
- frame = cv.resize(frame, (720, 720), fx=0, fy=0, interpolation=cv.INTER_CUBIC)
- cv.imshow('Original', frame)
-
- # Perform thresholding on the video frames
- ret1, frame1 = cv.threshold(frame, 180, 155, cv.THRESH_TOZERO_INV)
-
- # Convert from BGR to HSV format
- hsv = cv.cvtColor(frame1, cv.COLOR_BGR2HSV)
-
- dst = cv.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
-
- # Apply CamShift to get the new location
- ret2, track_window = cv.CamShift(dst, track_window, term_crit)
-
- # Draw it on the image
- pts = cv.boxPoints(ret2)
- pts = np.int0(pts)
-
- # Draw Tracking window on the video frame
- Result = cv.polylines(frame, [pts], True, (0, 255, 255), 2)
- cv.imshow('Camshift', Result)
-
- # Set ESC key as the exit button
- k = cv.waitKey(30) & 0xff
- if k == 27:
- break
-
- # Release the cap object
- cap.release()
-
- # Close all opened windows
- cv.destroyAllWindows()
Output:
|