# Optical Flow介绍与代码实现

## Optical Flow介绍与代码实现

#### 介绍

Optical flow 是一个概念, 描述的是一个相对于观测者的运动,这个运动是观测造成的,就是说,我们眼睛的目光和物体保持相对静止就没有光流运动啦.

What is motion?"对两个观点进行了辩论:

• 1, " motion is the physical movement of pixels, and therefore motion has to be measured in a physical way. "
• 2, " motion is human percept--motion is what we perceive in our brain, something we can sense and communicate."

image

If the ultimate goal of computer vision is to let the computer see what humans perceive, then it is certainly the right way to let humans teach computer how to see the world. Our human-assisted motion annotation serves exactly for this purpose. Indeed, we shall show you that (a) humans' annotations are very consistent, and (b) human's annotations are consistent with other ground-truth data.

#### 言归正传

(ux, vy) = (x2, y2) - (x1,y1)。

1981年，Horn和Schunck创造性地将二维速度场与灰度相联系，引入光流约束方程，得到光流计算的基本算法。人们基于不同的理论基础提出各种光流计算方法，算法性能各有不同。Barron等人对多种光流计算技术进行了总结，按照理论基础与数学方法的区别把它们分成四种：基于梯度的方法、基于匹配的方法、基于能量的方法、基于相位的方法。近年来神经动力学方法也颇受学者重视。

#### 几种测定方法

openCV 的API是 calcOpticalFlowPyrLK.

• The Gunnar-Farneback optical flow

opencv 的API 是 calcOpticalFlowFarneback.

• block matching method

• Horn–Schunck method
用Horn-Schunck 的算法计算稠密光流。OpenCv的API是 CalcOpticalFlowHS.
• SimpleFlow

OpenCv的API是 calcOpticalFlowSF.

#### 代码演示

##### python
#!/usr/bin/python

import numpy as np
import cv2

# opencv-3.1.0/samples/data/768x576.avi
cap = cv2.VideoCapture('768x576.avi')

# params for ShiTomasi corner detection
feature_params = dict( maxCorners = 100,
qualityLevel = 0.3,
minDistance = 7,
blockSize = 7 )

# Parameters for lucas kanade optical flow
lk_params = dict( winSize  = (15,15),
maxLevel = 2,
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))

# Create some random colors
color = np.random.randint(0,255,(100,3))

# Take first frame and find corners in it
ret, old_frame = cap.read()
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
p0 = cv2.goodFeaturesToTrack(old_gray, mask = None, **feature_params)

# Create a mask image for drawing purposes

while(1):
if ret is True:
print ret
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# calculate optical flow
p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)

# Select good points
good_new = p1[st==1]
good_old = p0[st==1]

# draw the tracks
for i,(new,old) in enumerate(zip(good_new,good_old)):
a,b = new.ravel()
c,d = old.ravel()
frame = cv2.circle(frame,(a,b),5,color[i].tolist(),-1)

cv2.imshow('frame',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break

# Now update the previous frame and previous points
old_gray = frame_gray.copy()
p0 = good_new.reshape(-1,1,2)
else:
break

cv2.destroyAllWindows()
cap.release()



#### 参考文献

Application of local optical flow methods to high-velocity free-surface flows:
Validation and application to stepped chutes ：http://staff.civil.uq.edu.au/h.chanson/reprints/Zhang_Chanson_etfs_2018.pdf (介绍了主流的几种光流算法)

https://blog.csdn.net/zouxy09/article/details/8683859（光流Optical Flow介绍与OpenCV实现）

#### 最后的最后

|
4月前
|

Flow
Flow
134 6
|

Flow 学习笔记
Flow 学习笔记
109 0
|

optical flow

492 0
|

compare hdfs & oss files
111 0
|

Flow file 生成器2 | 学习笔记

101 0
|

1. 回顾并总结了图的基本概念。 2. 学习思考算法实现的代码思路--Node2Vec的实现以及RandomWalk的实现。 3. 对源码阅读能力的提升。
114 0
hook+ts业务开发思路2-完成input组件的编写
hook+ts业务开发思路2-完成input组件的编写
82 0
flow
flow
83 0
|

271 0
|
Arthas 缓存 测试技术
Project Reactor 深度解析 - 2. 响应式编程调试，FLow的概念设计以及实现（上）
Project Reactor 深度解析 - 2. 响应式编程调试，FLow的概念设计以及实现（上）
476 0