• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3import numpy as np
4import cv2
5import video
6
7help_message = '''
8USAGE: opt_flow.py [<video_source>]
9
10Keys:
11 1 - toggle HSV flow visualization
12 2 - toggle glitch
13
14'''
15
16def draw_flow(img, flow, step=16):
17    h, w = img.shape[:2]
18    y, x = np.mgrid[step/2:h:step, step/2:w:step].reshape(2,-1)
19    fx, fy = flow[y,x].T
20    lines = np.vstack([x, y, x+fx, y+fy]).T.reshape(-1, 2, 2)
21    lines = np.int32(lines + 0.5)
22    vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
23    cv2.polylines(vis, lines, 0, (0, 255, 0))
24    for (x1, y1), (x2, y2) in lines:
25        cv2.circle(vis, (x1, y1), 1, (0, 255, 0), -1)
26    return vis
27
28def draw_hsv(flow):
29    h, w = flow.shape[:2]
30    fx, fy = flow[:,:,0], flow[:,:,1]
31    ang = np.arctan2(fy, fx) + np.pi
32    v = np.sqrt(fx*fx+fy*fy)
33    hsv = np.zeros((h, w, 3), np.uint8)
34    hsv[...,0] = ang*(180/np.pi/2)
35    hsv[...,1] = 255
36    hsv[...,2] = np.minimum(v*4, 255)
37    bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
38    return bgr
39
40def warp_flow(img, flow):
41    h, w = flow.shape[:2]
42    flow = -flow
43    flow[:,:,0] += np.arange(w)
44    flow[:,:,1] += np.arange(h)[:,np.newaxis]
45    res = cv2.remap(img, flow, None, cv2.INTER_LINEAR)
46    return res
47
48if __name__ == '__main__':
49    import sys
50    print help_message
51    try:
52        fn = sys.argv[1]
53    except:
54        fn = 0
55
56    cam = video.create_capture(fn)
57    ret, prev = cam.read()
58    prevgray = cv2.cvtColor(prev, cv2.COLOR_BGR2GRAY)
59    show_hsv = False
60    show_glitch = False
61    cur_glitch = prev.copy()
62
63    while True:
64        ret, img = cam.read()
65        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
66        flow = cv2.calcOpticalFlowFarneback(prevgray, gray, None, 0.5, 3, 15, 3, 5, 1.2, 0)
67        prevgray = gray
68
69        cv2.imshow('flow', draw_flow(gray, flow))
70        if show_hsv:
71            cv2.imshow('flow HSV', draw_hsv(flow))
72        if show_glitch:
73            cur_glitch = warp_flow(cur_glitch, flow)
74            cv2.imshow('glitch', cur_glitch)
75
76        ch = 0xFF & cv2.waitKey(5)
77        if ch == 27:
78            break
79        if ch == ord('1'):
80            show_hsv = not show_hsv
81            print 'HSV flow visualization is', ['off', 'on'][show_hsv]
82        if ch == ord('2'):
83            show_glitch = not show_glitch
84            if show_glitch:
85                cur_glitch = img.copy()
86            print 'glitch is', ['off', 'on'][show_glitch]
87    cv2.destroyAllWindows()
88