Skip to content

Instantly share code, notes, and snippets.

@jhaberstro
Last active January 9, 2018 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhaberstro/3dc58512b75a02644e0e7298d5578eaf to your computer and use it in GitHub Desktop.
Save jhaberstro/3dc58512b75a02644e0e7298d5578eaf to your computer and use it in GitHub Desktop.
Lucas-Kanade Dense Optical Flow
import numpy as np
from skimage import filters
def optical_flow_lk(t0, t1, sigma):
# setup the local linear systems of equations
gradients = np.gradient(t0)
dx, dy = gradients[1], gradients[0]
dt = t1 - t0
A00 = filters.gaussian(dx * dx, sigma)
A11 = filters.gaussian(dy * dy, sigma)
A10 = filters.gaussian(dx * dy, sigma)
A01 = A10
b0 = -filters.gaussian(dx * dt, sigma)
b1 = -filters.gaussian(dy * dt, sigma)
# solve the local linear systems of equations via 2x2 matrix inversion
determinant = 1.0 / ((A00 * A11) - (A10 * A10))
u = determinant * ((b0 * A11) + (b1 * -A01))
v = determinant * ((b0 * -A10) + (b1 * A00))
return u, v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment