A short Python program implementing the Lucas-Kanade Optical Flow algorithm using NumPy, Matplotlib, and OpenCV-Python (cv2).
This program finds the optical flow from one frame of an image set to the next. First, the two frames are converted to grayscale, and the difference is taken between them after scaling. The starting frame is convolved with the two gradient filters, gx and gy, to create the matrices Ix and Iy. These matrices are used along with the difference to calculate the optical flow vectors with OLS. The optical flow vectors are then sent with two copies of the starting frame to the plotting function. One resulting image contains the Vx,Vy vectors plotted, and the other uses circles to show the magnitude or L2 distance of Vx,Vy. The vectors are plotted every 4 pixels, and the magnitudes are checked every 4 pixels, only being plotted when the magnitude is greater than one.
With optical flow computed and projected on the first frame:
With optical flow computed and projected on the first frame: