From b7889b2c4f398f292610ead86eb514ff4e08d820 Mon Sep 17 00:00:00 2001 From: Evgeni Burovski Date: Wed, 27 Jan 2016 11:07:08 +0000 Subject: [PATCH] MAINT: avoid doing np.sum in the hot loop (you gotta be kidding me, x2 speed-up) --- skimage/morphology/_skel.pyx | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/skimage/morphology/_skel.pyx b/skimage/morphology/_skel.pyx index 6eedc407..a500fb66 100644 --- a/skimage/morphology/_skel.pyx +++ b/skimage/morphology/_skel.pyx @@ -241,6 +241,18 @@ def is_surfacepoint(neighbors, points_LUT): return True +@cython.boundscheck(False) +@cython.wraparound(False) +cdef inline bint is_endpoint(neighb_type neighbors): + """An endpoint has exactly one neighbor in the 26-neighborhood. + """ + # The center pixel is counted, thus r.h.s. is 2 + cdef int s = 0, j + for j in range(27): + s += neighbors[j] + return s == 2 + + @cython.boundscheck(False) @cython.wraparound(False) cdef bint is_Euler_invariant(neighb_type neighbors): @@ -612,14 +624,12 @@ cdef list _loop_through(img_type img, get_neighborhood(img, p, r, c, neighborhood) - # check if (p, r, c) is an endpoint. An endpoint has exactly - # one neighbor in the 26-neighborhood. - # The center pixel is counted, thus r.h.s. is 2 - if np.sum(neighborhood) == 2: + # check if (p, r, c) is an endpoint: endpoints are not deletable. + if is_endpoint(neighborhood): continue - # check if point is Euler invariant (condition 1 in [Lee94]) - # if it is not, it's not deletable + # check if point is Euler invariant (condition 1 in [Lee94]): + # if it is not, it's not deletable. if not is_Euler_invariant(neighborhood): continue