Python OpenCV: Optical Flow with Lucas-Kanade method

Python OpenCV: Optical Flow with Lucas-Kanade method

Optical flow is a pattern of motion of objects, surfaces, or edges in a visual scene caused by the relative motion between an observer (an eye or a camera) and the scene. The Lucas-Kanade method is a widely used differential method for optical flow estimation developed by Bruce D. Lucas and Takeo Kanade.

Here's how to compute optical flow using the Lucas-Kanade method in OpenCV:

  1. Install OpenCV:

    If you haven't already, you'll need to install OpenCV:

    pip install opencv-python 
  2. Lucas-Kanade Optical Flow:

    Here's a simple example to demonstrate the Lucas-Kanade method using OpenCV:

    import cv2 import numpy as np cap = cv2.VideoCapture('path_to_your_video_file_or_camera_index') # Parameters for ShiTomasi corner detection (good features to track) feature_params = dict(maxCorners=100, qualityLevel=0.3, minDistance=7, blockSize=7) # Parameters for Lucas-Kanade optical flow lk_params = dict(winSize=(15, 15), maxLevel=2, criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) # Get the first frame and find corners in it ret, old_frame = cap.read() old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY) p0 = cv2.goodFeaturesToTrack(old_gray, mask=None, **feature_params) # Create a mask for drawing purposes mask = np.zeros_like(old_frame) while True: ret, frame = cap.read() frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Calculate optical flow p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params) # Select good points good_new = p1[st == 1] good_old = p0[st == 1] # Draw tracks for i, (new, old) in enumerate(zip(good_new, good_old)): a, b = new.ravel() c, d = old.ravel() mask = cv2.line(mask, (a, b), (c, d), (0, 255, 0), 2) frame = cv2.circle(frame, (a, b), 5, (0, 0, 255), -1) img = cv2.add(frame, mask) cv2.imshow('Optical Flow', img) # Update previous frame and previous points old_gray = frame_gray.copy() p0 = good_new.reshape(-1, 1, 2) if cv2.waitKey(30) & 0xFF == 27: # ESC key pressed? break cap.release() cv2.destroyAllWindows() 
  3. Note:

    Make sure you replace 'path_to_your_video_file_or_camera_index' with the path to your video file. If you want to use the default camera, replace it with 0.

This code initializes Shi-Tomasi corners in the first video frame and then tracks those corners using the Lucas-Kanade optical flow method in the subsequent frames. The result is a visual representation of motion tracking in the video.


More Tags

svg-filters glassfish automapper angularfire location-provider scikit-learn yocto filter public-key-encryption git-tag

More Programming Guides

Other Guides

More Programming Examples