From bfb0cfce2fbcfa2b15a3d223b6dc068a5f0bd1a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Fri, 18 Oct 2013 20:49:49 +0200 Subject: [PATCH 1/4] Speed up memory views in local_binary_pattern --- skimage/feature/_texture.pyx | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/skimage/feature/_texture.pyx b/skimage/feature/_texture.pyx index 6caa7ea3..ec83fa65 100644 --- a/skimage/feature/_texture.pyx +++ b/skimage/feature/_texture.pyx @@ -106,17 +106,17 @@ def _local_binary_pattern(double[:, ::1] image, """ # texture weights - cdef int[:] weights = 2 ** np.arange(P, dtype=np.int32) + cdef int[::1] weights = 2 ** np.arange(P, dtype=np.int32) # local position of texture elements rr = - R * np.sin(2 * np.pi * np.arange(P, dtype=np.double) / P) cc = R * np.cos(2 * np.pi * np.arange(P, dtype=np.double) / P) - cdef double[:] rp = np.round(rr, 5) - cdef double[:] cp = np.round(cc, 5) + cdef double[::1] rp = np.round(rr, 5) + cdef double[::1] cp = np.round(cc, 5) # pre-allocate arrays for computation - cdef double[:] texture = np.zeros(P, dtype=np.double) - cdef char[:] signed_texture = np.zeros(P, dtype=np.int8) - cdef int[:] rotation_chain = np.zeros(P, dtype=np.int32) + cdef double[::1] texture = np.zeros(P, dtype=np.double) + cdef char[::1] signed_texture = np.zeros(P, dtype=np.int8) + cdef int[::1] rotation_chain = np.zeros(P, dtype=np.int32) output_shape = (image.shape[0], image.shape[1]) cdef double[:, ::1] output = np.zeros(output_shape, dtype=np.double) @@ -162,21 +162,21 @@ def _local_binary_pattern(double[:, ::1] image, # n_ones=2: 0011, 1001, 1100, 0110 # n_ones=3: 0111, 1011, 1101, 1110 # n_ones=4: 1111 - # + # # For a pattern of size P there are 2 constant patterns # corresponding to n_ones=0 and n_ones=P. For each other # value of `n_ones` , i.e n_ones=[1..P-1], there are P # possible patterns which are related to each other through # circular permutations. The total number of uniform - # patterns is thus (2 + P * (P - 1)). + # patterns is thus (2 + P * (P - 1)). # Given any pattern (uniform or not) we must be able to - # associate a unique code: + # associate a unique code: # 1. Constant patterns patterns (with n_ones=0 and # n_ones=P) and non uniform patterns are given fixed # code values. # 2. Other uniform patterns are indexed considering the # value of n_ones, and an index called 'rot_index' - # reprenting the number of circular right shifts + # reprenting the number of circular right shifts # required to obtain the pattern starting from a # reference position (corresponding to all zeros stacked # on the right). This number of rotations (or circular @@ -215,7 +215,7 @@ def _local_binary_pattern(double[:, ::1] image, lbp += signed_texture[i] else: lbp = P + 1 - + if method == 'V': var = np.var(texture) if var != 0: From 342205fcde3104096294a46de3b9ac06a94bee97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Fri, 18 Oct 2013 20:54:28 +0200 Subject: [PATCH 2/4] Speed up memory views in line drawing function --- skimage/draw/_draw.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skimage/draw/_draw.pyx b/skimage/draw/_draw.pyx index db63d343..7300a2a1 100644 --- a/skimage/draw/_draw.pyx +++ b/skimage/draw/_draw.pyx @@ -71,8 +71,8 @@ def line(Py_ssize_t y, Py_ssize_t x, Py_ssize_t y2, Py_ssize_t x2): sx, sy = sy, sx d = (2 * dy) - dx - cdef Py_ssize_t[:] rr = np.zeros(int(dx) + 1, dtype=np.intp) - cdef Py_ssize_t[:] cc = np.zeros(int(dx) + 1, dtype=np.intp) + cdef Py_ssize_t[::1] rr = np.zeros(int(dx) + 1, dtype=np.intp) + cdef Py_ssize_t[::1] cc = np.zeros(int(dx) + 1, dtype=np.intp) for i in range(dx): if steep: From 3e28168914628dbf5a01b6ade9fd1a29a7613382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Fri, 18 Oct 2013 20:56:18 +0200 Subject: [PATCH 3/4] Speed up memory views in skeletonize function --- skimage/morphology/_skeletonize.py | 8 ++++---- skimage/morphology/_skeletonize_cy.pyx | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/skimage/morphology/_skeletonize.py b/skimage/morphology/_skeletonize.py index 8872aada..11181a7b 100644 --- a/skimage/morphology/_skeletonize.py +++ b/skimage/morphology/_skeletonize.py @@ -282,8 +282,8 @@ def medial_axis(image, mask=None, return_distance=False): i, j = np.mgrid[0:image.shape[0], 0:image.shape[1]] result = masked_image.copy() distance = distance[result] - i = np.ascontiguousarray(i[result], np.intp) - j = np.ascontiguousarray(j[result], np.intp) + i = np.ascontiguousarray(i[result], dtype=np.intp) + j = np.ascontiguousarray(j[result], dtype=np.intp) result = np.ascontiguousarray(result, np.uint8) # Determine the order in which pixels are processed. @@ -296,9 +296,9 @@ def medial_axis(image, mask=None, return_distance=False): order = np.lexsort((tiebreaker, corner_score[masked_image], distance)) - order = np.ascontiguousarray(order, np.int32) + order = np.ascontiguousarray(order, dtype=np.int32) - table = np.ascontiguousarray(table, np.uint8) + table = np.ascontiguousarray(table, dtype=np.uint8) # Remove pixels not belonging to the medial axis _skeletonize_loop(result, i, j, order, table) diff --git a/skimage/morphology/_skeletonize_cy.pyx b/skimage/morphology/_skeletonize_cy.pyx index 10e70d98..c4471a11 100644 --- a/skimage/morphology/_skeletonize_cy.pyx +++ b/skimage/morphology/_skeletonize_cy.pyx @@ -20,8 +20,8 @@ cimport numpy as cnp def _skeletonize_loop(cnp.uint8_t[:, ::1] result, - Py_ssize_t[:] i, Py_ssize_t[:] j, - cnp.int32_t[:] order, cnp.uint8_t[:] table): + Py_ssize_t[::1] i, Py_ssize_t[::1] j, + cnp.int32_t[::1] order, cnp.uint8_t[::1] table): """ Inner loop of skeletonize function From 1b66ab479a843fb1bcfbd996b1b16f205571fd19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Fri, 18 Oct 2013 21:00:40 +0200 Subject: [PATCH 4/4] Speed up memory views in watershed function --- skimage/morphology/_watershed.pyx | 6 +++--- skimage/morphology/watershed.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/skimage/morphology/_watershed.pyx b/skimage/morphology/_watershed.pyx index beaf874b..a0857707 100644 --- a/skimage/morphology/_watershed.pyx +++ b/skimage/morphology/_watershed.pyx @@ -23,12 +23,12 @@ include "heap_watershed.pxi" @cython.boundscheck(False) -def watershed(DTYPE_INT32_t[:] image, +def watershed(DTYPE_INT32_t[::1] image, DTYPE_INT32_t[:, ::1] pq, Py_ssize_t age, DTYPE_INT32_t[:, ::1] structure, - DTYPE_BOOL_t[:] mask, - DTYPE_INT32_t[:] output): + DTYPE_BOOL_t[::1] mask, + DTYPE_INT32_t[::1] output): """Do heavy lifting of watershed algorithm Parameters diff --git a/skimage/morphology/watershed.py b/skimage/morphology/watershed.py index c9fb1789..2bdb57b2 100644 --- a/skimage/morphology/watershed.py +++ b/skimage/morphology/watershed.py @@ -202,7 +202,7 @@ def watershed(image, markers, connectivity=None, offset=None, mask=None): stride = np.dot(image_stride, np.array(offs)) offs.insert(0, stride) c.append(offs) - c = np.array(c, np.int32) + c = np.array(c, dtype=np.int32) pq, age = __heapify_markers(c_markers, c_image) pq = np.ascontiguousarray(pq, dtype=np.int32)