The medial flow tracker
In this recipe, we're going to apply the Median Flow object tracker to track objects in a video. This tracker works in real time (even faster on modern hardware) and does its job accurately and steadily. Also, this tracker has a nice feature, it can determine the tracking failure. Let's see how we can use it in our applications.
Getting ready
Before you proceed with this recipe, you need to install the OpenCV 3.x Python API package with the OpenCV Contrib modules.
How to do it...
The steps for this recipe are:
- Import all of the necessary modules:
import cv2 import numpy as np
- Open a video file, read its frame, and select an object to track:
cap = cv2.VideoCapture("../data/traffic.mp4") _, frame = cap.read() bbox = cv2.selectROI(frame, False, True) cv2.destroyAllWindows()
- Create the Median Flow tracker and initialize it with the first frame from the video and the bounding box we've selected. Then, read the remaining frames one-by-one, feed them into the tracker, and get...