How it works...

To compute the optical flow, you need two images (which are usually consecutive frames from a video). Both methods that we've used in the code accept 8-bit grayscale images as frames.

First, let's discuss the usage of the cv2.calcOpticalFlowFarneback function. It takes the following parameters, the previous frame, current frame, initialization for optical flow, scale between the pyramid's layers, number of layers in the pyramid, size of the window for the smoothing step, number of iterations, number of neighborhood pixels to find the polynom's parameters, standard deviation of Gaussian (which is used to smooth the polynom's derivatives), and finally, flags.

The last parameter manages the optical flow process, if cv2.OPTFLOW_FARNEBACK_GAUSSIAN ...

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.