Tracking keypoints between frames using the Lucas-Kanade algorithm
In this recipe, you will learn how to track keypoints between frames in videos using the sparse Lucas-Kanade optical flow algorithm. This functionality is useful in many computer vision applications, such as object tracking and video stabilization.
Getting ready
Before you proceed with this recipe, you need to install the OpenCV version 3.3 (or greater) Python API package.
How to do it
You need to complete the following steps:
- Import the necessary modules:
import cv2
- Open a test video and initialize the auxiliary variables:
video = cv2.VideoCapture('../data/traffic.mp4') prev_pts = None prev_gray_frame = None tracks = None
- Start reading the frames from the video, converting each image into grayscale:
while True: retval, frame = video.read() if not retval: break gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- Track the keypoints from a previous frame using the sparse Lucas-Kanade optical flow algorithm or, if you've...