forked from max-lancaster/namsel
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfast_utils.pyx
189 lines (161 loc) · 4.77 KB
/
fast_utils.pyx
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
from libc.math cimport sqrt, floor, fmod, log, abs
import numpy as np
cimport numpy as np
cimport cython
from cv2 import resize, INTER_CUBIC
cdef extern from "math.h":
bint isnan(double x)
def gausslogprob(double mean, double std, double x):
cdef double pi = np.pi
cdef double df = x - mean
cdef double dn = log(sqrt(2*pi))
return (-(df*df)/(2*std*std)) - dn - log(std)
cpdef scale_transform(double [:] x, double [:] mean, double [:] o_std, int size):
cdef int i
for i in range(size):
x[i] = (x[i] - mean[i])*o_std[i]
@cython.boundscheck(False)
cpdef fadd_padding(np.uint8_t [:,:] arr, int padding):
cdef int i, j
cdef int h = arr.shape[0]
cdef int w = arr.shape[1]
cdef int nh = h+2*padding
cdef int nw = w+2*padding
cdef int ed1 = padding
cdef int ed2 = nw-padding
cdef int top1 = padding
cdef int top2 = nh-padding
cdef np.ndarray[np.uint8_t, ndim=2] newarr = np.empty((nh, nw), np.uint8)
for i in range(nh):
for j in range(nw):
if (i < top1) or (i >= top2):
newarr[i,j] = 1
elif (j < ed1) or (j >= ed2):
newarr[i,j] = 1
else:
newarr[i,j] = arr[i-padding, j-padding]
return newarr
@cython.boundscheck(False)
cpdef ftrim(np.ndarray[np.uint8_t, ndim=2] arr, sides='trbl', new_offset=False):
cdef int top = 0
cdef int left = 0
cdef int right = arr.shape[1]
cdef int rows = arr.shape[0]
cdef int bottom = rows
cdef int i, j
cdef int oft = 0
cdef int ofb = 0
cdef int ofr = 0
cdef int ofl = 0
cdef np.uint8_t [:,:] arrT = arr.T
if 't' in sides:
brk = False
for i in range(rows):
for j in range(right):
if arr[i,j] == 0:
top = i
oft = i
brk = True
break
if brk: break
if 'b' in sides:
brk = False
for i in range(bottom-1, 0, -1):
for j in range(right):
if arr[i,j] == 0:
ofb = -(bottom-i)
bottom = i
brk = True
break
if brk: break
if 'l' in sides:
brk = False
for i in range(right):
for j in range(rows):
if arrT[i,j] == 0:
left = i
ofl = i
brk = True
break
if brk: break
if 'r' in sides:
brk = False
for i in range(right-1, 0, -1):
for j in range(rows):
if arrT[i,j] == 0:
ofr = -(right-i)
right = i
brk = True
break
if brk: break
if not new_offset:
return arr[top:bottom, left:right]
else:
return arr[top:bottom, left:right], {'top':oft, 'bottom':ofb, 'right':ofr, 'left':ofl}
@cython.cdivision(True)
@cython.boundscheck(False)
cpdef to255(np.ndarray[np.uint8_t, ndim=2] a):
cdef int i, j
cdef int h = a.shape[0]
cdef int w = a.shape[1]
for i in range(h):
for j in range(w):
a[i,j] = a[i,j]*255
return a
@cython.wraparound(False)
@cython.cdivision(True)
@cython.boundscheck(False)
cpdef fnormalize(np.ndarray[np.uint8_t, ndim=2] a, np.ndarray[np.uint8_t, ndim=2] c):
cdef double h = a.shape[0]
cdef double w = a.shape[1]
cdef double L = 32.0
cdef int LL = 32
cdef double o_2 = 1.0/2.0
cdef double R1, R2, H2, W2, offset, start, end, alpha, beta, sm, bg, smn, df
cdef np.ndarray[np.uint8_t, ndim=2] b
cdef int smi, bgi, i, j, starti, endi
if h >= w:
bg = h
sm = w
smi = 1
bgi = 0
else:
bg = w
sm = h
smi = 0
bgi = 1
R1 = sm/bg
R2 = sqrt(R1)
if sm == h:
H2 = L*R2
W2 = L
else:
H2 = L
W2 = L*R2
alpha = W2 / w
beta = H2 / h
b = resize(a, (0,0), fy=beta, fx=alpha, interpolation=INTER_CUBIC)
smn = b.shape[smi]
df = L - smn
offset = floor(df * o_2)
if fmod(df, 2) == 1.0:
start = offset+1.0
end = offset
else:
start = end = offset
starti = int(start)
endi = int(end)
if sm == h:
for i in range(LL):
for j in range(LL):
if i < starti or i >= LL-endi:
c[i,j] = 1
else:
c[i,j] = b[i-starti,j]
else:
for i in range(32):
for j in range(32):
if j < starti or j >= LL-endi:
c[i,j] = 1
else:
c[i,j] = b[i,j-starti]