-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathframe_interp.py
157 lines (135 loc) · 6.51 KB
/
frame_interp.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import numpy as np
from pyPyrTools import SCFpyr
from skimage import color
from skimage import transform
def decompose(img, ht, n_orientations, t_width, scale, n_scales, xp=np):
lab = xp.array(color.rgb2lab(img)) / 255.
pyramids = {'pyramids': [], 'high_pass': [], 'low_pass': [], 'phase': [], 'amplitude': [], 'pind': 0}
for i in range(img.shape[-1]):
pyr = SCFpyr(lab[..., i], ht, n_orientations - 1, t_width, scale, n_scales, xp)
pyramids['pyramids'].append(pyr)
pyramids['high_pass'].append(pyr.pyrHigh())
pyramids['low_pass'].append(pyr.pyrLow())
pyramids['phase'].append([xp.angle(pyr.pyr[level]) for level in range(1, len(pyr.pyr) - 1)])
pyramids['amplitude'].append([xp.abs(pyr.pyr[level]) for level in range(1, len(pyr.pyr) - 1)])
return pyramids
def compute_phase_difference(L, R, *args):
xp = L['pyramids'][0].xp
phase_diff_out = []
for i in range(len(L['phase'])):
phase_diff = [xp.arctan2(xp.sin(R['phase'][i][j] - L['phase'][i][j]), xp.cos(R['phase'][i][j] - L['phase'][i][j]))
for j in range(len(L['phase'][i]))]
phase_diff_new = shift_correction(phase_diff, L['pyramids'][i], *args)
unwrapped_phase_diff = []
for j in range(len(phase_diff_new)):
unwrapped_phase_diff.append(unwrap(xp.stack([phase_diff_new[j], list(phase_diff)[j]], 0),
xp=L['pyramids'][0].xp)[0])
phase_diff_out.append(unwrapped_phase_diff)
return phase_diff_out
def shift_correction(pyr, pyramid, *args):
n_high_elems = pyramid.pyrSize[0]
n_low_elems = pyramid.pyrSize[-1]
corrected_pyr = list(pyr)
corrected_pyr.insert(0, np.zeros(n_high_elems))
corrected_pyr.append(np.zeros(n_low_elems))
n_levels = pyramid.spyrHt()
n_bands = pyramid.numBands()
for level in range(n_levels - 1, -1, -1):
corrected_level = correct_level(corrected_pyr, pyramid, level, *args)
start_ind = 1 + n_bands * level
corrected_pyr[start_ind:start_ind+n_bands] = corrected_level
corrected_pyr = corrected_pyr[1:len(corrected_pyr) - 1]
return corrected_pyr
def correct_level(pyr, pyramid, level, *args):
scale = args[0]
limit = args[1]
n_levels = pyramid.spyrHt()
n_bands = pyramid.numBands()
out_level = []
if level < n_levels - 1:
dims = pyramid.pyrSize[1+n_bands*level]
for band in range(n_bands):
index_lo = pyramid.bandIndex(level + 1, band)
low_level_small = pyr[index_lo]
if pyramid.xp.__name__ == 'numpy':
low_level = transform.resize(low_level_small, dims, mode='reflect').astype('float32')
else:
low_level = pyramid.xp.array(transform.resize(pyramid.xp.asnumpy(low_level_small),
dims, mode='reflect').astype('float32'))
index_hi = pyramid.bandIndex(level, band)
high_level = pyr[index_hi]
unwrapped = pyramid.xp.stack([low_level.reshape(-1) / scale, high_level.reshape(-1)], 0)
unwrapped = unwrap(unwrapped, xp=pyramid.xp)
high_level = unwrapped[1]
high_level = pyramid.xp.reshape(high_level, dims)
angle_diff = pyramid.xp.arctan2(pyramid.xp.sin(high_level-low_level/scale),
pyramid.xp.cos(high_level-low_level/scale))
to_fix = pyramid.xp.abs(angle_diff) > (np.pi / 2)
high_level[to_fix] = low_level[to_fix] / scale
if limit > 0:
to_fix = pyramid.xp.abs(high_level) > (limit * np.pi / scale ** (n_levels - level))
high_level[to_fix] = low_level[to_fix] / scale
out_level.append(high_level)
if level == n_levels - 1:
for band in range(n_bands):
index_lo = pyramid.bandIndex(level, band)
low_level = pyr[index_lo]
if limit > 0:
to_fix = pyramid.xp.abs(low_level) > (limit * np.pi / scale ** (n_levels - level))
low_level[to_fix] = 0.
out_level.append(low_level)
return out_level
def unwrap(p, cutoff=np.pi, xp=np):
def local_unwrap(p, cutoff):
dp = p[1] - p[0]
dps = xp.mod(dp + np.pi, 2 * np.pi) - np.pi
dps[xp.logical_and(dps == -np.pi, dp > 0)] = np.pi
dp_corr = dps - dp
dp_corr[xp.abs(dp) < cutoff] = 0.
p[1] += dp_corr
return p
shape = p.shape
p = xp.reshape(p, (shape[0], np.prod(shape[1:])))
q = local_unwrap(p, cutoff)
q = xp.reshape(q, shape)
return q
def interpolate_pyramid(L, R, phase_diff, alpha):
new_pyr = []
for i in range(len(phase_diff)):
new_pyr.append([])
high_pass = L['high_pass'][i] if alpha < 0.5 else R['high_pass'][i]
low_pass = (1 - alpha) * L['low_pass'][i] + alpha * R['low_pass'][i]
new_pyr[i].append(high_pass)
for k in range(len(R['phase'][i])):
new_phase = R['phase'][i][k] + (alpha - 1) * phase_diff[i][k]
new_amplitude = (1 - alpha) * L['amplitude'][i][k] + alpha * R['amplitude'][i][k]
mid_band = new_amplitude * np.e ** (1j * new_phase)
new_pyr[i].append(mid_band)
new_pyr[i].append(low_pass)
return new_pyr
def reconstruct_image(pyr):
xp = pyr['pyramids'][0].xp
out_img = xp.zeros((pyr['pyramids'][0].pyrSize[0][0], pyr['pyramids'][0].pyrSize[0][1], 3))
for i, pyr in enumerate(pyr['pyramids']):
out_img[..., i] = pyr.reconPyr('all', 'all')
if xp.__name__ == 'numpy':
out_img = color.lab2rgb(out_img * 255.)
else:
out_img = color.lab2rgb(xp.asnumpy(out_img) * 255.)
return out_img
def interpolate_frame(img1, img2, n_frames=1, n_orientations=8, t_width=1, scale=0.5, limit=.4, min_size=15,
max_levels=23, xp=np):
h, w, l = img1.shape
n_scales = min(np.ceil(np.log2(min((h, w))) / np.log2(1. / scale) -
(np.log2(min_size) / np.log2(1 / scale))).astype('int'), max_levels)
step = 1. / (n_frames + 1)
L = decompose(img1, n_scales, n_orientations, t_width, scale, n_scales, xp)
R = decompose(img2, n_scales, n_orientations, t_width, scale, n_scales, xp)
phase_diff = compute_phase_difference(L, R, scale, limit)
new_frames = []
for j in range(n_frames):
new_pyr = interpolate_pyramid(L, R, phase_diff, step * (j + 1))
for i, pyr in enumerate(L['pyramids']):
pyr.pyr = new_pyr[i]
new_frames.append(reconstruct_image(L))
return new_frames