-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
219 lines (179 loc) · 5.42 KB
/
utils.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
from constants import RGB_CHANNELS
from PIL import Image
from numba import njit
import numpy as np
import time
from typing import Callable
MAX_COLOR = 255
def open_image(img_filename):
img = Image.open(img_filename)
img_arr = np.array(img)
return img_arr
def backwards_mapping(
img_arr: np.ndarray,
h1: int,
w1: int,
sample_func: Callable[[np.ndarray, float, float], np.ndarray]
) -> np.ndarray:
if len(img_arr.shape) == 2:
new_shape = [h1, w1]
else:
new_shape = [h1, w1, img_arr.shape[2]]
new_arr = np.zeros(new_shape, dtype=np.uint8)
num_pixels = h1 * w1
for counter in range(num_pixels):
j = int(counter / w1)
i = int(counter % w1)
# sample the corresponding pixel in the original array
u = (i + 0.5) / w1
v = (h1 - (j + 0.5)) / h1 # y would be going from bottom to top
new_arr[j, i] = sample_func(img_arr, u, v)
counter += 1
return new_arr
def normalize(arr):
"""
Normalize a vector using numpy.
Args:
arr(ndarray): Input vector
Returns:
ndarray: Normalized input vector
"""
norm = np.linalg.norm(arr)
if norm == 0:
return arr
return arr / norm
def distance(p1, p2):
"""
Get the distance between points p1 and p2
Args:
p1(ndarray): Point 1
p2(ndarray): Point 2
Returns:
float: Distance
"""
dist = np.linalg.norm(p1 - p2)
return dist
def humanize_time(secs):
minutes, secs = divmod(secs, 60)
hours, minutes = divmod(minutes, 60)
return '%02d:%02d:%02d' % (hours, minutes, secs)
def degrees2radians(degrees):
return (degrees / 360) * 2 * np.pi
def normalize_color(color):
return color / MAX_COLOR
@njit
def scale_blerp_njit(img_arr: np.ndarray, h1: int, w1: int) -> np.ndarray:
new_arr = np.zeros((h1, w1, RGB_CHANNELS), dtype=np.uint8)
for j in range(h1):
for i in range(w1):
# sample the corresponding pixel in the original array
u = (i + 0.5) / w1
v = (h1 - (j + 0.5)) / h1 # y would be going from bottom to top
new_arr[j, i] = blerp_uv_njit(img_arr, u, v)
return new_arr
@njit
def scale_nn_njit(img_arr: np.ndarray, h1: int, w1: int) -> np.ndarray:
new_arr = np.zeros((h1, w1, RGB_CHANNELS), dtype=np.uint8)
for j in range(h1):
for i in range(w1):
# sample the corresponding pixel in the original array
u = (i + 0.5) / w1
v = (h1 - (j + 0.5)) / h1 # y would be going from bottom to top
new_arr[j, i] = nearest_neighbor_uv_njit(img_arr, u, v)
return new_arr
# Sample functions
# -----------------------------------------------------------------------------
def blerp(img_arr, x, y):
height, width = img_arr.shape[:2]
# Interpolate values of pixel neighborhood of x and y
i = int(x)
# Flip y value to go from top to bottom
y = height - y
j = int(y)
# But not in the borders
if i == 0 or j == 0 or i == width or j == height:
if i == width:
i -= 1
if j == height:
j -= 1
return img_arr[j][i]
# t and s are interpolation parameters that go from 0 to 1
t = x - i
s = y - j
# Bi-linear interpolation
color = np.round(
img_arr[j - 1][i - 1] * (1 - t) * (1 - s)
+ img_arr[j - 1][i] * t * (1 - s)
+ img_arr[j][i - 1] * (1 - t) * s
+ img_arr[j][i] * t * s
).astype(np.uint8)
return color
def blerp_uv(img_arr, u, v):
height, width = img_arr.shape[:2]
x = u * width
y = v * height
return blerp(img_arr, x, y)
def nearest_neighbor(img_arr, x, y):
height, width = img_arr.shape[:2]
i = int(x)
# Flip y value to go from top to bottom
y = height - y
j = int(y)
return img_arr[j, i]
def nearest_neighbor_uv(img_arr, u, v):
height, width = img_arr.shape[:2]
x = u * width
y = v * height
return nearest_neighbor(img_arr, x, y)
@njit
def nearest_neighbor_uv_njit(
img_arr: np.ndarray, u: float, v: float
) -> np.ndarray:
height, width = img_arr.shape[:2]
x = u * width
y = v * height
i = int(x)
# Flip y value to go from top to bottom
y = height - y
j = int(y)
return img_arr[j, i]
@njit
def blerp_uv_njit(img_arr: np.ndarray, u: float, v: float) -> np.ndarray:
height, width = img_arr.shape[:2]
x = u * width
y = v * height
# Interpolate values of pixel neighborhood of x and y
i = int(x)
# Flip y value to go from top to bottom
y = height - y
j = int(y)
# But not in the borders
if i == 0 or j == 0 or i == width or j == height:
if i == width:
i -= 1
if j == height:
j -= 1
return img_arr[j][i]
# t and s are interpolation parameters that go from 0 to 1
t = x - i
s = y - j
# Bi-linear interpolation
color = np.round(
img_arr[j - 1][i - 1] * (1 - t) * (1 - s)
+ img_arr[j - 1][i] * t * (1 - s)
+ img_arr[j][i - 1] * (1 - t) * s
+ img_arr[j][i] * t * s
).astype(np.uint8)
return color
class Timer:
def __init__(self):
self.start_time = 0
self.end_time = 0
self.elapsed_time = 0
def start(self):
self.start_time = time.time()
def stop(self):
self.end_time = time.time()
self.elapsed_time = self.end_time - self.start_time
def __str__(self):
return humanize_time(self.elapsed_time)