Skip to content

Commit d7c7159

Browse files
committed
rasterization with numba - faster
1 parent 802a19b commit d7c7159

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

pclines/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .accumulator import accumulate, find_peaks, lines
1+
from .accumulator import PCLines

pclines/accumulator.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131

3232

3333
import numpy as np
34-
#import numba as nb
3534
from skimage.feature import peak_local_max
3635
from skimage.morphology.grey import erosion, dilation
3736

37+
from .rasterizer import polys
3838

3939
def _linear_transform(src, dst):
4040
""" Parameters of a linear transform from range specifications """
@@ -112,7 +112,7 @@ def transform(self, x):
112112
"""
113113
_check_points(x)
114114
x0,x1 = np.split(x,2,axis=1)
115-
return np.concatenate([self.norm_u(x1), self.norm_v(x0), self.norm_w(x1)], axis=1)
115+
return np.concatenate([self.norm_u(x1), self.norm_v(x0), self.norm_w(x1)], axis=1).astype("f")
116116

117117
def inverse(self, l):
118118
"""
@@ -145,25 +145,23 @@ def insert(self, x, weight=None):
145145
if weight is None:
146146
weight = np.ones(n, np.float32)
147147

148+
if weight.dtype != np.float32:
149+
weight = weight.astype(np.float32)
150+
148151
valid = self.valid_points(p)
149152
p = p[valid]
150153
weight = weight[valid.flat]
151154

152-
d = self.d
153-
c = np.arange(2*d-1,dtype="i")
154-
for (u,v,w),wt in zip(p, weight): # remove space wraping
155-
t_part = np.linspace(u,v,d)
156-
s_part = np.linspace(v,w,d)
157-
r = np.concatenate([t_part, s_part[1:]]).astype("i")
158-
self.A[r,c] += wt # TODO add weight
155+
polys(p, weight, self.A)
156+
159157

160158
def find_peaks(self, t=0.8, prominence=2, min_dist=1):
161159
"""
162160
Retrieve locations with prominent local maxima in the accumulator
163161
"""
164162
p = dilation(self.A+1)/erosion(self.A+1)
165-
peaks = peak_local_max(self.A, threshold_rel=t, min_distance=min_dist)
163+
peaks = peak_local_max(self.A, threshold_rel=t, min_distance=min_dist, exclude_border=False)
166164
r,c = peaks[:,0], peaks[:,1]
167-
value = A[r,c]
165+
value = self.A[r,c]
168166
valid = p[r,c] > prominence
169167
return peaks[valid], value[valid]

pclines/rasterizer.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import numpy as np
2+
import numba as nb
3+
4+
@nb.njit
5+
def line(xr, yr, w, arr):
6+
x0,x1 = xr
7+
y0,y1 = yr
8+
dy = (y1-y0) / (x1-x0)
9+
for step,x in enumerate(range(x0, x1)):
10+
y = int(y0 + (dy * step))
11+
arr[y,x] += w
12+
13+
@nb.njit
14+
def polys(p, weight, arr):
15+
n = weight.size
16+
d = arr.shape[0]
17+
for i in range(n):
18+
u,v,w = p[i]
19+
wt = weight[i]
20+
# rasterize (u,v), (0,d-1)
21+
line((0, d), (u, v), wt, arr)
22+
# rasterize (v,w), (d, 2d-1)
23+
line((d, 2*d-1), (v, w), wt, arr)
24+
25+

0 commit comments

Comments
 (0)