-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
283 lines (226 loc) · 8.74 KB
/
utils.py
File metadata and controls
283 lines (226 loc) · 8.74 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
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
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams as mpl_param
from PIL import Image
import glob
import os
import cv2
IMAGETYPES = ('*.bmp', '*.png', '*.jpg', '*.jpeg', '*.tif') # Supported image types
PI = math.pi
mpl_param["figure.dpi"] = 500
def propagate(input_wave, dist, pxsize, wavlen):
# calculate parameters
k = 2*PI/wavlen
n1 = input_wave.shape[0]
n2 = input_wave.shape[1]
# define meshgrid
k1 = PI/pxsize*torch.linspace(-1, 1-2/n1, n1)
k2 = PI/pxsize*torch.linspace(-1, 1-2/n2, n2)
kk1, kk2 = torch.meshgrid(k1, k2, indexing='ij')
# circular convolution via FFTs
output_wave = torch.fft.ifft2(torch.fft.ifftshift(
torch.exp(1j*dist*torch.sqrt(k**2 - kk1**2 - kk2**2))
* torch.fft.fftshift(torch.fft.fft2(input_wave))))
return output_wave
def cropimg(x, cropsize):
if cropsize > 0:
y = x[cropsize:-cropsize,cropsize:-cropsize]
else:
y = x
return y
def padimg(x, padsize, padval=0):
y = F.pad(x, (padsize,padsize,padsize,padsize), mode='constant', value=padval)
return y
def imshift(x, shift):
n1, n2 = x.shape[0], x.shape[1]
f1 = torch.linspace(-n1/2,n1/2-1,n1)
f2 = torch.linspace(-n2/2,n2/2-1,n2)
u1, u2 = torch.meshgrid(f1, f2, indexing='ij')
y = torch.fft.ifft2(torch.fft.fftshift(
torch.exp(-1j*2*PI*(shift[0]*u1/n1 + shift[1]*u2/n2))
* torch.fft.fftshift(torch.fft.fft2(x))))
return y
def load(path):
"""Load PIL image."""
img = Image.open(path)
return img
def load_image(path, imsize=-1):
"""Load an image and resize to a cpecific size.
Args:
path: path to image
imsize: tuple or scalar with dimensions; -1 for `no resize`
"""
img = load(path)
if isinstance(imsize, int):
imsize = (imsize, imsize)
if imsize[0]!= -1 and img.size != imsize:
if imsize[0] > img.size[0]:
img = img.resize(imsize, Image.BICUBIC)
else:
img = img.resize(imsize, Image.LANCZOS)
return torch.tensor(np.asarray(img)/255.)
def open_sequence(seq_dir, gray_mode, expand_if_needed=False, max_num_fr=100):
r""" Opens a sequence of images and expands it to even sizes if necesary
Args:
fpath: string, path to image sequence
gray_mode: boolean, True indicating if images is to be open are in grayscale mode
expand_if_needed: if True, the spatial dimensions will be expanded if
size is odd
expand_axis0: if True, output will have a fourth dimension
max_num_fr: maximum number of frames to load
Returns:
seq: array of dims [num_frames, C, H, W], C=1 grayscale or C=3 RGB, H and W are even.
The image gets normalized gets normalized to the range [0, 1].
expanded_h: True if original dim H was odd and image got expanded in this dimension.
expanded_w: True if original dim W was odd and image got expanded in this dimension.
"""
# Get ordered list of filenames
files = get_imagenames(seq_dir)
seq_list = []
# print("\tOpen sequence in folder: ", seq_dir)
for fpath in files[0:max_num_fr]:
img, expanded_h, expanded_w = open_image(fpath,\
gray_mode=gray_mode,\
expand_if_needed=expand_if_needed,\
expand_axis0=False)
seq_list.append(img)
seq = np.stack(seq_list, axis=0)
return seq, expanded_h, expanded_w
def get_imagenames(seq_dir, pattern=None):
""" Get ordered list of filenames
"""
files = []
for typ in IMAGETYPES:
files.extend(glob.glob(os.path.join(seq_dir, typ)))
# filter filenames
if not pattern is None:
ffiltered = []
ffiltered = [f for f in files if pattern in os.path.split(f)[-1]]
files = ffiltered
del ffiltered
# sort filenames alphabetically
files.sort(key=lambda f: int(''.join(filter(str.isdigit, f))))
return files
def open_image(fpath, gray_mode, expand_if_needed=False, expand_axis0=True, normalize_data=True):
r""" Opens an image and expands it if necesary
Args:
fpath: string, path of image file
gray_mode: boolean, True indicating if image is to be open
in grayscale mode
expand_if_needed: if True, the spatial dimensions will be expanded if
size is odd
expand_axis0: if True, output will have a fourth dimension
Returns:
img: image of dims NxCxHxW, N=1, C=1 grayscale or C=3 RGB, H and W are even.
if expand_axis0=False, the output will have a shape CxHxW.
The image gets normalized gets normalized to the range [0, 1].
expanded_h: True if original dim H was odd and image got expanded in this dimension.
expanded_w: True if original dim W was odd and image got expanded in this dimension.
"""
if not gray_mode:
# Open image as a CxHxW torch.Tensor
img = cv2.imread(fpath)
# from HxWxC to CxHxW, RGB image
img = (cv2.cvtColor(img, cv2.COLOR_BGR2RGB)).transpose(2, 0, 1)
else:
# from HxWxC to CxHxW grayscale image (C=1)
img = cv2.imread(fpath, cv2.IMREAD_GRAYSCALE)
if expand_axis0:
img = np.expand_dims(img, 0)
# Handle odd sizes
expanded_h = False
expanded_w = False
sh_im = img.shape
if expand_if_needed:
if sh_im[-2]%2 == 1:
expanded_h = True
if expand_axis0:
img = np.concatenate((img, \
img[:, :, -1, :][:, :, np.newaxis, :]), axis=2)
else:
img = np.concatenate((img, \
img[:, -1, :][:, np.newaxis, :]), axis=1)
if sh_im[-1]%2 == 1:
expanded_w = True
if expand_axis0:
img = np.concatenate((img, \
img[:, :, :, -1][:, :, :, np.newaxis]), axis=3)
else:
img = np.concatenate((img, \
img[:, :, -1][:, :, np.newaxis]), axis=2)
if normalize_data:
img = normalize(img)
return img, expanded_h, expanded_w
def normalize(data):
r"""Normalizes a unit8 image to a float32 image in the range [0, 1]
Args:
data: a unint8 numpy array to normalize from [0, 255] to [0, 1]
"""
return np.float32(data/255.)
def visualize_complex(cimg, cmap, amp_range, mode, reverse):
amp = np.abs(cimg)
pha = np.angle(cimg)
amin = amp_range[0]
amax = amp_range[1]
amp_norm = (amp-amin)/(amax-amin)
b = 1
ncmap = cmap.shape[0]
img = np.zeros((cimg.shape[0], cimg.shape[1] ,3))
for i in range (cimg.shape[0]):
for j in range(cimg.shape[1]):
a = amp_norm[i,j]
if reverse:
a = 1-a
if mode.lower() == 'hsv':
img[i,j,:] = cmap[1+round((ncmap-1)/2/PI*(pha[i,j]+PI)),:] * a
elif mode.lower() == 'hsl':
if a > 1/2:
w = (2*(a-1/2))**b
img[i,j,:] = cmap[round((ncmap-1)/2/PI*(pha[i,j]+PI)),:] * (1-w) + np.array([1,1,1]) * w
else:
w = (2*(1/2-a))**b
img[i,j,:] = cmap[round((ncmap-1)/2/PI*(pha[i,j]+PI)),:] * (1-w) + np.array([0,0,0]) * w
n = 256
cbarimg = np.zeros((n,n,3))
x = np.linspace(-1,1,n)
y = x
X, Y = np.meshgrid(x,y,indexing='xy')
theta, rho = cart2pol(X,Y)
for i in range(n):
for j in range(n):
if rho[i,j] <= 1:
a = rho[i,j]
if reverse:
a = 1-a
if mode.lower() == 'hsv':
cbarimg[i,j,:] = cmap[round((ncmap-1)/2/PI*(theta[i,j]+PI)),:] * a
elif mode.lower() == 'hsl':
if a > 1/2:
w = (2*(a-1/2))**b
cbarimg[i,j,:] = cmap[round((ncmap-1)/2/PI*(theta[i,j]+PI)),:] * (1-w) + np.array([1,1,1]) * w
else:
w = (2*(1/2-a))**b
cbarimg[i,j,:] = cmap[round((ncmap-1)/2/PI*(theta[i,j]+PI)),:] * (1-w) + np.array([0,0,0]) * w
return img, cbarimg
def cart2pol(x, y):
rho = np.sqrt(x**2 + y**2)
phi = np.arctan2(y, x)
return(rho, phi)
def pol2cart(rho, phi):
x = rho * np.cos(phi)
y = rho * np.sin(phi)
return(x, y)
def sinebow(n):
h = np.linspace(0,1,n)
h = h + 1/2;
h = h * (-1)
r = np.sin(PI*h)
g = np.sin(PI*(h+1/3))
b = np.sin(PI*(h+2/3));
c = np.concatenate((r.reshape(n,1),g.reshape(n,1),b.reshape(n,1)),axis=1)
c = c**2
return c