def HornSchunck(im1, im2, alpha=0.001, Niter=8, verbose=False):
"""
im1: image at t=0
im2: image at t=1
alpha: regularization constant
Niter: number of iteration
"""
im1 = im1.astype(np.float32)
im2 = im2.astype(np.float32)
#set up initial velocities
uInitial = np.zeros([im1.shape[0],im1.shape[1]])
vInitial = np.zeros([im1.shape[0],im1.shape[1]])
# Set initial value for the flow vectors
U = uInitial
V = vInitial
# Estimate derivatives
[fx, fy, ft] = computeDerivatives(im1, im2)
if verbose:
plotderiv(fx,fy,ft)
# print(fx[100,100],fy[100,100],ft[100,100])
# Iteration to reduce error
for _ in range(Niter):
#%% Compute local averages of the flow vectors
uAvg = filter2(U, HSKERN)
vAvg = filter2(V, HSKERN)
#%% common part of update step
der = (fx*uAvg + fy*vAvg + ft) / (alpha**2 + fx**2 + fy**2)
#%% iterative step
U = uAvg - fx * der
V = vAvg - fy * der
return U,V
评论列表
文章目录