Movement detection dense optical flow

Optical flow is a fundamental concept in computer vision that describes the apparent motion of objects in an image or sequence of images. It involves tracking the movement of pixels or features from one frame to another, providing valuable information about the direction and velocity of objects in a scene. There are two main approaches to optical flow: sparse optical flow and dense optical flow.
01. Sparse Optical Flow: Sparse optical flow computes the motion vectors only for a select set of interest points or keypoints in the image. These interest points are typically detected using feature detection algorithms such as Shi-Tomasi, Harris, or SIFT. Once the keypoints are identified in consecutive frames, the displacement of each keypoint is estimated using techniques like Lucas-Kanade optical flow. Sparse optical flow is computationally efficient but may not capture the motion of all pixels in the scene.
02. Dense Optical Flow: Dense optical flow estimates motion vectors for every pixel in the image, providing a dense motion field across the entire frame. It computes the optical flow at each pixel location by analyzing the spatial intensity changes between consecutive frames. Dense optical flow algorithms, such as the Farneback method or DeepFlow, utilize techniques like variational optimization or convolutional neural networks (CNNs) to estimate pixel-wise motion vectors efficiently. Dense optical flow provides a more detailed representation of motion but can be more computationally demanding compared to sparse optical flow.

Description

In this exercise we will use the dense optical flow approach, to obtain information about the movement of the cars in a video.

For this purpose, each pixel is analysed comparing the differences between frames and coloring it depending on the direction and speed of this change.

Requirements

(libraries to install):

pip install numpy
pip install cv2

Execution

Inside the folder in the CMD, you can execute the program by writing:

python 05_movement_detection_DenseOpticalFlow.py

To calculate the dense optical flow, the frame will be converted to gray colorscal and the Farneback method will be applu. This calculation obtains the direction of each pixel as a Hue value in HSV colorscape and the magnitude encoded as the Value component. This visualization technique provides insight into the movement patterns within the video frame, with different colors indicating the direction of motion and brightness representing the magnitude of motion. The HSV image is converted back to the BGR color space for visualization purposes and the result will be similar to the next image, where the cars movement can be easily visualized:

Besides, the final video processed is saved to the code folder. 

Download

To receive the “Movement detection dense optical flow” code, please fill out the form below.

Upon submission, we’ll promptly send you the ZIP file containing the code you requested. Please ensure you’ve checked your spam folder if you don’t receive our email shortly after submitting your request.