Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/python/opt_flow.py
16337 views
1
#!/usr/bin/env python
2
3
'''
4
example to show optical flow
5
6
USAGE: opt_flow.py [<video_source>]
7
8
Keys:
9
1 - toggle HSV flow visualization
10
2 - toggle glitch
11
12
Keys:
13
ESC - exit
14
'''
15
16
# Python 2/3 compatibility
17
from __future__ import print_function
18
19
import numpy as np
20
import cv2 as cv
21
import video
22
23
24
def draw_flow(img, flow, step=16):
25
h, w = img.shape[:2]
26
y, x = np.mgrid[step/2:h:step, step/2:w:step].reshape(2,-1).astype(int)
27
fx, fy = flow[y,x].T
28
lines = np.vstack([x, y, x+fx, y+fy]).T.reshape(-1, 2, 2)
29
lines = np.int32(lines + 0.5)
30
vis = cv.cvtColor(img, cv.COLOR_GRAY2BGR)
31
cv.polylines(vis, lines, 0, (0, 255, 0))
32
for (x1, y1), (_x2, _y2) in lines:
33
cv.circle(vis, (x1, y1), 1, (0, 255, 0), -1)
34
return vis
35
36
37
def draw_hsv(flow):
38
h, w = flow.shape[:2]
39
fx, fy = flow[:,:,0], flow[:,:,1]
40
ang = np.arctan2(fy, fx) + np.pi
41
v = np.sqrt(fx*fx+fy*fy)
42
hsv = np.zeros((h, w, 3), np.uint8)
43
hsv[...,0] = ang*(180/np.pi/2)
44
hsv[...,1] = 255
45
hsv[...,2] = np.minimum(v*4, 255)
46
bgr = cv.cvtColor(hsv, cv.COLOR_HSV2BGR)
47
return bgr
48
49
50
def warp_flow(img, flow):
51
h, w = flow.shape[:2]
52
flow = -flow
53
flow[:,:,0] += np.arange(w)
54
flow[:,:,1] += np.arange(h)[:,np.newaxis]
55
res = cv.remap(img, flow, None, cv.INTER_LINEAR)
56
return res
57
58
if __name__ == '__main__':
59
import sys
60
print(__doc__)
61
try:
62
fn = sys.argv[1]
63
except IndexError:
64
fn = 0
65
66
cam = video.create_capture(fn)
67
ret, prev = cam.read()
68
prevgray = cv.cvtColor(prev, cv.COLOR_BGR2GRAY)
69
show_hsv = False
70
show_glitch = False
71
cur_glitch = prev.copy()
72
73
while True:
74
ret, img = cam.read()
75
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
76
flow = cv.calcOpticalFlowFarneback(prevgray, gray, None, 0.5, 3, 15, 3, 5, 1.2, 0)
77
prevgray = gray
78
79
cv.imshow('flow', draw_flow(gray, flow))
80
if show_hsv:
81
cv.imshow('flow HSV', draw_hsv(flow))
82
if show_glitch:
83
cur_glitch = warp_flow(cur_glitch, flow)
84
cv.imshow('glitch', cur_glitch)
85
86
ch = cv.waitKey(5)
87
if ch == 27:
88
break
89
if ch == ord('1'):
90
show_hsv = not show_hsv
91
print('HSV flow visualization is', ['off', 'on'][show_hsv])
92
if ch == ord('2'):
93
show_glitch = not show_glitch
94
if show_glitch:
95
cur_glitch = img.copy()
96
print('glitch is', ['off', 'on'][show_glitch])
97
cv.destroyAllWindows()
98
99