opt_flow.py demo
This commit is contained in:
@@ -1,38 +1,83 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import cv2, cv
|
import cv2, cv
|
||||||
import video
|
import video
|
||||||
import sys
|
|
||||||
|
|
||||||
|
help_message = '''
|
||||||
|
USAGE: opt_flow.py [<video_source>]
|
||||||
|
|
||||||
|
Keys:
|
||||||
|
1 - toggle HSV flow visualization
|
||||||
|
2 - toggle glitch
|
||||||
|
|
||||||
|
'''
|
||||||
|
|
||||||
|
def draw_flow(img, flow, step=16):
|
||||||
|
h, w = img.shape[:2]
|
||||||
|
y, x = np.mgrid[step/2:h:step, step/2:w:step].reshape(2,-1)
|
||||||
|
fx, fy = flow[y,x].T
|
||||||
|
lines = np.vstack([x, y, x+fx, y+fy]).T.reshape(-1, 2, 2)
|
||||||
|
lines = np.int32(lines + 0.5)
|
||||||
|
vis = cv2.cvtColor(img, cv.CV_GRAY2BGR)
|
||||||
|
cv2.polylines(vis, lines, 0, (0, 255, 0))
|
||||||
|
for (x1, y1), (x2, y2) in lines:
|
||||||
|
cv2.circle(vis, (x1, y1), 1, (0, 255, 0), -1)
|
||||||
|
return vis
|
||||||
|
|
||||||
|
def draw_hsv(flow):
|
||||||
|
h, w = flow.shape[:2]
|
||||||
|
fx, fy = flow[:,:,0], flow[:,:,1]
|
||||||
|
ang = np.arctan2(fy, fx) + np.pi
|
||||||
|
v = np.sqrt(fx*fx+fy*fy)
|
||||||
|
hsv = np.zeros((h, w, 3), np.uint8)
|
||||||
|
hsv[...,0] = np.rad2deg(ang)/2
|
||||||
|
hsv[...,1] = 255
|
||||||
|
hsv[...,2] = np.minimum(v*4, 255)
|
||||||
|
bgr = cv2.cvtColor(hsv, cv.CV_HSV2BGR)
|
||||||
|
return bgr
|
||||||
|
|
||||||
|
def warp_flow(img, flow):
|
||||||
|
h, w = flow.shape[:2]
|
||||||
|
flow = -flow
|
||||||
|
flow[:,:,0] += np.arange(w)
|
||||||
|
flow[:,:,1] += np.arange(h)[:,np.newaxis]
|
||||||
|
res = cv2.remap(img, flow, None, cv2.INTER_LINEAR)
|
||||||
|
return res
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import sys
|
||||||
|
print help_message
|
||||||
try: fn = sys.argv[1]
|
try: fn = sys.argv[1]
|
||||||
except: fn = video.presets['chess']
|
except: fn = video.presets['chess']
|
||||||
|
|
||||||
cam = video.create_capture(fn)
|
cam = video.create_capture(fn)
|
||||||
ret, prev = cam.read()
|
ret, prev = cam.read()
|
||||||
#prev = cv2.pyrDown(prev)
|
|
||||||
prevgray = cv2.cvtColor(prev, cv.CV_BGR2GRAY)
|
prevgray = cv2.cvtColor(prev, cv.CV_BGR2GRAY)
|
||||||
|
show_hsv = False
|
||||||
def draw_flow(img, flow, step):
|
show_glitch = False
|
||||||
h, w = img.shape[:2]
|
cur_glitch = prev.copy()
|
||||||
y, x = map(np.ravel, np.mgrid[step/2:h:step, step/2:w:step])
|
|
||||||
f = flow[y,x]
|
|
||||||
x1 = x + f[:,0]
|
|
||||||
y1 = y + f[:,1]
|
|
||||||
#lines = np.int32( np.vstack([x, y, x1, y1]).T )
|
|
||||||
vis = cv2.cvtColor(img, cv.CV_GRAY2BGR)
|
|
||||||
#print lines
|
|
||||||
#cv2.polylines(vis, lines, 0, (0, 255, 0))
|
|
||||||
for x_, y_, x1_, y1_ in np.int32(zip(x, y, x1, y1)):
|
|
||||||
cv2.line(vis, (x_, y_), (x1_, y1_), (0, 255, 0))
|
|
||||||
return vis
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
ret, img = cam.read()
|
ret, img = cam.read()
|
||||||
#img = cv2.pyrDown(img)
|
|
||||||
gray = cv2.cvtColor(img, cv.CV_BGR2GRAY)
|
gray = cv2.cvtColor(img, cv.CV_BGR2GRAY)
|
||||||
flow = cv2.calcOpticalFlowFarneback(prevgray, gray, 0.5, 3, 15, 3, 5, 1.2, 0)
|
flow = cv2.calcOpticalFlowFarneback(prevgray, gray, 0.5, 3, 15, 3, 5, 1.2, 0)
|
||||||
prevgray = gray
|
prevgray = gray
|
||||||
|
|
||||||
cv2.imshow('flow', draw_flow(gray, flow, 16))
|
cv2.imshow('flow', draw_flow(gray, flow))
|
||||||
if cv2.waitKey(5) == 27:
|
if show_hsv:
|
||||||
break
|
cv2.imshow('flow HSV', draw_hsv(flow))
|
||||||
|
if show_glitch:
|
||||||
|
cur_glitch = warp_flow(cur_glitch, flow)
|
||||||
|
cv2.imshow('glitch', cur_glitch)
|
||||||
|
|
||||||
|
ch = cv2.waitKey(5)
|
||||||
|
if ch == 27:
|
||||||
|
break
|
||||||
|
if ch == ord('1'):
|
||||||
|
show_hsv = not show_hsv
|
||||||
|
print 'HSV flow visualization is', ['off', 'on'][show_hsv]
|
||||||
|
if ch == ord('2'):
|
||||||
|
show_glitch = not show_glitch
|
||||||
|
if show_glitch:
|
||||||
|
cur_glitch = img.copy()
|
||||||
|
print 'glitch is', ['off', 'on'][show_glitch]
|
||||||
|
|
||||||
|
@@ -60,6 +60,7 @@ class Chess(VideoSynthBase):
|
|||||||
[0.0,0.0, 1.0]])
|
[0.0,0.0, 1.0]])
|
||||||
|
|
||||||
self.dist_coef = np.float64([-0.2, 0.1, 0, 0])
|
self.dist_coef = np.float64([-0.2, 0.1, 0, 0])
|
||||||
|
self.t = 0
|
||||||
|
|
||||||
def draw_quads(self, img, quads, color = (0, 255, 0)):
|
def draw_quads(self, img, quads, color = (0, 255, 0)):
|
||||||
img_quads = cv2.projectPoints(quads.reshape(-1, 3), self.rvec, self.tvec, self.K, self.dist_coef) [0]
|
img_quads = cv2.projectPoints(quads.reshape(-1, 3), self.rvec, self.tvec, self.K, self.dist_coef) [0]
|
||||||
@@ -68,7 +69,8 @@ class Chess(VideoSynthBase):
|
|||||||
cv2.fillConvexPoly(img, np.int32(q*4), color, cv2.CV_AA, shift=2)
|
cv2.fillConvexPoly(img, np.int32(q*4), color, cv2.CV_AA, shift=2)
|
||||||
|
|
||||||
def render(self, dst):
|
def render(self, dst):
|
||||||
t = clock()
|
t = self.t
|
||||||
|
self.t += 1.0/30.0
|
||||||
|
|
||||||
sx, sy = self.grid_size
|
sx, sy = self.grid_size
|
||||||
center = np.array([0.5*sx, 0.5*sy, 0.0])
|
center = np.array([0.5*sx, 0.5*sy, 0.0])
|
||||||
|
Reference in New Issue
Block a user