LK光流(稀疏光流)
传统稠密光流算法:
Farneback算法
2003 - Gunner Farneback - Two-Frame Motion Estimation Based on Polynomial Expansion (多项式展开)(稠密光流)
flow = cv2.calcOpticalFlowFarneback(img0, img1, None, 0.5, 3, 15, 3, 5, 1.2, 0)
https://www.cnblogs.com/luofeiju/p/11971181.html
Deep Flow算法
of_estim = cv2.optflow.createOptFlow_DeepFlow()
flow = of_estim.calc(img0, img1, None)
TVL1算法
of_estim = cv2.optflow.DualTVL1OpticalFlow_create()
flow = of_estim.calc(img0, img1, None)
Simple Flow算法
of_estim = cv2.optflow.createOptFlow_SimpleFlow()
flow = of_estim.calc(img1, img0, None)
深度学习算法:
FlowNet
PWCNet
TOFlow
光流对齐:
warping代码例子
https://subscription.packtpub.com/book/application_development/9781785283932/1/ch01lvl1sec16/image-warping
import numpy as np
def AdjustIndex(img, border):
img[img<0] = 0
img[img>=border] = border - 1
return img
def alignImages(im1, flow):
height, width = im1.shape[0], im1.shape[1]
# index
u = np.arange(0, width)
ul = np.tile(u, (height, 1))
idx = ul + flow[:,:,0]
idx0 = np.floor(idx) # 横坐标
idx1 = idx0 + 1
AdjustIndex(idx0, width)
AdjustIndex(idx1, width)
v = np.arange(0, height)
v = v.reshape(v.shape[0], 1)
vl = np.tile(v, (1, width))
idy = vl + flow[:,:,1]
idy0 = np.floor(idy) # 纵坐标
idy1 = idy0 + 1
AdjustIndex(idy0, height)
AdjustIndex(idy1, height)
# weight
w00 = (1 - (idy - idy0)) * (1 - (idx - idx0))
w01 = (1 - (idy - idy0)) * (idx - idx0)
w10 = (idy - idy0) * (1 - (idx - idx0))
w11 = (idy - idy0) * (idx - idx0)
# take value
f00 = np.zeros(im1.shape, dtype=im1.dtype)
f01 = np.zeros(im1.shape, dtype=im1.dtype)
f10 = np.zeros(im1.shape, dtype=im1.dtype)
f11 = np.zeros(im1.shape, dtype=im1.dtype)
for h in range(0, height):
for w in range(0, width):
f00[h, w] = im1[int(idy0[h, w]), int(idx0[h, w])]
f01[h, w] = im1[int(idy0[h, w]), int(idx1[h, w])]
f10[h, w] = im1[int(idy1[h, w]), int(idx0[h, w])]
f11[h, w] = im1[int(idy1[h, w]), int(idx1[h, w])]
# bilinear warping
align_im = np.zeros(im1.shape, dtype=im1.dtype)
for i in range(im1.shape[2]):
align_im[:,:,i] = w00 * f00[:,:,i] + w01 * f01[:,:,i] + w10 * f10[:,:,i] + w11 * f11[:,:,i] # 双线性插值
return align_im