This repository has been archived by the owner on Apr 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterpolations.py
171 lines (136 loc) · 4.26 KB
/
Interpolations.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
import numpy as np
import math
#import numerico as nc
import time
def resize(img, fac_x, fac_y, method):
if method is "nn":
result = nearest_neighbor(img, fac_x, fac_y)
if method is "bilinear":
result = bilinear_interpol(img, fac_x, fac_y)
if method is "bilinear2":
result = bilinear_interpol_2(img, fac_x, fac_y)
if method is "bicubic":
result = bicubic_interpol(img, fac_x, fac_y)
return result
def nearest_neighbor(data, by_x, by_y):
new_x = int(np.ceil( data.shape[0]*by_x ))
new_y = int(np.ceil( data.shape[1]*by_y ))
new_img = np.empty((new_x, new_y))
dec_ratio_x = 1.0/by_x
dec_ratio_y = 1.0/by_y
for i in range(0, new_img.shape[0]):
for j in range(0, new_img.shape[1]):
back_index_x = int(np.floor(i*dec_ratio_x))
back_index_y = int(np.floor(j*dec_ratio_y))
new_img[i, j] = data[back_index_x, back_index_y]
return new_img
# Obtiene las referencias al primero de los 4 puntos de 'p'
# de la data antes de redimensionar, o retorna None si p
# esta fuera de los límites
def bilinear_refs(old_data, p, factor_x, factor_y):
i_floor = np.floor(p[0]/factor_x)
j_floor = np.floor(p[1]/factor_y)
i_ceil = np.ceil(p[0]/factor_x)
j_ceil = np.ceil(p[1]/factor_y)
if i_ceil >= old_data.shape[0] or j_ceil >= old_data.shape[1]:
return math.nan, math.nan
else:
Q_00_x = int(i_floor)
Q_00_y = int(j_floor)
return Q_00_x, Q_00_y
def bilinear_interpol(data, by_x, by_y):
new_x = int(np.ceil( data.shape[0]*by_x ))
new_y = int(np.ceil( data.shape[1]*by_y ))
new_img = np.empty((new_x, new_y))
for i in range(0, new_img.shape[0]):
for j in range(0, new_img.shape[1]):
coord = (i, j)
Q = bilinear_refs(data, coord ,by_x, by_y)
x_0 = Q[0]
y_0 = Q[1]
x_1 = Q[0]+1
y_1 = Q[1]+1
if(x_1 >= data.shape[0]):
x_1 -= 1
x_0 -= 1
if(y_1 >= data.shape[1]):
y_1 -= 1
y_0 -= 1
if not (math.isnan(x_0) or math.isnan(y_0)):
factor = 1/((x_1 - x_0)*(y_1 - y_0))
X = np.array([
[x_1 - i/by_x],
[i/by_x - x_0]
]).T
fQ = np.array([
[ data[x_0, y_0], data[x_0, y_1] ],
[ data[x_1, y_0], data[x_1, y_1] ]
])
Y = np.array([
[y_1 - j/by_y],
[j/by_y - y_0]
])
new_img[i, j] = factor*np.matmul(np.matmul(X, fQ) , Y)
return new_img[:int((data.shape[0]-1)*by_x),:int((data.shape[1]-1)*by_y)]
def bilinear_interpol_2(data, by_x, by_y):
new_x = int(np.ceil( data.shape[0]*by_x ))
new_y = int(np.ceil( data.shape[1]*by_y ))
new_img = np.empty((new_x, new_y))
start = time.time()
for i in range(0, new_img.shape[0]):
for j in range(0, new_img.shape[1]):
coord = (i, j)
Q = bilinear_refs(data, coord ,by_x, by_y)
x_0 = Q[0]
y_0 = Q[1]
x_1 = Q[0]+1
y_1 = Q[1]+1
if(x_1 >= data.shape[0]):
x_1 -= 1
x_0 -= 1
if(y_1 >= data.shape[1]):
y_1 -= 1
y_0 -= 1
if not (math.isnan(x_0) or math.isnan(y_0)):
A = np.array([
[1.0, x_0, y_0, x_0*y_0],
[1.0, x_0, y_1, x_0*y_1],
[1.0, x_1, y_0, x_1*y_0],
[1.0, x_1, y_1, x_1*y_1]
])
fQ = np.array([
[ data[x_0, y_0] ],
[ data[x_0, y_1] ],
[ data[x_1, y_0] ],
[ data[x_1, y_1] ]
])
result = np.linalg.solve(A, fQ)
new_img[i,j] = result[0] + result[1]*i/by_x + result[2]*j/by_y + result[3]*i*j/(by_x*by_y)
end = time.time()
#print("Bilinear interpolation exec. time: ", end - start)
return new_img[:int((data.shape[0]-1)*by_x),:int((data.shape[1]-1)*by_y)]
def w_cub(x):
val = 0
if (0 <= np.abs(x)) and (np.abs(x) < 1):
val = np.abs(x)**3 - 2*np.abs(x)**2 + 1.0
elif (1 <= np.abs(x)) and (np.abs(x) < 2):
val = -np.abs(x)**3 + 5*np.abs(x)**2 - 8*np.abs(x) + 4.0
else:
val = 0
return val
def p_bicubic_interpol(data, p_ij):
q = 0
for j in range(0, 3):
v = int(np.floor(p_ij[1] - 1 + j))
p = 0
for i in range(0, 3):
u = int(np.floor(p_ij[0]) - 1 + i)
p = p + data[u, v]*w_cub(p_ij[0] - u)
q = q + p*w_cub(p_ij[1] - v)
return q
def bicubic_interpol(data, by_x, by_y):
new_data = np.empty((int(data.shape[0]*by_x), int(data.shape[1]*by_y)))
for i in range(0, new_data.shape[0] - int(by_x)):
for j in range(0, new_data.shape[1] - int(by_y)):
new_data[i, j] = p_bicubic_interpol(data, (float(i)/by_x, float(j)/by_y))
return new_data[:int((data.shape[0]-1)*by_x),:int((data.shape[1]-1)*by_y)]