Slightly rearrangment for this article(OpenCV: Optical Flow)
Optical flow is the pattern of apparent motion of image objects bettween two consecutive frames caused by motion of object or camera. It's 2D vector field where each vector is a displacement vector showing the movement of points from first frame to the second. Illustration shows below(Image courtesy:https://en.wikipedia.org/wiki/Optical_flow)
It shows a ball moving in five consecutive frames. The arrow shows its displacement vector. Optical flow has many applications in areas like:
1. Structure from Motion
2. Video compensation
3. Video Stabilization
Note: Optical flow works on several assumptions:
1. The pixel intensities of an object do not change between consecutive frames.
2. Neighbouring pixels have similar motion
Consider a pixel I(x, y, t) in first frame(check a new dimension, time, is added here. Earlier we were working with images only, so no need of time). It moves by distance (dx, dy) in next frame taken after dt time. So since those pixels are the same and intensity does not change, we can say,
Then take taylor series approximation of right-hand side, remove common terms and devide by dt to get the following equation:
where:
Above equation is called Optical flow equation. In it, we can find fx and fy, they are image gradients. Similarly ft is the gradient along time. but (u,v) is unknown. we can't solve this one equation with two unknown variables. So several methods are provided to solve this problem and one of them is Lucas-Kanade.
Lucas-Kanade method
We have seen an assumption before, that all the neighbouring pixels will have similar motion. Lucas-Kanade method takes a 3x3 patch around the point. So all the 9 points have the same motion. We can find (fx, fy, ft) for these 9 points. So now our problem becomes solving 9 equations with two unknown variables which is over-determined. A better solution is obtained with least square fit method. Below is the final solution which is two equation-two unknown problem and solve to get the solution.
Check similarity of inverse matrix with harris corner detector. It denotes that corners are better points to be tracked.
So from the user point of view, the idea is simple, we give some points to track, we receive the optical flow vectors of those points. But again there are some problems. Until now, we were dealing with small motions, so it fails when there is a large motion. To deal with this we use pyramids. When we go up in the pyramid, small motions are removed and large motions become small motions. So by applying Lucas-Kanade there, we get optical flow along with the scale.
Dense Optical Flow in OpenCV
Lucas-Kanade method computes optical flow for a sparse feature set(in our example, corners detected using Shi-Tomasi algorithm). OpenCV provides another algorithm to find the dense optical flow. It computes the optical flow for all the points in the frame. It is based on Gunnar Farneback's algorithm which is explianed in "Two-Frame Motion Estimation Based on Polynomial Expansion" by Gunnar Farneback in 2003.
Below sample shows how to find the dense optical flow using above algorithm. We get 2-channel array with optical flow vectors, (u, v). We find their magnitude and direction. We color code the result for better visualization.Direction corresponds to Hue value of the image, Magnitude corresponds to Value plane. See the python code below:
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(cv.samples.findFile("vtest.avi"))
ret, frame1 = cap.read()
prvs = cv.cvtColor(frame1, cv.COLOR_BGR2GRAY)
hsv = np.zeros_like(frame1)
hsv[..., 1] = 255
while(1):
ret, frame2 = cap.read()
if not ret:
print('No frames grabbed!')
break
next = cv.cvtColor(frame2, cv.COLOR_BGR2GRAY)
flow = cv.calcOpticalFlowFarneback(prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0)
mag, ang = cv.cartToPolar(flow[..., 0], flow[..., 1])
hsv[..., 0] = ang*180/np.pi/2
hsv[..., 2] = cv.normalize(mag, None, 0, 255, cv.NORM_MINMAX)
bgr = cv.cvtColor(hsv, cv.COLOR_HSV2BGR)
cv.imshow('frame2', bgr)
k = cv.waitKey(30) & 0xff
if k == 27:
break
elif k == ord('s'):
cv.imwrite('opticalfb.png', frame2)
cv.imwrite('opticalhsv.png', bgr)
prvs = next
cv.destroyAllWindows()
See the result below: