Computing the dense optical flow between two frames
The optical flow is a family of algorithms which addresses the issue of finding the movement of points between two images (usually subsequent frames in a video). Dense optical flow algorithms find movements of all pixels in a frame. The dense optical flow can be used to find objects moving in a sequence of frames, or to detect camera movements. In this recipe, we will find out how to compute and display the dense optical flow in several ways, using OpenCV functionality.
Getting ready
Before you proceed with this recipe, you need to install the OpenCV 3.x Python API package.
How to do it...
You need to perform the following steps:
- Import the modules we're going to use:
import cv2 import numpy as np
- Define the function to display the optical flow:
def display_flow(img, flow, stride=40): for index in np.ndindex(flow[::stride, ::stride].shape[:2]): pt1 = tuple(i*stride for i in index) delta = flow[pt1].astype(np.int32)[::-1] ...