Merge pull request #794 from ahojnnes/lbp-memview

Speed up one-dimensional memory views
This commit is contained in:
Juan Nunez-Iglesias
2013-10-19 04:56:52 -07:00
6 changed files with 23 additions and 23 deletions
+2 -2
View File
@@ -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:
+11 -11
View File
@@ -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:
+4 -4
View File
@@ -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)
+2 -2
View File
@@ -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
+3 -3
View File
@@ -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
+1 -1
View File
@@ -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)