-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeedup.pyx
More file actions
234 lines (196 loc) · 6.98 KB
/
speedup.pyx
File metadata and controls
234 lines (196 loc) · 6.98 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
# cython: language_level=3
import os
os.environ['NUMPY_EXPERIMENTAL_ARRAY_FUNCTION'] = '0'
import math
cimport cython
import param
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
@cython.initializedcheck(False)
@cython.binding(False)
def pixelcount(int ppv_count, int triangle_count, int fancount):
cdef unsigned char[::1] triangle_img_deck_ravel_view = param.triangle_img_deck_ravel
cdef unsigned char[::1] circle_mask = param.circle_mask
cdef short unsigned int[::1] img_sum_ravel_view = param.img_sum_ravel
cdef Py_ssize_t i, j
cdef unsigned int pixel_total = param.IMG_WIDTH * param.IMG_HEIGHT
cdef unsigned short w = param.IMG_WIDTH
cdef unsigned int start, length, mi, o
cdef int ppv_count_last, r
cdef unsigned char h
start = 0
r = param.RESOLUTION
o = fancount * pixel_total
start = 0
length = pixel_total
for i in range(r):
mi = 0
for j in range(start, length):
if triangle_img_deck_ravel_view[j] != 0:
h = triangle_img_deck_ravel_view[j]
triangle_img_deck_ravel_view[j] = 0
if circle_mask[mi] != 0:
img_sum_ravel_view[mi + o] += h
ppv_count += 1
mi += 1
start += pixel_total
length += pixel_total
triangle_count += 1
return ppv_count, triangle_count
def init_calc(int ppv_count, int triangle_count, int fan_index, float xlaser, float ylaser):
cdef unsigned char[::1] triangle_img_deck_ravel_view = param.triangle_img_deck_ravel
cdef unsigned char[::1] circle_mask = param.circle_mask
cdef unsigned int[::1] pixel_positions_view = param.pixel_positions
cdef float[::1] pixel_weights_view = param.pixel_weights
cdef short unsigned int[::1] img_sum_ravel_view = param.img_sum_ravel
cdef int[::1] fan_triangles_ravel_view = param.fan_triangles_ravel
cdef unsigned int[::1] triagle_start_positions_view = param.triagle_start_positions
cdef unsigned int[::1] triangle_pixel_counts_view = param.triangle_pixel_counts
cdef Py_ssize_t i, j
cdef unsigned int pixel_total = param.IMG_WIDTH * param.IMG_HEIGHT
cdef unsigned short w = param.IMG_WIDTH
cdef unsigned int start, length, mi, hh
cdef float distance, x, y
cdef int ppv_count_last
cdef unsigned char h
hh = fan_index * pixel_total
start = 0
r = param.RESOLUTION
ppv_count_last = ppv_count
for i in range(r):
mi = 0
triagle_start_positions_view[triangle_count] = ppv_count
for j in range(start, start + pixel_total):
if triangle_img_deck_ravel_view[j] != 0:
h = triangle_img_deck_ravel_view[j]
triangle_img_deck_ravel_view[j] = 0
if circle_mask[mi] != 0:
pixel_positions_view[ppv_count] = mi
dx = -xlaser + mi % w
dy = -ylaser + mi // w
distance = math.sqrt(dx*dx + dy*dy)
pixel_weights_view[ppv_count] = h / (img_sum_ravel_view[hh + mi] * distance)
ppv_count += 1
mi += 1
triangle_pixel_counts_view[triangle_count] = ppv_count - ppv_count_last
ppv_count_last = ppv_count
triangle_count += 1
start += pixel_total
return ppv_count, triangle_count
def match_opti(double th, int c):
cdef unsigned char[::1] img_to_view
cdef unsigned char[::1] img_to_view_r = param.img_to_r
cdef unsigned char[::1] img_to_view_g = param.img_to_g
cdef unsigned char[::1] img_to_view_b = param.img_to_b
cdef float[::1] img_is_view
cdef float[::1] img_is_view_r = param.img_is_r
cdef float[::1] img_is_view_g = param.img_is_g
cdef float[::1] img_is_view_b = param.img_is_b
cdef float[::1] multiplyer_view
cdef float[::1] multiplyer_view_r = param.multiplier_r
cdef float[::1] multiplyer_view_g = param.multiplier_g
cdef float[::1] multiplyer_view_b = param.multiplier_b
cdef unsigned int[::1] pixel_positions_view = param.pixel_positions
cdef float[::1] pixel_weights_view = param.pixel_weights
cdef unsigned int[::1] triagle_start_positions_view = param.triagle_start_positions
cdef unsigned int[::1] triangle_pixel_counts_view = param.triangle_pixel_counts
cdef unsigned int start, length, pix_pos
cdef double current_dist, current_dist_last, pot_fut_dist, diff, improv
cdef Py_ssize_t i, j, count = 0
cdef float as_is, to_be, multiplyer, pwv
cdef str col_str
if c == 0:
img_to_view = img_to_view_b
img_is_view = img_is_view_b
multiplyer_view = multiplyer_view_b
col_str = 'blue '
if c == 1:
img_to_view = img_to_view_g
img_is_view = img_is_view_g
multiplyer_view = multiplyer_view_g
col_str = 'green'
if c == 2:
img_to_view = img_to_view_r
img_is_view = img_is_view_r
multiplyer_view = multiplyer_view_r
col_str = 'red '
for i in range(param.RESOLUTION * param.LASERCOUNT):
start = triagle_start_positions_view[i]
length = triangle_pixel_counts_view[i]
multiplyer = multiplyer_view[i]
length += start
for j in range(start, length):
pix_pos = pixel_positions_view[j]
pwv = pixel_weights_view[j]
img_is_view[pix_pos] += pwv * multiplyer
#distance
current_dist_last = 0.0
for i in range(param.IMG_WIDTH * param.IMG_HEIGHT):
to_be = img_to_view[i]
if to_be != 0:
as_is = img_is_view[i]
as_is -= to_be
current_dist_last += as_is*as_is
current_dist_last = math.sqrt(current_dist_last)
while True:
for i in range(param.RESOLUTION * param.LASERCOUNT):
start = triagle_start_positions_view[i]
length = triangle_pixel_counts_view[i]
multiplyer = multiplyer_view[i]
current_dist = 0.0
pot_fut_dist = 0.0
length += start
for j in range(start, length):
pix_pos = pixel_positions_view[j]
pwv = pixel_weights_view[j]
to_be = img_to_view[pix_pos]
as_is = img_is_view[pix_pos] - multiplyer * pwv
to_be -= as_is
current_dist += to_be * pwv
pot_fut_dist += pwv*pwv
if pot_fut_dist != 0.0:
diff = current_dist / pot_fut_dist
else:
diff = 0.0
if diff < 0.0:
diff = 0.0
for j in range(start, length):
pix_pos = pixel_positions_view[j]
pwv = pixel_weights_view[j]
pwv *= (diff - multiplyer)
img_is_view[pix_pos] += pwv
multiplyer_view[i] = diff
count += 1
#distance
current_dist = 0.0
for i in range(param.IMG_WIDTH * param.IMG_HEIGHT):
to_be = img_to_view[i]
if to_be != 0.0:
as_is = img_is_view[i]
as_is -= to_be
current_dist += as_is*as_is
current_dist = math.sqrt(current_dist)
improv = current_dist_last - current_dist
print( col_str,
"| round: {:>5}".format(count),
"| RMSE: {:>13.1f}".format(current_dist),
"| RMSE improve: {:>12.1f}".format(improv),
"/ {:>4.1f}".format(th),
flush = True)
current_dist_last = current_dist
if improv < th:
break
for i in range(param.IMG_WIDTH * param.IMG_HEIGHT):
img_is_view[i] = 0.0
for i in range(param.RESOLUTION * param.LASERCOUNT):
start = triagle_start_positions_view[i]
length = triangle_pixel_counts_view[i]
multiplyer = multiplyer_view[i]
length += start
for j in range(start, length):
pix_pos = pixel_positions_view[j]
pwv = pixel_weights_view[j]
img_is_view[pix_pos] += pwv * multiplyer
return