diff --git a/skimage/_shared/setup.py b/skimage/_shared/setup.py index 6e4d1b6b..ed48916e 100644 --- a/skimage/_shared/setup.py +++ b/skimage/_shared/setup.py @@ -14,9 +14,12 @@ def configuration(parent_package='', top_path=None): config.add_data_dir('tests') cython(['interpolation.pyx'], working_path=base_path) + cython(['transform.pyx'], working_path=base_path) config.add_extension('interpolation', sources=['interpolation.c'], include_dirs=[get_numpy_include_dirs()]) + config.add_extension('transform', sources=['transform.c'], + include_dirs=[get_numpy_include_dirs()]) return config diff --git a/skimage/_shared/transform.pxd b/skimage/_shared/transform.pxd new file mode 100644 index 00000000..2953ac20 --- /dev/null +++ b/skimage/_shared/transform.pxd @@ -0,0 +1,6 @@ +cimport numpy as cnp +import numpy as np + + +cdef float integrate(cnp.ndarray[float, ndim=2, mode="c"] sat, + int r0, int c0, int r1, int c1) diff --git a/skimage/_shared/transform.pyx b/skimage/_shared/transform.pyx new file mode 100644 index 00000000..b6649852 --- /dev/null +++ b/skimage/_shared/transform.pyx @@ -0,0 +1,45 @@ +#cython: cdivison=True +#cython: boundscheck=False +#cython: nonecheck=False +#cython: wraparound=False +cimport numpy as cnp +import numpy as np + + +cdef float integrate(cnp.ndarray[float, ndim=2, mode="c"] sat, + int r0, int c0, int r1, int c1): + """ + Using a summed area table / integral image, calculate the sum + over a given window. + + This function is the same as the `integrate` function in + `skimage.transform.integrate`, but this Cython version significantly + speeds up the code. + + Parameters + ---------- + sat : ndarray of float + Summed area table / integral image. + r0, c0 : int + Top-left corner of block to be summed. + r1, c1 : int + Bottom-right corner of block to be summed. + + Returns + ------- + S : int + Sum over the given window. + """ + cdef float S = 0 + + S += sat[r1, c1] + + if (r0 - 1 >= 0) and (c0 - 1 >= 0): + S += sat[r0 - 1, c0 - 1] + + if (r0 - 1 >= 0): + S -= sat[r0 - 1, c1] + + if (c0 - 1 >= 0): + S -= sat[r1, c0 - 1] + return S diff --git a/skimage/feature/_template.pyx b/skimage/feature/_template.pyx index b83761a8..58d48524 100644 --- a/skimage/feature/_template.pyx +++ b/skimage/feature/_template.pyx @@ -35,51 +35,8 @@ cimport numpy as np import numpy as np from scipy.signal import fftconvolve from skimage.transform import integral - - -cdef extern from "math.h": - float sqrt(float x) - float fabs(float x) - - -@cython.boundscheck(False) -cdef float integrate(np.ndarray[float, ndim=2, mode="c"] sat, - int r0, int c0, int r1, int c1): - """ - Using a summed area table / integral image, calculate the sum - over a given window. - - This function is the same as the `integrate` function in - `skimage.transform.integrate`, but this Cython version significantly - speeds up the code. - - Parameters - ---------- - sat : ndarray of float - Summed area table / integral image. - r0, c0 : int - Top-left corner of block to be summed. - r1, c1 : int - Bottom-right corner of block to be summed. - - Returns - ------- - S : int - Sum over the given window. - """ - cdef float S = 0 - - S += sat[r1, c1] - - if (r0 - 1 >= 0) and (c0 - 1 >= 0): - S += sat[r0 - 1, c0 - 1] - - if (r0 - 1 >= 0): - S -= sat[r0 - 1, c1] - - if (c0 - 1 >= 0): - S -= sat[r1, c0 - 1] - return S +from libc.math cimport sqrt, fabs +from skimage._shared.transform cimport integrate @cython.boundscheck(False) diff --git a/skimage/feature/setup.py b/skimage/feature/setup.py index 0b0b80bd..9c9074be 100644 --- a/skimage/feature/setup.py +++ b/skimage/feature/setup.py @@ -16,10 +16,9 @@ def configuration(parent_package='', top_path=None): cython(['_template.pyx'], working_path=base_path) config.add_extension('_texture', sources=['_texture.c'], - include_dirs=[get_numpy_include_dirs(), - '../transform']) + include_dirs=[get_numpy_include_dirs(), '../_shared']) config.add_extension('_template', sources=['_template.c'], - include_dirs=[get_numpy_include_dirs()]) + include_dirs=[get_numpy_include_dirs(), '../_shared']) return config