-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_stable.py
130 lines (110 loc) · 3.7 KB
/
video_stable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import cv2
import numpy as np
import os
import pickle
# from matplotlib import pyplot as plt
# params for ShiTomasi corner detection
feature_params = dict( maxCorners = 200,
qualityLevel = 0.1,
minDistance = 30)#,
# 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))
def compute_trajectory(filename):
if 'mp4' in filename:
pkl = filename.replace('mp4', 'pkl')
else:
pkl = filename.replace('avi', 'pkl')
if os.path.exists(pkl):
transform = pickle.load(open(pkl, 'rb'))
return transform
cap = cv2.VideoCapture(filename)
ret, frame_prev = cap.read()
frame_prev = cv2.cvtColor(frame_prev, cv2.COLOR_BGR2GRAY)
point_prev = cv2.goodFeaturesToTrack(frame_prev, mask = None, **feature_params)
transform = []
while True:
ret,frame = cap.read()
if not ret:
break
frame_cur = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
point_cur, status, err = cv2.calcOpticalFlowPyrLK(frame_prev, frame_cur, point_prev, None, **lk_params)
good_cur = point_cur[status==1]
good_prev = point_prev[status==1]
trans = cv2.estimateRigidTransform(good_prev, good_cur, False)
if trans is None:
transform.append(transform[-1])
else:
transform.append(trans)
frame_prev = frame_cur
point_prev = cv2.goodFeaturesToTrack(frame_prev, mask = None, **feature_params)
cap.release()
transform = np.array(transform)
pickle.dump(transform, open(pkl, 'wb'))
return transform
# def plot(y, name):
# x = np.arange(y.shape[0])
# fig = plt.figure(figsize=(64,32))
# plt.plot(x, y)
# plt.savefig(name+'.jpg')
def smooth(transform, r=30):
x = transform[:,0,2]
y = transform[:,1,2]
a = np.arctan2(transform[:,1,0], transform[:,0,0])
da = smooth_single(a, r)
dx = smooth_single(x, r)
dy = smooth_single(y, r)
smoothed = np.zeros_like(transform)
smoothed[:,0,0] = np.cos(da)
smoothed[:,0,1] = -np.sin(da)
smoothed[:,1,0] = np.sin(da)
smoothed[:,1,1] = np.cos(da)
smoothed[:,0,2] = dx
smoothed[:,1,2] = dy
return smoothed
def smooth_single(dx, r=30):
'''
Args:
dx: diff between consecutive frames
r: moving average size
Return:
d: smoothed dx
x: original trajectory
avg: smoothed trajectory
'''
x = np.cumsum(dx)
x_pad = np.pad(x, (r, r), mode='edge')
avg = np.convolve(x_pad, np.ones((2*r+1,))/(2*r+1), mode='valid')
d = dx + avg - x
return d
def show_and_write(filename, T, outname='out'):
outname += '.mp4'
cap = cv2.VideoCapture(filename)
ret, frame = cap.read()
h, w = frame.shape[:2]
out = cv2.VideoWriter(outname, 0x00000021, 30.0, (w,h))
for i in range(T.shape[0]-1):
if not ret:
break
frame_cur = cv2.warpAffine(frame, T[i], (w, h))
out.write(frame_cur)
canvas = np.zeros((h, 2*w+10, 3))
canvas[:, :w,...] = frame
canvas[:, w+10:, ...] = frame_cur
canvas = cv2.resize(canvas, ((2*w+10)//2, h//2))
cv2.imshow('compare', canvas.astype(np.uint8))
k = cv2.waitKey(30) & 0xff
if k == 27:
break
ret, frame = cap.read()
cap.release()
out.release()
def main(filename):
out_name = filename.split('.')[0]+'_stabled'
transform = compute_trajectory(filename)
smoothed = smooth(transform, 30)
show_and_write(filename, smoothed, out_name)
if __name__ == '__main__':
main('hippo.mp4')