-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathampPhase.py
333 lines (269 loc) · 12.1 KB
/
ampPhase.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import cv2
import numpy as np
from scipy.signal import butter
from concurrent.futures import ThreadPoolExecutor
from tqdm import tqdm
import time
from tkinter.filedialog import askopenfilename
import os
from os.path import isfile, join
import sys
def ComputeLaplacianPyramid(frame, max_levels):
G = frame.copy()
gpA = [G]
for i in range(max_levels):
G = cv2.pyrDown(G)
gpA.append(G)
lpA = [gpA[-1]]
for i in range(len(gpA)-1, 0, -1):
GE = cv2.pyrUp(gpA[i])
size = (gpA[i-1].shape[1], gpA[i-1].shape[0])
GE = cv2.resize(GE, size)
L = cv2.subtract(gpA[i-1], GE)
lpA.append(L)
lpA.reverse()
return lpA
def ComputeRieszPyramid(frame, max_levels):
laplacian_pyramid = ComputeLaplacianPyramid(frame, max_levels)
number_of_levels = len(laplacian_pyramid) - 1
kernel_x = np.array([[0.0, 0.0, 0.0],
[0.5, 0.0, -0.5],
[0.0, 0.0, 0.0]], dtype=np.float32)
kernel_y = np.array([[0.0, 0.5, 0.0],
[0.0, 0.0, 0.0],
[0.0, -0.5, 0.0]], dtype=np.float32)
riesz_x = []
riesz_y = []
for k in range(number_of_levels):
rx = cv2.filter2D(laplacian_pyramid[k], -1, kernel_x, borderType=cv2.BORDER_REFLECT)
ry = cv2.filter2D(laplacian_pyramid[k], -1, kernel_y, borderType=cv2.BORDER_REFLECT)
riesz_x.append(rx)
riesz_y.append(ry)
return laplacian_pyramid, riesz_x, riesz_y
def ComputePhaseDifferenceAndAmplitude(current_real, current_x, current_y, previous_real, previous_x, previous_y):
q_conj_prod_real = current_real * previous_real + current_x * previous_x + current_y * previous_y
q_conj_prod_x = -current_real * previous_x + previous_real * current_x
q_conj_prod_y = -current_real * previous_y + previous_real * current_y
q_conj_prod_amplitude = np.sqrt(q_conj_prod_real ** 2 + q_conj_prod_x ** 2 + q_conj_prod_y ** 2) + 1e-8
phase_difference = np.arccos(np.clip(q_conj_prod_real / q_conj_prod_amplitude, -1, 1))
denom_orientation = np.sqrt(q_conj_prod_x ** 2 + q_conj_prod_y ** 2) + 1e-8
cos_orientation = q_conj_prod_x / denom_orientation
sin_orientation = q_conj_prod_y / denom_orientation
phase_difference_cos = phase_difference * cos_orientation
phase_difference_sin = phase_difference * sin_orientation
amplitude = np.sqrt(q_conj_prod_amplitude)
return phase_difference_cos, phase_difference_sin, amplitude
def IIRTemporalFilter(B, A, phase, register0, register1):
temporally_filtered_phase = B[0] * phase + register0
register0_new = B[1] * phase + register1 - A[1] * temporally_filtered_phase
register1_new = B[2] * phase - A[2] * temporally_filtered_phase
return temporally_filtered_phase, register0_new, register1_new
def AmplitudeWeightedBlur(temporally_filtered_phase, amplitude, blur_kernel):
numerator = cv2.filter2D(temporally_filtered_phase * amplitude, -1, blur_kernel, borderType=cv2.BORDER_REFLECT)
denominator = cv2.filter2D(amplitude, -1, blur_kernel, borderType=cv2.BORDER_REFLECT) + 1e-8
spatially_smooth_temporally_filtered_phase = numerator / denominator
return spatially_smooth_temporally_filtered_phase
def PhaseShiftCoefficientRealPart(riesz_real, riesz_x, riesz_y, phase_cos, phase_sin):
phase_magnitude = np.sqrt(phase_cos ** 2 + phase_sin ** 2) + 1e-8
exp_phase_real = np.cos(phase_magnitude)
sin_phase_magnitude = np.sin(phase_magnitude)
exp_phase_x = phase_cos / phase_magnitude * sin_phase_magnitude
exp_phase_y = phase_sin / phase_magnitude * sin_phase_magnitude
result = exp_phase_real * riesz_real - exp_phase_x * riesz_x - exp_phase_y * riesz_y
return result
def CollapseLaplacianPyramid(pyramid):
current = pyramid[-1]
for level in reversed(pyramid[:-1]):
upsampled = cv2.pyrUp(current)
size = (level.shape[1], level.shape[0])
upsampled = cv2.resize(upsampled, size)
current = upsampled + level
return current
def OnlineRieszVideoMagnification(amplification_factor, low_cutoff, high_cutoff, input_video_path, output_video_path):
cap = cv2.VideoCapture(input_video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
nyquist_frequency = fps / 2.0
temporal_filter_order = 1
low = low_cutoff / nyquist_frequency
high = high_cutoff / nyquist_frequency
B, A = butter(temporal_filter_order, [low, high], btype='bandpass')
B = B.astype(np.float32)
A = A.astype(np.float32)
gaussian_kernel_sd = 2
gaussian_kernel_size = int(gaussian_kernel_sd * 6 + 1)
gaussian_kernel_1d = cv2.getGaussianKernel(gaussian_kernel_size, gaussian_kernel_sd)
gaussian_kernel_2d = gaussian_kernel_1d * gaussian_kernel_1d.T
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))
ret, frame = cap.read()
if not ret:
print("Failed to read video")
return
# Remove grayscale conversion to retain color
frame = frame.astype(np.float32) / 255.0
max_levels = 4 # Adjust based on your needs
previous_laplacian_pyramid, previous_riesz_x, previous_riesz_y = ComputeRieszPyramid(frame, max_levels)
number_of_levels = len(previous_laplacian_pyramid) - 1
# Initialize phase and registers with the appropriate shape (including channels)
phase_cos = [{} for _ in range(number_of_levels)]
phase_sin = [{} for _ in range(number_of_levels)]
register0_cos = [{} for _ in range(number_of_levels)]
register1_cos = [{} for _ in range(number_of_levels)]
register0_sin = [{} for _ in range(number_of_levels)]
register1_sin = [{} for _ in range(number_of_levels)]
for k in range(number_of_levels):
size = previous_laplacian_pyramid[k].shape
phase_cos[k] = np.zeros(size, dtype=np.float32)
phase_sin[k] = np.zeros(size, dtype=np.float32)
register0_cos[k] = np.zeros(size, dtype=np.float32)
register1_cos[k] = np.zeros(size, dtype=np.float32)
register0_sin[k] = np.zeros(size, dtype=np.float32)
register1_sin[k] = np.zeros(size, dtype=np.float32)
# Initialize the progress bar
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
pbar = tqdm(total=total_frames - 1, desc='Processing frames', unit='frame', ncols=80)
while True:
ret, frame = cap.read()
if not ret:
break
# Remove grayscale conversion to retain color
current_frame = frame.astype(np.float32) / 255.0
current_laplacian_pyramid, current_riesz_x, current_riesz_y = ComputeRieszPyramid(current_frame, max_levels)
motion_magnified_laplacian_pyramid = [None] * len(current_laplacian_pyramid)
# Prepare data for parallel processing
args_list = []
for k in range(number_of_levels):
args_list.append((
k,
current_laplacian_pyramid[k],
current_riesz_x[k],
current_riesz_y[k],
previous_laplacian_pyramid[k],
previous_riesz_x[k],
previous_riesz_y[k],
phase_cos[k],
phase_sin[k],
register0_cos[k],
register1_cos[k],
register0_sin[k],
register1_sin[k],
B, A,
amplification_factor,
gaussian_kernel_2d
))
# Use ThreadPoolExecutor to parallelize the processing of pyramid levels
with ThreadPoolExecutor() as executor:
results = list(executor.map(process_pyramid_level, args_list))
# Collect results
for res in results:
k = res['level']
motion_magnified_laplacian_pyramid[k] = res['motion_magnified_coeff']
# Update phase and registers
phase_cos[k] = res['phase_cos']
phase_sin[k] = res['phase_sin']
register0_cos[k] = res['register0_cos']
register1_cos[k] = res['register1_cos']
register0_sin[k] = res['register0_sin']
register1_sin[k] = res['register1_sin']
motion_magnified_laplacian_pyramid[number_of_levels] = current_laplacian_pyramid[number_of_levels]
motion_magnified_frame = CollapseLaplacianPyramid(motion_magnified_laplacian_pyramid)
motion_magnified_frame = np.clip(motion_magnified_frame, 0, 1)
motion_magnified_frame_uint8 = (motion_magnified_frame * 255).astype(np.uint8)
# Write the color frame directly
out.write(motion_magnified_frame_uint8)
previous_laplacian_pyramid = current_laplacian_pyramid
previous_riesz_x = current_riesz_x
previous_riesz_y = current_riesz_y
pbar.update(1) # Update the progress bar
pbar.close() # Close the progress bar
cap.release()
out.release()
cv2.destroyAllWindows()
def process_pyramid_level(args):
(
k,
current_laplacian,
current_riesz_x,
current_riesz_y,
previous_laplacian,
previous_riesz_x,
previous_riesz_y,
phase_cos_k,
phase_sin_k,
register0_cos_k,
register1_cos_k,
register0_sin_k,
register1_sin_k,
B, A,
amplification_factor,
gaussian_kernel_2d
) = args
phase_difference_cos, phase_difference_sin, amplitude = ComputePhaseDifferenceAndAmplitude(
current_laplacian,
current_riesz_x,
current_riesz_y,
previous_laplacian,
previous_riesz_x,
previous_riesz_y
)
phase_cos_k += phase_difference_cos
phase_sin_k += phase_difference_sin
phase_filtered_cos, register0_cos_k, register1_cos_k = IIRTemporalFilter(B, A, phase_cos_k, register0_cos_k, register1_cos_k)
phase_filtered_sin, register0_sin_k, register1_sin_k = IIRTemporalFilter(B, A, phase_sin_k, register0_sin_k, register1_sin_k)
phase_filtered_cos = AmplitudeWeightedBlur(phase_filtered_cos, amplitude, gaussian_kernel_2d)
phase_filtered_sin = AmplitudeWeightedBlur(phase_filtered_sin, amplitude, gaussian_kernel_2d)
phase_magnified_filtered_cos = amplification_factor * phase_filtered_cos
phase_magnified_filtered_sin = amplification_factor * phase_filtered_sin
result = PhaseShiftCoefficientRealPart(
current_laplacian,
current_riesz_x,
current_riesz_y,
phase_magnified_filtered_cos,
phase_magnified_filtered_sin
)
return {
'level': k,
'motion_magnified_coeff': result,
'phase_cos': phase_cos_k,
'phase_sin': phase_sin_k,
'register0_cos': register0_cos_k,
'register1_cos': register1_cos_k,
'register0_sin': register0_sin_k,
'register1_sin': register1_sin_k
}
if __name__ == "__main__":
amplification_factor = 20 # Adjust as needed
low_cutoff = 0.4 # In Hz
high_cutoff = 3.0 # In Hz
input_video_path = askopenfilename(title="Select the first video file")
if not input_video_path:
print("No first video file selected.")
sys.exit(1)
_, tail = os.path.split(input_video_path)
tail = os.path.splitext(tail)[0]
try:
fullIndex = tail.index('full')+4
except:
fullIndex = 0
tail = tail[fullIndex:]
i = 0
while os.path.exists(f"amp_{tail}_{i}.mp4"):
i += 1
output_video_path = f"amp_{tail}_{i}.mp4"
print(output_video_path)
start_time = time.time()
OnlineRieszVideoMagnification(
amplification_factor,
low_cutoff,
high_cutoff,
input_video_path,
output_video_path
)
end_time = time.time()
# Calculate the execution time
execution_time = end_time - start_time
# Print the result
print(f"Script executed in: {execution_time:.2f} seconds")