-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathphase_utils.py
More file actions
175 lines (139 loc) · 5.26 KB
/
phase_utils.py
File metadata and controls
175 lines (139 loc) · 5.26 KB
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
"""
Utils for Phase Based Video Magnification
Isaac Berrios
January 2024
"""
import os
from PIL import Image
from glob import glob
import numpy as np
import cv2
import torch
from scipy import signal
## ==========================================================================================
## Color spaces
def rgb2yiq(rgb):
""" Converts an RGB image to YIQ using FCC NTSC format.
This is a numpy version of the colorsys implementation
https://github.com/python/cpython/blob/main/Lib/colorsys.py
Inputs:
rgb - (N,M,3) rgb image
Outputs
yiq - (N,M,3) YIQ image
"""
# compute Luma Channel
y = rgb @ np.array([[0.30], [0.59], [0.11]])
# subtract y channel from red and blue channels
rby = rgb[:, :, (0,2)] - y
i = np.sum(rby * np.array([[[0.74, -0.27]]]), axis=-1)
q = np.sum(rby * np.array([[[0.48, 0.41]]]), axis=-1)
yiq = np.dstack((y.squeeze(), i, q))
return yiq
def bgr2yiq(bgr):
""" Coverts a BGR image to float32 YIQ """
# get normalized YIQ frame
rgb = np.float32(cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB))
yiq = rgb2yiq(rgb)
return yiq
def yiq2rgb(yiq):
""" Converts a YIQ image to RGB.
Inputs:
yiq - (N,M,3) YIQ image
Outputs:
rgb - (N,M,3) rgb image
"""
r = yiq @ np.array([1.0, 0.9468822170900693, 0.6235565819861433])
g = yiq @ np.array([1.0, -0.27478764629897834, -0.6356910791873801])
b = yiq @ np.array([1.0, -1.1085450346420322, 1.7090069284064666])
rgb = np.clip(np.dstack((r, g, b)), 0, 1)
return rgb
## ==========================================================================================
## Video Utils
def get_video(video_path, scale_factor, colorspace_func=lambda x: x):
""" Obtains frames from input video path
Inputs:
video_path - path to video
scale_factor - scale factor for frame sizes
colorspace_func - function to map default BGR to
Outputs:
frames - extracted video frames in desired colorspace and scale size
fs - video sample rate
"""
frames = [] # frames for processing
cap = cv2.VideoCapture(video_path)
# video sampling rate
fs = cap.get(cv2.CAP_PROP_FPS)
idx = 0
while(cap.isOpened()):
ret, frame = cap.read()
# if frame is read correctly ret is True
if not ret:
break
if idx == 0:
og_h, og_w, _ = frame.shape
w = int(og_w*scale_factor)
h = int(og_h*scale_factor)
# convert normalized uint8 BGR to the desired color space
frame = colorspace_func(np.float32(frame/255))
# append resized frame
frames.append(cv2.resize(frame, (w, h)))
idx += 1
cap.release()
cv2.destroyAllWindows()
del cap
return frames, fs
## ==========================================================================================
## GIF utils
def create_gif_from_images(save_path, image_path, ext):
''' creates a GIF from a folder of images
Inputs:
save_path (str) - path to save GIF
image_path (str) - path where images are located
ext (str) - extension of the images
Outputs:
None
Update:
Add functionality for multiple extensions
'''
image_paths = sorted(glob(os.path.join(image_path, f'*.{ext}')))
pil_images = [Image.open(im_path ) for im_path in image_paths]
pil_images[0].save(save_path, format='GIF', append_images=pil_images,
save_all=True, duration=45, loop=0)
def create_gif_from_numpy(save_path, images):
''' creates a GIF from numpy images
Inputs:
save_path (str) - path to save GIF
image_path (str) - path where images are located
ext (str) - extension of the images
Outputs:
None
Update:
Add functionality for multiple extensions
'''
pil_images = [Image.fromarray(img) for img in images]
pil_images[0].save(save_path, format='GIF', append_images=pil_images,
save_all=True, duration=45, loop=0)
## ==========================================================================================
## Misc utils
def get_fft2_batch(tensor_in):
return torch.fft.fftshift(torch.fft.fft2(tensor_in, dim=(1, 2)), dim=(1, 2)).type(torch.complex64)
def bandpass_filter(freq_lo, freq_hi, fs, num_taps, device):
""" Obtains Frequency Domain Transfer Function for Band pass filter
Inputs:
freq_lo
freq_hi
fs
num_frames - number of taps for filter
device - CUDA or CPU
Outputs:
transfer_function - frequency domain transfer function
"""
freq_lo = freq_lo / fs * 2
freq_hi = freq_hi / fs * 2
bandpass = signal.firwin(numtaps=num_taps,
cutoff=[freq_lo, freq_hi],
pass_zero=False)
bandpass = torch.tensor(bandpass).to(device)
transfer_function = torch.fft.fft(torch.fft.ifftshift(bandpass)).type(torch.complex64)
transfer_function = torch.tile(transfer_function, [1, 1, 1, 1]).permute(0, 3, 1, 2)
return transfer_function