0
0
Computer Visionml~5 mins

Optical flow concept in Computer Vision

Choose your learning style9 modes available
Introduction
Optical flow helps us see how things move between two pictures taken one after another. It shows the direction and speed of moving parts.
Tracking a ball moving in a sports video to know where it goes next.
Helping a robot understand how objects around it are moving to avoid collisions.
Detecting moving cars in traffic camera footage to count or follow them.
Creating smooth animations by understanding motion between frames.
Improving video stabilization by knowing how the camera or scene moves.
Syntax
Computer Vision
optical_flow = cv2.calcOpticalFlowFarneback(prev_frame, next_frame, None, pyr_scale, levels, winsize, iterations, poly_n, poly_sigma, flags)
prev_frame and next_frame are two consecutive grayscale images.
The function returns a flow field showing movement vectors for each pixel.
Examples
Calculates dense optical flow using Farneback method with common parameters.
Computer Vision
flow = cv2.calcOpticalFlowFarneback(prev_gray, next_gray, None, 0.5, 3, 15, 3, 5, 1.2, 0)
Uses different parameters to adjust sensitivity and smoothness of flow.
Computer Vision
flow = cv2.calcOpticalFlowFarneback(frame1, frame2, None, 0.3, 5, 10, 5, 7, 1.5, 0)
Sample Model
This program reads two images, calculates the optical flow between them, and prints the average motion magnitude showing how much movement is detected on average.
Computer Vision
import cv2
import numpy as np

# Load two consecutive frames in grayscale
prev_frame = cv2.imread('frame1.png', cv2.IMREAD_GRAYSCALE)
next_frame = cv2.imread('frame2.png', cv2.IMREAD_GRAYSCALE)

# Calculate optical flow using Farneback method
flow = cv2.calcOpticalFlowFarneback(prev_frame, next_frame, None, 0.5, 3, 15, 3, 5, 1.2, 0)

# Compute magnitude and angle of flow vectors
magnitude, angle = cv2.cartToPolar(flow[..., 0], flow[..., 1])

# Print average magnitude to see overall motion strength
print(f'Average motion magnitude: {np.mean(magnitude):.4f}')
OutputSuccess
Important Notes
Optical flow works best on small movements between frames.
Lighting changes or fast motion can make flow less accurate.
Grayscale images are used because color is not needed for flow calculation.
Summary
Optical flow shows how pixels move between two images.
It helps track motion in videos or camera feeds.
OpenCV's Farneback method is a popular way to compute dense optical flow.