diff --git a/skimage/draw/_draw.pyx b/skimage/draw/_draw.pyx index efd5fdc5..2b319094 100644 --- a/skimage/draw/_draw.pyx +++ b/skimage/draw/_draw.pyx @@ -2,11 +2,11 @@ #cython: boundscheck=False #cython: nonecheck=False #cython: wraparound=False -cimport cython -cimport numpy as np -from libc.math cimport sqrt import math import numpy as np + +cimport numpy as cnp +from libc.math cimport sqrt from skimage._shared.geometry cimport point_in_polygon @@ -29,7 +29,7 @@ def line(Py_ssize_t y, Py_ssize_t x, Py_ssize_t y2, Py_ssize_t x2): """ - cdef np.ndarray[np.intp_t, ndim=1, mode="c"] rr, cc + cdef cnp.ndarray[cnp.intp_t, ndim=1, mode="c"] rr, cc cdef char steep = 0 cdef Py_ssize_t dx = abs(x2 - x) @@ -110,11 +110,11 @@ def polygon(y, x, shape=None): cdef Py_ssize_t r, c #: make contigous arrays for r, c coordinates - cdef np.ndarray contiguous_rdata, contiguous_cdata + cdef cnp.ndarray contiguous_rdata, contiguous_cdata contiguous_rdata = np.ascontiguousarray(y, 'double') contiguous_cdata = np.ascontiguousarray(x, 'double') - cdef np.double_t* rptr = contiguous_rdata.data - cdef np.double_t* cptr = contiguous_cdata.data + cdef cnp.double_t* rptr = contiguous_rdata.data + cdef cnp.double_t* cptr = contiguous_cdata.data #: output coordinate arrays cdef list rr = list() diff --git a/skimage/feature/_template.pyx b/skimage/feature/_template.pyx index 842e1eda..03695959 100644 --- a/skimage/feature/_template.pyx +++ b/skimage/feature/_template.pyx @@ -1,3 +1,8 @@ +#cython: cdivision=True +#cython: boundscheck=False +#cython: nonecheck=False +#cython: wraparound=False + """ Template matching using normalized cross-correlation. @@ -30,22 +35,24 @@ the image window *before* squaring.) .. [2] J. P. Lewis, "Fast Normalized Cross-Correlation", Industrial Light and Magic. """ -import cython -cimport numpy as np + import numpy as np from scipy.signal import fftconvolve -from skimage.transform import integral + +cimport numpy as cnp from libc.math cimport sqrt, fabs from skimage._shared.transform cimport integrate -@cython.boundscheck(False) -def match_template(np.ndarray[float, ndim=2, mode="c"] image, - np.ndarray[float, ndim=2, mode="c"] template): +from skimage.transform import integral - cdef np.ndarray[float, ndim=2, mode="c"] corr - cdef np.ndarray[float, ndim=2, mode="c"] image_sat - cdef np.ndarray[float, ndim=2, mode="c"] image_sqr_sat + +def match_template(cnp.ndarray[float, ndim=2, mode="c"] image, + cnp.ndarray[float, ndim=2, mode="c"] template): + + cdef cnp.ndarray[float, ndim=2, mode="c"] corr + cdef cnp.ndarray[float, ndim=2, mode="c"] image_sat + cdef cnp.ndarray[float, ndim=2, mode="c"] image_sqr_sat cdef float template_mean = np.mean(template) cdef float template_ssd cdef float inv_area @@ -88,4 +95,3 @@ def match_template(np.ndarray[float, ndim=2, mode="c"] image, corr[r, c] /= den return corr - diff --git a/skimage/feature/_texture.pyx b/skimage/feature/_texture.pyx index ffff38d7..f98ed4ca 100644 --- a/skimage/feature/_texture.pyx +++ b/skimage/feature/_texture.pyx @@ -3,20 +3,20 @@ #cython: nonecheck=False #cython: wraparound=False import numpy as np -cimport numpy as np +cimport numpy as cnp from libc.math cimport sin, cos, abs from skimage._shared.interpolation cimport bilinear_interpolation -def _glcm_loop(np.ndarray[dtype=np.uint8_t, ndim=2, - negative_indices=False, mode='c'] image, - np.ndarray[dtype=np.float64_t, ndim=1, - negative_indices=False, mode='c'] distances, - np.ndarray[dtype=np.float64_t, ndim=1, - negative_indices=False, mode='c'] angles, +def _glcm_loop(cnp.ndarray[dtype=cnp.uint8_t, ndim=2, + negative_indices=False, mode='c'] image, + cnp.ndarray[dtype=cnp.float64_t, ndim=1, + negative_indices=False, mode='c'] distances, + cnp.ndarray[dtype=cnp.float64_t, ndim=1, + negative_indices=False, mode='c'] angles, int levels, - np.ndarray[dtype=np.uint32_t, ndim=4, - negative_indices=False, mode='c'] out): + cnp.ndarray[dtype=cnp.uint32_t, ndim=4, + negative_indices=False, mode='c'] out): """Perform co-occurrence matrix accumulation. Parameters @@ -39,8 +39,8 @@ def _glcm_loop(np.ndarray[dtype=np.uint8_t, ndim=2, cdef: Py_ssize_t a_idx, d_idx, r, c, rows, cols, row, col - np.uint8_t i, j - np.float64_t angle, distance + cnp.uint8_t i, j + cnp.float64_t angle, distance rows = image.shape[0] cols = image.shape[1] @@ -81,7 +81,7 @@ cdef inline int _bit_rotate_right(int value, int length): return (value >> 1) | ((value & 1) << (length - 1)) -def _local_binary_pattern(np.ndarray[double, ndim=2] image, +def _local_binary_pattern(cnp.ndarray[double, ndim=2] image, int P, float R, char method='D'): """Gray scale and rotation invariant LBP (Local Binary Patterns). @@ -111,19 +111,19 @@ def _local_binary_pattern(np.ndarray[double, ndim=2] image, """ # texture weights - cdef np.ndarray[int, ndim=1] weights = 2 ** np.arange(P, dtype=np.int32) + cdef cnp.ndarray[int, ndim=1] weights = 2 ** np.arange(P, dtype=np.int32) # local position of texture elements rp = - R * np.sin(2 * np.pi * np.arange(P, dtype=np.double) / P) cp = R * np.cos(2 * np.pi * np.arange(P, dtype=np.double) / P) - cdef np.ndarray[double, ndim=2] coords = np.round(np.vstack([rp, cp]).T, 5) + cdef cnp.ndarray[double, ndim=2] coords = np.round(np.vstack([rp, cp]).T, 5) # pre allocate arrays for computation - cdef np.ndarray[double, ndim=1] texture = np.zeros(P, np.double) - cdef np.ndarray[char, ndim=1] signed_texture = np.zeros(P, np.int8) - cdef np.ndarray[int, ndim=1] rotation_chain = np.zeros(P, np.int32) + cdef cnp.ndarray[double, ndim=1] texture = np.zeros(P, np.double) + cdef cnp.ndarray[char, ndim=1] signed_texture = np.zeros(P, np.int8) + cdef cnp.ndarray[int, ndim=1] rotation_chain = np.zeros(P, np.int32) output_shape = (image.shape[0], image.shape[1]) - cdef np.ndarray[double, ndim=2] output = np.zeros(output_shape, np.double) + cdef cnp.ndarray[double, ndim=2] output = np.zeros(output_shape, np.double) cdef Py_ssize_t rows = image.shape[0] cdef Py_ssize_t cols = image.shape[1] diff --git a/skimage/filter/_ctmf.pyx b/skimage/filter/_ctmf.pyx index 65feb9e4..1a03ff5a 100644 --- a/skimage/filter/_ctmf.pyx +++ b/skimage/filter/_ctmf.pyx @@ -10,18 +10,20 @@ Copyright (c) 2009-2011 Broad Institute All rights reserved. Original author: Lee Kamentsky ''' + import numpy as np -cimport numpy as np + +cimport numpy as cnp cimport cython from libc.stdlib cimport malloc, free from libc.string cimport memset -cdef extern from "../_shared/vectorized_ops.h": - void add16(np.uint16_t *dest, np.uint16_t *src) - void sub16(np.uint16_t *dest, np.uint16_t *src) -np.import_array() +cdef extern from "../_shared/vectorized_ops.h": + void add16(cnp.uint16_t *dest, cnp.uint16_t *src) + void sub16(cnp.uint16_t *dest, cnp.uint16_t *src) + ############################################################################## # @@ -43,7 +45,7 @@ np.import_array() DTYPE_UINT32 = np.uint32 DTYPE_BOOL = np.bool -ctypedef np.uint16_t pixel_count_t +ctypedef cnp.uint16_t pixel_count_t ########### # @@ -58,8 +60,8 @@ ctypedef np.uint16_t pixel_count_t ########### cdef struct HistogramPiece: - np.uint16_t coarse[16] - np.uint16_t fine[256] + cnp.uint16_t coarse[16] + cnp.uint16_t fine[256] cdef struct Histogram: HistogramPiece top_left # top-left corner @@ -92,9 +94,9 @@ cdef struct Histograms: void *memory # pointer to the allocated memory Histogram *histogram # pointer to the histogram memory PixelCount *pixel_count # pointer to the pixel count memory - np.uint8_t *data # pointer to the image data - np.uint8_t *mask # pointer to the image mask - np.uint8_t *output # pointer to the output array + cnp.uint8_t *data # pointer to the image data + cnp.uint8_t *mask # pointer to the image mask + cnp.uint8_t *output # pointer to the output array Py_ssize_t column_count # number of columns represented by this # structure Py_ssize_t stripe_length # number of columns including "radius" before @@ -172,15 +174,15 @@ cdef Histograms *allocate_histograms(Py_ssize_t rows, Py_ssize_t col_stride, Py_ssize_t radius, Py_ssize_t percent, - np.uint8_t *data, - np.uint8_t *mask, - np.uint8_t *output): + cnp.uint8_t *data, + cnp.uint8_t *mask, + cnp.uint8_t *output): cdef: Py_ssize_t adjusted_stripe_length = columns + 2*radius + 1 Py_ssize_t memory_size void *ptr Histograms *ph - size_t roundoff + Py_ssize_t roundoff Py_ssize_t a SCoord *psc @@ -232,7 +234,7 @@ cdef Histograms *allocate_histograms(Py_ssize_t rows, # a_2 is the offset from the center to each of the octagon # corners # - a = (radius * 2.0 / 2.414213) + a = (radius * 2.0 / 2.414213) a_2 = a / 2 if a_2 == 0: a_2 = 1 @@ -456,7 +458,7 @@ cdef inline void deaccumulate_fine_histogram(Histograms *ph, ############################################################################ cdef inline void accumulate(Histograms *ph): - cdef np.int32_t accumulator + cdef cnp.int32_t accumulator accumulate_coarse_histogram(ph, ph.current_column) deaccumulate_coarse_histogram(ph, ph.current_column) @@ -514,7 +516,7 @@ cdef inline void update_histogram(Histograms *ph, Py_ssize_t current_stride = ph.current_stride Py_ssize_t column_count = ph.column_count Py_ssize_t row_count = ph.row_count - np.uint8_t value + cnp.uint8_t value Py_ssize_t stride Py_ssize_t x Py_ssize_t y @@ -557,8 +559,8 @@ cdef inline void update_current_location(Histograms *ph): Py_ssize_t bottom_left_off = tr_bl_colidx(ph, current_column) Py_ssize_t bottom_right_off = tl_br_colidx(ph, current_column) Py_ssize_t leading_edge_off = leading_edge_colidx(ph, current_column) - np.int32_t *coarse_histogram - np.int32_t *fine_histogram + cnp.int32_t *coarse_histogram + cnp.int32_t *fine_histogram Py_ssize_t last_xoff Py_ssize_t last_yoff Py_ssize_t last_stride @@ -597,13 +599,13 @@ cdef inline void update_current_location(Histograms *ph): # ############################################################################ -cdef inline np.uint8_t find_median(Histograms *ph): +cdef inline cnp.uint8_t find_median(Histograms *ph): cdef: Py_ssize_t pixels_below # of pixels below the median Py_ssize_t i Py_ssize_t j Py_ssize_t k - np.uint32_t accumulator + cnp.uint32_t accumulator if ph.accumulator_count == 0: return 0 @@ -625,7 +627,7 @@ cdef inline np.uint8_t find_median(Histograms *ph): for j in range(i*16, (i + 1)*16): accumulator += ph.accumulator.fine[j] if accumulator > pixels_below: - return j + return j return 0 @@ -650,9 +652,9 @@ cdef int c_median_filter(Py_ssize_t rows, Py_ssize_t col_stride, Py_ssize_t radius, Py_ssize_t percent, - np.uint8_t *data, - np.uint8_t *mask, - np.uint8_t *output): + cnp.uint8_t *data, + cnp.uint8_t *mask, + cnp.uint8_t *output): cdef: Histograms *ph Histogram *phistogram @@ -731,11 +733,14 @@ cdef int c_median_filter(Py_ssize_t rows, return 0 -def median_filter(np.ndarray[dtype=np.uint8_t, ndim=2, negative_indices=False, mode='c'] data, - np.ndarray[dtype=np.uint8_t, ndim=2, negative_indices=False, mode='c'] mask, - np.ndarray[dtype=np.uint8_t, ndim=2, negative_indices=False, mode='c'] output, +def median_filter(cnp.ndarray[dtype=cnp.uint8_t, ndim=2, + negative_indices=False, mode='c'] data, + cnp.ndarray[dtype=cnp.uint8_t, ndim=2, + negative_indices=False, mode='c'] mask, + cnp.ndarray[dtype=cnp.uint8_t, ndim=2, + negative_indices=False, mode='c'] output, int radius, - np.int32_t percent): + cnp.int32_t percent): """Median filter with octagon shape and masking. Parameters @@ -770,10 +775,12 @@ def median_filter(np.ndarray[dtype=np.uint8_t, ndim=2, negative_indices=False, m raise ValueError('Data shape (%d, %d) is not output shape (%d, %d)' % (data.shape[0], data.shape[1], output.shape[0], output.shape[1])) - if c_median_filter( data.shape[0], data.shape[1], - data.strides[0], data.strides[1], + if c_median_filter(data.shape[0], + data.shape[1], + data.strides[0], + data.strides[1], radius, percent, - data.data, - mask.data, - output.data): + data.data, + mask.data, + output.data): raise MemoryError('Failed to allocate scratchpad memory') diff --git a/skimage/filter/rank/_core16.pxd b/skimage/filter/rank/_core16.pxd index 5f7de9df..5586aea1 100644 --- a/skimage/filter/rank/_core16.pxd +++ b/skimage/filter/rank/_core16.pxd @@ -1,4 +1,7 @@ -cimport numpy as np +cimport numpy as cnp + + +ctypedef cnp.uint16_t dtype_t cdef int int_max(int a, int b) @@ -6,12 +9,12 @@ cdef int int_min(int a, int b) # 16-bit core kernel receives extra information about data bitdepth -cdef void _core16(np.uint16_t kernel(Py_ssize_t *, float, np.uint16_t, - Py_ssize_t, Py_ssize_t, Py_ssize_t, float, - float, Py_ssize_t, Py_ssize_t), - np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask, - np.ndarray[np.uint16_t, ndim=2] out, +cdef void _core16(dtype_t kernel(Py_ssize_t *, float, dtype_t, + Py_ssize_t, Py_ssize_t, Py_ssize_t, float, + float, Py_ssize_t, Py_ssize_t), + cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask, + cnp.ndarray[dtype_t, ndim=2] out, char shift_x, char shift_y, Py_ssize_t bitdepth, float p0, float p1, Py_ssize_t s0, Py_ssize_t s1) except * diff --git a/skimage/filter/rank/_core16.pyx b/skimage/filter/rank/_core16.pyx index aa959fd0..36bc500d 100644 --- a/skimage/filter/rank/_core16.pyx +++ b/skimage/filter/rank/_core16.pyx @@ -4,7 +4,8 @@ #cython: wraparound=False import numpy as np -cimport numpy as np + +cimport numpy as cnp from libc.stdlib cimport malloc, free from _core8 cimport is_in_mask @@ -18,24 +19,24 @@ cdef inline int int_min(int a, int b): cdef inline void histogram_increment(Py_ssize_t * histo, float * pop, - np.uint16_t value): + dtype_t value): histo[value] += 1 pop[0] += 1 cdef inline void histogram_decrement(Py_ssize_t * histo, float * pop, - np.uint16_t value): + dtype_t value): histo[value] -= 1 pop[0] -= 1 -cdef void _core16(np.uint16_t kernel(Py_ssize_t *, float, np.uint16_t, - Py_ssize_t, Py_ssize_t, Py_ssize_t, float, - float, Py_ssize_t, Py_ssize_t), - np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask, - np.ndarray[np.uint16_t, ndim=2] out, +cdef void _core16(dtype_t kernel(Py_ssize_t *, float, dtype_t, + Py_ssize_t, Py_ssize_t, Py_ssize_t, float, + float, Py_ssize_t, Py_ssize_t), + cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask, + cnp.ndarray[dtype_t, ndim=2] out, char shift_x, char shift_y, Py_ssize_t bitdepth, float p0, float p1, Py_ssize_t s0, Py_ssize_t s1) except *: """Compute histogram for each pixel neighborhood, apply kernel function and @@ -67,9 +68,9 @@ cdef void _core16(np.uint16_t kernel(Py_ssize_t *, float, np.uint16_t, assert (image < maxbin).all() # define pointers to the data - cdef np.uint16_t * out_data = out.data - cdef np.uint16_t * image_data = image.data - cdef np.uint8_t * mask_data = mask.data + cdef dtype_t * out_data = out.data + cdef dtype_t * image_data = image.data + cdef cnp.uint8_t * mask_data = mask.data # define local variable types cdef Py_ssize_t r, c, rr, cc, s, value, local_max, i, even_row diff --git a/skimage/filter/rank/_core8.pxd b/skimage/filter/rank/_core8.pxd index 38236b87..d3b6d8c2 100644 --- a/skimage/filter/rank/_core8.pxd +++ b/skimage/filter/rank/_core8.pxd @@ -1,22 +1,25 @@ -cimport numpy as np +cimport numpy as cnp -cdef np.uint8_t uint8_max(np.uint8_t a, np.uint8_t b) -cdef np.uint8_t uint8_min(np.uint8_t a, np.uint8_t b) +ctypedef cnp.uint8_t dtype_t -cdef np.uint8_t is_in_mask(Py_ssize_t rows, Py_ssize_t cols, - Py_ssize_t r, Py_ssize_t c, - np.uint8_t * mask) +cdef dtype_t uint8_max(dtype_t a, dtype_t b) +cdef dtype_t uint8_min(dtype_t a, dtype_t b) + + +cdef dtype_t is_in_mask(Py_ssize_t rows, Py_ssize_t cols, + Py_ssize_t r, Py_ssize_t c, + dtype_t * mask) # 8-bit core kernel receives extra information about data inferior and superior # percentiles -cdef void _core8(np.uint8_t kernel(Py_ssize_t *, float, np.uint8_t, float, - float, Py_ssize_t, Py_ssize_t), - np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask, - np.ndarray[np.uint8_t, ndim=2] out, +cdef void _core8(dtype_t kernel(Py_ssize_t *, float, dtype_t, float, + float, Py_ssize_t, Py_ssize_t), + cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask, + cnp.ndarray[dtype_t, ndim=2] out, char shift_x, char shift_y, float p0, float p1, Py_ssize_t s0, Py_ssize_t s1) except * diff --git a/skimage/filter/rank/_core8.pyx b/skimage/filter/rank/_core8.pyx index 9f4bc693..eca47891 100644 --- a/skimage/filter/rank/_core8.pyx +++ b/skimage/filter/rank/_core8.pyx @@ -4,33 +4,34 @@ #cython: wraparound=False import numpy as np -cimport numpy as np + +cimport numpy as cnp from libc.stdlib cimport malloc, free -cdef inline np.uint8_t uint8_max(np.uint8_t a, np.uint8_t b): +cdef inline dtype_t uint8_max(dtype_t a, dtype_t b): return a if a >= b else b -cdef inline np.uint8_t uint8_min(np.uint8_t a, np.uint8_t b): +cdef inline dtype_t uint8_min(dtype_t a, dtype_t b): return a if a <= b else b cdef inline void histogram_increment(Py_ssize_t * histo, float * pop, - np.uint8_t value): + dtype_t value): histo[value] += 1 pop[0] += 1 cdef inline void histogram_decrement(Py_ssize_t * histo, float * pop, - np.uint8_t value): + dtype_t value): histo[value] -= 1 pop[0] -= 1 -cdef inline np.uint8_t is_in_mask(Py_ssize_t rows, Py_ssize_t cols, - Py_ssize_t r, Py_ssize_t c, - np.uint8_t * mask): +cdef inline dtype_t is_in_mask(Py_ssize_t rows, Py_ssize_t cols, + Py_ssize_t r, Py_ssize_t c, + dtype_t * mask): """Check whether given coordinate is within image and mask is true.""" if r < 0 or r > rows - 1 or c < 0 or c > cols - 1: return 0 @@ -41,12 +42,12 @@ cdef inline np.uint8_t is_in_mask(Py_ssize_t rows, Py_ssize_t cols, return 0 -cdef void _core8(np.uint8_t kernel(Py_ssize_t *, float, np.uint8_t, float, - float, Py_ssize_t, Py_ssize_t), - np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask, - np.ndarray[np.uint8_t, ndim=2] out, +cdef void _core8(dtype_t kernel(Py_ssize_t *, float, dtype_t, float, + float, Py_ssize_t, Py_ssize_t), + cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask, + cnp.ndarray[dtype_t, ndim=2] out, char shift_x, char shift_y, float p0, float p1, Py_ssize_t s0, Py_ssize_t s1) except *: """Compute histogram for each pixel neighborhood, apply kernel function and @@ -69,9 +70,9 @@ cdef void _core8(np.uint8_t kernel(Py_ssize_t *, float, np.uint8_t, float, # define pointers to the data - cdef np.uint8_t * out_data = out.data - cdef np.uint8_t * image_data = image.data - cdef np.uint8_t * mask_data = mask.data + cdef dtype_t * out_data = out.data + cdef dtype_t * image_data = image.data + cdef dtype_t * mask_data = mask.data # define local variable types cdef Py_ssize_t r, c, rr, cc, s, value, local_max, i, even_row diff --git a/skimage/filter/rank/_crank16.pyx b/skimage/filter/rank/_crank16.pyx index e81bf81f..87199e80 100644 --- a/skimage/filter/rank/_crank16.pyx +++ b/skimage/filter/rank/_crank16.pyx @@ -3,8 +3,7 @@ #cython: nonecheck=False #cython: wraparound=False -import numpy as np -cimport numpy as np +cimport numpy as cnp from libc.math cimport log2 from skimage.filter.rank._core16 cimport _core16 @@ -14,11 +13,14 @@ from skimage.filter.rank._core16 cimport _core16 # ----------------------------------------------------------------- -cdef inline np.uint16_t kernel_autolevel(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +ctypedef cnp.uint16_t dtype_t + + +cdef inline dtype_t kernel_autolevel(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i, imin, imax, delta if pop: @@ -32,16 +34,16 @@ cdef inline np.uint16_t kernel_autolevel(Py_ssize_t * histo, float pop, break delta = imax - imin if delta > 0: - return < np.uint16_t > (1. * (maxbin - 1) * (g - imin) / delta) + return (1. * (maxbin - 1) * (g - imin) / delta) else: - return < np.uint16_t > (imax - imin) + return (imax - imin) -cdef inline np.uint16_t kernel_bottomhat(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_bottomhat(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i if pop: @@ -49,15 +51,15 @@ cdef inline np.uint16_t kernel_bottomhat(Py_ssize_t * histo, float pop, if histo[i]: break - return < np.uint16_t > (g - i) + return (g - i) else: - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_equalize(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_equalize(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i cdef float sum = 0. @@ -67,16 +69,16 @@ cdef inline np.uint16_t kernel_equalize(Py_ssize_t * histo, float pop, if i >= g: break - return < np.uint16_t > (((maxbin - 1) * sum) / pop) + return (((maxbin - 1) * sum) / pop) else: - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_gradient(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_gradient(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i, imin, imax if pop: @@ -88,66 +90,66 @@ cdef inline np.uint16_t kernel_gradient(Py_ssize_t * histo, float pop, if histo[i]: imin = i break - return < np.uint16_t > (imax - imin) + return (imax - imin) else: - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_maximum(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_maximum(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i if pop: for i in range(maxbin - 1, -1, -1): if histo[i]: - return < np.uint16_t > (i) + return (i) - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_mean(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_mean(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i cdef float mean = 0. if pop: for i in range(maxbin): mean += histo[i] * i - return < np.uint16_t > (mean / pop) + return (mean / pop) else: - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_meansubstraction(Py_ssize_t * histo, - float pop, - np.uint16_t g, - Py_ssize_t bitdepth, - Py_ssize_t maxbin, - Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_meansubstraction(Py_ssize_t * histo, + float pop, + dtype_t g, + Py_ssize_t bitdepth, + Py_ssize_t maxbin, + Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i cdef float mean = 0. if pop: for i in range(maxbin): mean += histo[i] * i - return < np.uint16_t > ((g - mean / pop) / 2. + (midbin - 1)) + return ((g - mean / pop) / 2. + (midbin - 1)) else: - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_median(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_median(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i cdef float sum = pop / 2.0 @@ -156,31 +158,31 @@ cdef inline np.uint16_t kernel_median(Py_ssize_t * histo, float pop, if histo[i]: sum -= histo[i] if sum < 0: - return < np.uint16_t > (i) + return (i) else: - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_minimum(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_minimum(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i if pop: for i in range(maxbin): if histo[i]: - return < np.uint16_t > (i) + return (i) else: - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_modal(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_modal(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t hmax = 0, imax = 0 if pop: @@ -188,19 +190,19 @@ cdef inline np.uint16_t kernel_modal(Py_ssize_t * histo, float pop, if histo[i] > hmax: hmax = histo[i] imax = i - return < np.uint16_t > (imax) + return (imax) else: - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_morph_contr_enh(Py_ssize_t * histo, - float pop, - np.uint16_t g, - Py_ssize_t bitdepth, - Py_ssize_t maxbin, - Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_morph_contr_enh(Py_ssize_t * histo, + float pop, + dtype_t g, + Py_ssize_t bitdepth, + Py_ssize_t maxbin, + Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i, imin, imax if pop: @@ -213,42 +215,42 @@ cdef inline np.uint16_t kernel_morph_contr_enh(Py_ssize_t * histo, imin = i break if imax - g < g - imin: - return < np.uint16_t > (imax) + return (imax) else: - return < np.uint16_t > (imin) + return (imin) else: - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_pop(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): - return < np.uint16_t > (pop) +cdef inline dtype_t kernel_pop(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): + return (pop) -cdef inline np.uint16_t kernel_threshold(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_threshold(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i cdef float mean = 0. if pop: for i in range(maxbin): mean += histo[i] * i - return < np.uint16_t > (g > (mean / pop)) + return (g > (mean / pop)) else: - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_tophat(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_tophat(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i if pop: @@ -256,15 +258,15 @@ cdef inline np.uint16_t kernel_tophat(Py_ssize_t * histo, float pop, if histo[i]: break - return < np.uint16_t > (i - g) + return (i - g) else: - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_entropy(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_entropy(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i cdef float e, p @@ -276,145 +278,145 @@ cdef inline np.uint16_t kernel_entropy(Py_ssize_t * histo, float pop, if p > 0: e -= p * log2(p) - return < np.uint16_t > e * 1000 + return e * 1000 else: - return < np.uint16_t > (0) + return (0) # ----------------------------------------------------------------- # python wrappers # ----------------------------------------------------------------- -def autolevel(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def autolevel(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_autolevel, image, selem, mask, out, shift_x, shift_y, bitdepth, 0, 0, 0, 0) -def bottomhat(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def bottomhat(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_bottomhat, image, selem, mask, out, shift_x, shift_y, bitdepth, 0, 0, 0, 0) -def equalize(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def equalize(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_equalize, image, selem, mask, out, shift_x, shift_y, bitdepth, 0, 0, 0, 0) -def gradient(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def gradient(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_gradient, image, selem, mask, out, shift_x, shift_y, bitdepth, 0, 0, 0, 0) -def maximum(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def maximum(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_maximum, image, selem, mask, out, shift_x, shift_y, bitdepth, 0, 0, 0, 0) -def mean(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def mean(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_mean, image, selem, mask, out, shift_x, shift_y, bitdepth, 0, 0, 0, 0) -def meansubstraction(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def meansubstraction(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_meansubstraction, image, selem, mask, out, shift_x, shift_y, bitdepth, 0, 0, 0, 0) -def median(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def median(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_median, image, selem, mask, out, shift_x, shift_y, bitdepth, 0, 0, 0, 0) -def minimum(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def minimum(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_minimum, image, selem, mask, out, shift_x, shift_y, bitdepth, 0, 0, 0, 0) -def morph_contr_enh(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def morph_contr_enh(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_morph_contr_enh, image, selem, mask, out, shift_x, shift_y, bitdepth, 0, 0, 0, 0) -def modal(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def modal(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_modal, image, selem, mask, out, shift_x, shift_y, bitdepth, 0, 0, 0, 0) -def pop(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def pop(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_pop, image, selem, mask, out, shift_x, shift_y, bitdepth, 0, 0, 0, 0) -def threshold(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def threshold(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_threshold, image, selem, mask, out, shift_x, shift_y, bitdepth, 0, 0, 0, 0) -def tophat(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def tophat(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_tophat, image, selem, mask, out, shift_x, shift_y, bitdepth, 0, 0, 0, 0) -def entropy(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def entropy(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_entropy, image, selem, mask, out, shift_x, shift_y, bitdepth, 0, 0, 0, 0) diff --git a/skimage/filter/rank/_crank16_bilateral.pyx b/skimage/filter/rank/_crank16_bilateral.pyx index c71ccc5c..e431e42b 100644 --- a/skimage/filter/rank/_crank16_bilateral.pyx +++ b/skimage/filter/rank/_crank16_bilateral.pyx @@ -3,8 +3,7 @@ #cython: nonecheck=False #cython: wraparound=False -import numpy as np -cimport numpy as np +cimport numpy as cnp from skimage.filter.rank._core16 cimport _core16 @@ -13,11 +12,14 @@ from skimage.filter.rank._core16 cimport _core16 # ----------------------------------------------------------------- -cdef inline np.uint16_t kernel_mean(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +ctypedef cnp.uint16_t dtype_t + + +cdef inline dtype_t kernel_mean(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef int i, bilat_pop = 0 cdef float mean = 0. @@ -28,18 +30,18 @@ cdef inline np.uint16_t kernel_mean(Py_ssize_t * histo, float pop, bilat_pop += histo[i] mean += histo[i] * i if bilat_pop: - return < np.uint16_t > (mean / bilat_pop) + return (mean / bilat_pop) else: - return < np.uint16_t > (0) + return (0) else: - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_pop(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_pop(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef int i, bilat_pop = 0 @@ -47,9 +49,9 @@ cdef inline np.uint16_t kernel_pop(Py_ssize_t * histo, float pop, for i in range(maxbin): if (g > (i - s0)) and (g < (i + s1)): bilat_pop += histo[i] - return < np.uint16_t > (bilat_pop) + return (bilat_pop) else: - return < np.uint16_t > (0) + return (0) # ----------------------------------------------------------------- @@ -57,10 +59,10 @@ cdef inline np.uint16_t kernel_pop(Py_ssize_t * histo, float pop, # ----------------------------------------------------------------- -def mean(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def mean(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, int bitdepth=8, int s0=1, int s1=1): """average greylevel (clipped on uint8) """ @@ -68,10 +70,10 @@ def mean(np.ndarray[np.uint16_t, ndim=2] image, bitdepth, 0., 0., s0, s1) -def pop(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def pop(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, int bitdepth=8, int s0=1, int s1=1): """returns the number of actual pixels of the structuring element inside the mask diff --git a/skimage/filter/rank/_crank16_percentiles.pyx b/skimage/filter/rank/_crank16_percentiles.pyx index 220d2386..0ab71353 100644 --- a/skimage/filter/rank/_crank16_percentiles.pyx +++ b/skimage/filter/rank/_crank16_percentiles.pyx @@ -3,8 +3,7 @@ #cython: nonecheck=False #cython: wraparound=False -import numpy as np -cimport numpy as np +cimport numpy as cnp from skimage.filter.rank._core16 cimport _core16, int_min, int_max @@ -13,11 +12,14 @@ from skimage.filter.rank._core16 cimport _core16, int_min, int_max # ----------------------------------------------------------------- -cdef inline np.uint16_t kernel_autolevel(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +ctypedef cnp.uint16_t dtype_t + + +cdef inline dtype_t kernel_autolevel(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef int i, imin, imax, sum, delta @@ -38,19 +40,20 @@ cdef inline np.uint16_t kernel_autolevel(Py_ssize_t * histo, float pop, delta = imax - imin if delta > 0: - return < np.uint16_t > (1.0 * (maxbin - 1) - * (int_min(int_max(imin, g), imax) - imin) / delta) + return (1.0 * (maxbin - 1) + * (int_min(int_max(imin, g), imax) + - imin) / delta) else: - return < np.uint16_t > (imax - imin) + return (imax - imin) else: - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_gradient(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_gradient(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef int i, imin, imax, sum, delta @@ -69,16 +72,16 @@ cdef inline np.uint16_t kernel_gradient(Py_ssize_t * histo, float pop, imax = i break - return < np.uint16_t > (imax - imin) + return (imax - imin) else: - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_mean(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_mean(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef int i, sum, mean, n @@ -93,21 +96,21 @@ cdef inline np.uint16_t kernel_mean(Py_ssize_t * histo, float pop, mean += histo[i] * i if n > 0: - return < np.uint16_t > (1.0 * mean / n) + return (1.0 * mean / n) else: - return < np.uint16_t > (0) + return (0) else: - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_mean_substraction(Py_ssize_t * histo, - float pop, - np.uint16_t g, - Py_ssize_t bitdepth, - Py_ssize_t maxbin, - Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_mean_substraction(Py_ssize_t * histo, + float pop, + dtype_t g, + Py_ssize_t bitdepth, + Py_ssize_t maxbin, + Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef int i, sum, mean, n @@ -121,21 +124,21 @@ cdef inline np.uint16_t kernel_mean_substraction(Py_ssize_t * histo, n += histo[i] mean += histo[i] * i if n > 0: - return < np.uint16_t > ((g - (mean / n)) * .5 + midbin) + return ((g - (mean / n)) * .5 + midbin) else: - return < np.uint16_t > (0) + return (0) else: - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_morph_contr_enh(Py_ssize_t * histo, - float pop, - np.uint16_t g, - Py_ssize_t bitdepth, - Py_ssize_t maxbin, - Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_morph_contr_enh(Py_ssize_t * histo, + float pop, + dtype_t g, + Py_ssize_t bitdepth, + Py_ssize_t maxbin, + Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef int i, imin, imax, sum, delta @@ -154,22 +157,22 @@ cdef inline np.uint16_t kernel_morph_contr_enh(Py_ssize_t * histo, imax = i break if g > imax: - return < np.uint16_t > imax + return imax if g < imin: - return < np.uint16_t > imin + return imin if imax - g < g - imin: - return < np.uint16_t > imax + return imax else: - return < np.uint16_t > imin + return imin else: - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_percentile(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_percentile(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef int i cdef float sum = 0. @@ -180,16 +183,16 @@ cdef inline np.uint16_t kernel_percentile(Py_ssize_t * histo, float pop, if sum >= p0 * pop: break - return < np.uint16_t > (i) + return (i) else: - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_pop(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_pop(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef int i, sum, n @@ -200,16 +203,16 @@ cdef inline np.uint16_t kernel_pop(Py_ssize_t * histo, float pop, sum += histo[i] if (sum >= p0 * pop) and (sum <= p1 * pop): n += histo[i] - return < np.uint16_t > (n) + return (n) else: - return < np.uint16_t > (0) + return (0) -cdef inline np.uint16_t kernel_threshold(Py_ssize_t * histo, float pop, - np.uint16_t g, Py_ssize_t bitdepth, - Py_ssize_t maxbin, Py_ssize_t midbin, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_threshold(Py_ssize_t * histo, float pop, + dtype_t g, Py_ssize_t bitdepth, + Py_ssize_t maxbin, Py_ssize_t midbin, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef int i cdef float sum = 0. @@ -220,9 +223,9 @@ cdef inline np.uint16_t kernel_threshold(Py_ssize_t * histo, float pop, if sum >= p0 * pop: break - return < np.uint16_t > ((maxbin - 1) * (g >= i)) + return ((maxbin - 1) * (g >= i)) else: - return < np.uint16_t > (0) + return (0) # ----------------------------------------------------------------- @@ -230,10 +233,10 @@ cdef inline np.uint16_t kernel_threshold(Py_ssize_t * histo, float pop, # ----------------------------------------------------------------- -def autolevel(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def autolevel(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, int bitdepth=8, float p0=0., float p1=0.): """bottom hat @@ -242,10 +245,10 @@ def autolevel(np.ndarray[np.uint16_t, ndim=2] image, bitdepth, p0, p1, 0, 0) -def gradient(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def gradient(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, int bitdepth=8, float p0=0., float p1=0.): """return p0,p1 percentile gradient @@ -254,10 +257,10 @@ def gradient(np.ndarray[np.uint16_t, ndim=2] image, bitdepth, p0, p1, 0, 0) -def mean(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def mean(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, int bitdepth=8, float p0=0., float p1=0.): """return mean between [p0 and p1] percentiles @@ -266,10 +269,10 @@ def mean(np.ndarray[np.uint16_t, ndim=2] image, bitdepth, p0, p1, 0, 0) -def mean_substraction(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def mean_substraction(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, int bitdepth=8, float p0=0., float p1=0.): """return original - mean between [p0 and p1] percentiles *.5 +127 @@ -279,10 +282,10 @@ def mean_substraction(np.ndarray[np.uint16_t, ndim=2] image, bitdepth, p0, p1, 0, 0) -def morph_contr_enh(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def morph_contr_enh(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, int bitdepth=8, float p0=0., float p1=0.): """reforce contrast using percentiles @@ -291,10 +294,10 @@ def morph_contr_enh(np.ndarray[np.uint16_t, ndim=2] image, bitdepth, p0, p1, 0, 0) -def percentile(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def percentile(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, int bitdepth=8, float p0=0., float p1=0.): """return p0 percentile @@ -303,10 +306,10 @@ def percentile(np.ndarray[np.uint16_t, ndim=2] image, bitdepth, p0, p1, 0, 0) -def pop(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def pop(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, int bitdepth=8, float p0=0., float p1=0.): """return nb of pixels between [p0 and p1] @@ -315,10 +318,10 @@ def pop(np.ndarray[np.uint16_t, ndim=2] image, bitdepth, p0, p1, 0, 0) -def threshold(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, +def threshold(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[cnp.uint8_t, ndim=2] selem, + cnp.ndarray[cnp.uint8_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, int bitdepth=8, float p0=0., float p1=0.): """return (maxbin-1) if g > percentile p0 diff --git a/skimage/filter/rank/_crank8.pyx b/skimage/filter/rank/_crank8.pyx index 8bff9703..c9e8b14f 100644 --- a/skimage/filter/rank/_crank8.pyx +++ b/skimage/filter/rank/_crank8.pyx @@ -3,8 +3,7 @@ #cython: nonecheck=False #cython: wraparound=False -import numpy as np -cimport numpy as np +cimport numpy as cnp from libc.math cimport log2 from skimage.filter.rank._core8 cimport _core8 @@ -14,9 +13,12 @@ from skimage.filter.rank._core8 cimport _core8 # ----------------------------------------------------------------- -cdef inline np.uint8_t kernel_autolevel(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +ctypedef cnp.uint8_t dtype_t + + +cdef inline dtype_t kernel_autolevel(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i, imin, imax, delta @@ -31,16 +33,16 @@ cdef inline np.uint8_t kernel_autolevel(Py_ssize_t * histo, float pop, break delta = imax - imin if delta > 0: - return < np.uint8_t > (255. * (g - imin) / delta) + return (255. * (g - imin) / delta) else: - return < np.uint8_t > (imax - imin) + return (imax - imin) else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_bottomhat(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_bottomhat(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i @@ -49,14 +51,14 @@ cdef inline np.uint8_t kernel_bottomhat(Py_ssize_t * histo, float pop, if histo[i]: break - return < np.uint8_t > (g - i) + return (g - i) else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_equalize(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_equalize(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i cdef float sum = 0. @@ -67,14 +69,14 @@ cdef inline np.uint8_t kernel_equalize(Py_ssize_t * histo, float pop, if i >= g: break - return < np.uint8_t > ((255 * sum) / pop) + return ((255 * sum) / pop) else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_gradient(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_gradient(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i, imin, imax @@ -87,28 +89,28 @@ cdef inline np.uint8_t kernel_gradient(Py_ssize_t * histo, float pop, if histo[i]: imin = i break - return < np.uint8_t > (imax - imin) + return (imax - imin) else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_maximum(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_maximum(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i if pop: for i in range(255, -1, -1): if histo[i]: - return < np.uint8_t > (i) + return (i) else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_mean(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_mean(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i cdef float mean = 0. @@ -116,14 +118,14 @@ cdef inline np.uint8_t kernel_mean(Py_ssize_t * histo, float pop, if pop: for i in range(256): mean += histo[i] * i - return < np.uint8_t > (mean / pop) + return (mean / pop) else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_meansubstraction(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_meansubstraction(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i cdef float mean = 0. @@ -131,14 +133,14 @@ cdef inline np.uint8_t kernel_meansubstraction(Py_ssize_t * histo, float pop, if pop: for i in range(256): mean += histo[i] * i - return < np.uint8_t > ((g - mean / pop) / 2. + 127) + return ((g - mean / pop) / 2. + 127) else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_median(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_median(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i cdef float sum = pop / 2.0 @@ -148,28 +150,28 @@ cdef inline np.uint8_t kernel_median(Py_ssize_t * histo, float pop, if histo[i]: sum -= histo[i] if sum < 0: - return < np.uint8_t > (i) + return (i) else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_minimum(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_minimum(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i if pop: for i in range(256): if histo[i]: - return < np.uint8_t > (i) + return (i) else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_modal(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_modal(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t hmax = 0, imax = 0 @@ -178,14 +180,14 @@ cdef inline np.uint8_t kernel_modal(Py_ssize_t * histo, float pop, if histo[i] > hmax: hmax = histo[i] imax = i - return < np.uint8_t > (imax) + return (imax) else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_morph_contr_enh(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_morph_contr_enh(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i, imin, imax @@ -199,23 +201,23 @@ cdef inline np.uint8_t kernel_morph_contr_enh(Py_ssize_t * histo, float pop, imin = i break if imax - g < g - imin: - return < np.uint8_t > (imax) + return (imax) else: - return < np.uint8_t > (imin) + return (imin) else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_pop(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_pop(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): - return < np.uint8_t > (pop) + return (pop) -cdef inline np.uint8_t kernel_threshold(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_threshold(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i cdef float mean = 0. @@ -223,14 +225,14 @@ cdef inline np.uint8_t kernel_threshold(Py_ssize_t * histo, float pop, if pop: for i in range(256): mean += histo[i] * i - return < np.uint8_t > (g > (mean / pop)) + return (g > (mean / pop)) else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_tophat(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_tophat(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i @@ -239,20 +241,20 @@ cdef inline np.uint8_t kernel_tophat(Py_ssize_t * histo, float pop, if histo[i]: break - return < np.uint8_t > (i - g) + return (i - g) else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_noise_filter(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_noise_filter(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i cdef Py_ssize_t min_i # early stop if at least one pixel of the neighborhood has the same g if histo[g] > 0: - return < np.uint8_t > 0 + return 0 for i in range(g, -1, -1): if histo[i]: @@ -262,14 +264,14 @@ cdef inline np.uint8_t kernel_noise_filter(Py_ssize_t * histo, float pop, if histo[i]: break if i - g < min_i: - return < np.uint8_t > (i - g) + return (i - g) else: - return < np.uint8_t > min_i + return min_i -cdef inline np.uint8_t kernel_entropy(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_entropy(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i cdef float e, p @@ -281,13 +283,13 @@ cdef inline np.uint8_t kernel_entropy(Py_ssize_t * histo, float pop, if p > 0: e -= p * log2(p) - return < np.uint8_t > e * 10 + return e * 10 else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_otsu(Py_ssize_t * histo, float pop, np.uint8_t g, - float p0, float p1, Py_ssize_t s0, - Py_ssize_t s1): +cdef inline dtype_t kernel_otsu(Py_ssize_t * histo, float pop, dtype_t g, + float p0, float p1, Py_ssize_t s0, + Py_ssize_t s1): cdef Py_ssize_t i cdef Py_ssize_t max_i cdef float P, mu1, mu2, q1, new_q1, sigma_b, max_sigma_b @@ -299,7 +301,7 @@ cdef inline np.uint8_t kernel_otsu(Py_ssize_t * histo, float pop, np.uint8_t g, mu += histo[i] * i mu = (mu / pop) else: - return < np.uint8_t > (0) + return (0) # maximizing the between class variance max_i = 0 @@ -319,7 +321,7 @@ cdef inline np.uint8_t kernel_otsu(Py_ssize_t * histo, float pop, np.uint8_t g, max_i = i q1 = new_q1 - return < np.uint8_t > max_i + return max_i # ----------------------------------------------------------------- @@ -328,154 +330,154 @@ cdef inline np.uint8_t kernel_otsu(Py_ssize_t * histo, float pop, np.uint8_t g, # ----------------------------------------------------------------- -def autolevel(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def autolevel(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_autolevel, image, selem, mask, out, shift_x, shift_y, 0, 0, 0, 0) -def bottomhat(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def bottomhat(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_bottomhat, image, selem, mask, out, shift_x, shift_y, 0, 0, 0, 0) -def equalize(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def equalize(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_equalize, image, selem, mask, out, shift_x, shift_y, 0, 0, 0, 0) -def gradient(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def gradient(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_gradient, image, selem, mask, out, shift_x, shift_y, 0, 0, 0, 0) -def maximum(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def maximum(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_maximum, image, selem, mask, out, shift_x, shift_y, 0, 0, 0, 0) -def mean(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def mean(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_mean, image, selem, mask, out, shift_x, shift_y, 0, 0, 0, 0) -def meansubstraction(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def meansubstraction(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_meansubstraction, image, selem, mask, out, shift_x, shift_y, 0, 0, 0, 0) -def median(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def median(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_median, image, selem, mask, out, shift_x, shift_y, 0, 0, 0, 0) -def minimum(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def minimum(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_minimum, image, selem, mask, out, shift_x, shift_y, 0, 0, 0, 0) -def morph_contr_enh(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def morph_contr_enh(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_morph_contr_enh, image, selem, mask, out, shift_x, shift_y, 0, 0, 0, 0) -def modal(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def modal(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_modal, image, selem, mask, out, shift_x, shift_y, 0, 0, 0, 0) -def pop(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def pop(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_pop, image, selem, mask, out, shift_x, shift_y, 0, 0, 0, 0) -def threshold(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def threshold(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_threshold, image, selem, mask, out, shift_x, shift_y, 0, 0, 0, 0) -def tophat(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def tophat(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_tophat, image, selem, mask, out, shift_x, shift_y, 0, 0, 0, 0) -def noise_filter(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def noise_filter(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_noise_filter, image, selem, mask, out, shift_x, shift_y, 0, 0, 0, 0) -def entropy(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def entropy(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_entropy, image, selem, mask, out, shift_x, shift_y, 0, 0, 0, 0) -def otsu(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def otsu(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_otsu, image, selem, mask, out, shift_x, shift_y, 0, 0, 0, 0) diff --git a/skimage/filter/rank/_crank8_percentiles.pyx b/skimage/filter/rank/_crank8_percentiles.pyx index 31b00742..d085957e 100644 --- a/skimage/filter/rank/_crank8_percentiles.pyx +++ b/skimage/filter/rank/_crank8_percentiles.pyx @@ -3,8 +3,7 @@ #cython: nonecheck=False #cython: wraparound=False -import numpy as np -cimport numpy as np +cimport numpy as cnp from skimage.filter.rank._core8 cimport _core8, uint8_max, uint8_min @@ -13,9 +12,12 @@ from skimage.filter.rank._core8 cimport _core8, uint8_max, uint8_min # ----------------------------------------------------------------- -cdef inline np.uint8_t kernel_autolevel(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +ctypedef cnp.uint8_t dtype_t + + +cdef inline dtype_t kernel_autolevel(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef int i, imin, imax, sum, delta if pop: @@ -37,17 +39,17 @@ cdef inline np.uint8_t kernel_autolevel(Py_ssize_t * histo, float pop, break delta = imax - imin if delta > 0: - return < np.uint8_t > (255 - * (uint8_min(uint8_max(imin, g), imax) - imin) / delta) + return (255 * (uint8_min(uint8_max(imin, g), imax) + - imin) / delta) else: - return < np.uint8_t > (imax - imin) + return (imax - imin) else: - return < np.uint8_t > (128) + return (128) -cdef inline np.uint8_t kernel_gradient(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_gradient(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef int i, imin, imax, sum, delta if pop: @@ -65,14 +67,14 @@ cdef inline np.uint8_t kernel_gradient(Py_ssize_t * histo, float pop, imax = i break - return < np.uint8_t > (imax - imin) + return (imax - imin) else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_mean(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_mean(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef int i, sum, mean, n if pop: @@ -85,18 +87,18 @@ cdef inline np.uint8_t kernel_mean(Py_ssize_t * histo, float pop, n += histo[i] mean += histo[i] * i if n > 0: - return < np.uint8_t > (1.0 * mean / n) + return (1.0 * mean / n) else: - return < np.uint8_t > (0) + return (0) else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_mean_substraction(Py_ssize_t * histo, - float pop, - np.uint8_t g, - float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_mean_substraction(Py_ssize_t * histo, + float pop, + dtype_t g, + float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef int i, sum, mean, n if pop: @@ -109,17 +111,17 @@ cdef inline np.uint8_t kernel_mean_substraction(Py_ssize_t * histo, n += histo[i] mean += histo[i] * i if n > 0: - return < np.uint8_t > ((g - (mean / n)) * .5 + 127) + return ((g - (mean / n)) * .5 + 127) else: - return < np.uint8_t > (0) + return (0) else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_morph_contr_enh(Py_ssize_t * histo, - float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_morph_contr_enh(Py_ssize_t * histo, + float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef int i, imin, imax, sum, delta if pop: @@ -137,20 +139,20 @@ cdef inline np.uint8_t kernel_morph_contr_enh(Py_ssize_t * histo, imax = i break if g > imax: - return < np.uint8_t > imax + return imax if g < imin: - return < np.uint8_t > imin + return imin if imax - g < g - imin: - return < np.uint8_t > imax + return imax else: - return < np.uint8_t > imin + return imin else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_percentile(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_percentile(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef int i cdef float sum = 0. @@ -160,14 +162,14 @@ cdef inline np.uint8_t kernel_percentile(Py_ssize_t * histo, float pop, if sum >= p0 * pop: break - return < np.uint8_t > (i) + return (i) else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_pop(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_pop(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef int i, sum, n if pop: @@ -177,14 +179,14 @@ cdef inline np.uint8_t kernel_pop(Py_ssize_t * histo, float pop, sum += histo[i] if (sum >= p0 * pop) and (sum <= p1 * pop): n += histo[i] - return < np.uint8_t > (n) + return (n) else: - return < np.uint8_t > (0) + return (0) -cdef inline np.uint8_t kernel_threshold(Py_ssize_t * histo, float pop, - np.uint8_t g, float p0, float p1, - Py_ssize_t s0, Py_ssize_t s1): +cdef inline dtype_t kernel_threshold(Py_ssize_t * histo, float pop, + dtype_t g, float p0, float p1, + Py_ssize_t s0, Py_ssize_t s1): cdef int i cdef float sum = 0. @@ -194,9 +196,9 @@ cdef inline np.uint8_t kernel_threshold(Py_ssize_t * histo, float pop, if sum >= p0 * pop: break - return < np.uint8_t > (255 * (g >= i)) + return (255 * (g >= i)) else: - return < np.uint8_t > (0) + return (0) # ----------------------------------------------------------------- @@ -204,10 +206,10 @@ cdef inline np.uint8_t kernel_threshold(Py_ssize_t * histo, float pop, # ----------------------------------------------------------------- -def autolevel(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def autolevel(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, float p0=0., float p1=0.): """autolevel """ @@ -215,10 +217,10 @@ def autolevel(np.ndarray[np.uint8_t, ndim=2] image, 0, 0) -def gradient(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def gradient(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, float p0=0., float p1=0.): """return p0,p1 percentile gradient """ @@ -226,10 +228,10 @@ def gradient(np.ndarray[np.uint8_t, ndim=2] image, 0, 0) -def mean(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def mean(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, float p0=0., float p1=0.): """return mean between [p0 and p1] percentiles """ @@ -237,10 +239,10 @@ def mean(np.ndarray[np.uint8_t, ndim=2] image, 0, 0) -def mean_substraction(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def mean_substraction(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, float p0=0., float p1=0.): """return original - mean between [p0 and p1] percentiles *.5 +127 """ @@ -248,10 +250,10 @@ def mean_substraction(np.ndarray[np.uint8_t, ndim=2] image, p0, p1, 0, 0) -def morph_contr_enh(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def morph_contr_enh(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, float p0=0., float p1=0.): """reforce contrast using percentiles """ @@ -259,10 +261,10 @@ def morph_contr_enh(np.ndarray[np.uint8_t, ndim=2] image, p0, p1, 0, 0) -def percentile(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def percentile(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, float p0=0., float p1=0.): """return p0 percentile """ @@ -270,10 +272,10 @@ def percentile(np.ndarray[np.uint8_t, ndim=2] image, p0, p1, 0, 0) -def pop(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def pop(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, float p0=0., float p1=0.): """return nb of pixels between [p0 and p1] """ @@ -281,10 +283,10 @@ def pop(np.ndarray[np.uint8_t, ndim=2] image, 0, 0) -def threshold(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, +def threshold(cnp.ndarray[dtype_t, ndim=2] image, + cnp.ndarray[dtype_t, ndim=2] selem, + cnp.ndarray[dtype_t, ndim=2] mask=None, + cnp.ndarray[dtype_t, ndim=2] out=None, char shift_x=0, char shift_y=0, float p0=0., float p1=0.): """return 255 if g > percentile p0 """ diff --git a/skimage/graph/_mcp.pxd b/skimage/graph/_mcp.pxd index 4222d877..b2e2a548 100644 --- a/skimage/graph/_mcp.pxd +++ b/skimage/graph/_mcp.pxd @@ -4,11 +4,11 @@ other cython modules can "cimport mcp" and subclass it. """ cimport heap -cimport numpy as np +cimport numpy as cnp ctypedef heap.BOOL_T BOOL_T -ctypedef unsigned char DIM_T -ctypedef np.float64_t FLOAT_T +ctypedef unsigned char DIM_T +ctypedef cnp.float64_t FLOAT_T cdef class MCP: cdef heap.FastUpdateBinaryHeap costs_heap @@ -23,7 +23,7 @@ cdef class MCP: cdef object flat_offsets cdef object offset_lengths cdef BOOL_T dirty - cdef BOOL_T use_start_cost + cdef BOOL_T use_start_cost # if use_start_cost is true, the cost of the starting element is added to # the cost of the path. Set to true by default in the base class... diff --git a/skimage/graph/_mcp.pyx b/skimage/graph/_mcp.pyx index 320493c2..4e5f6d52 100644 --- a/skimage/graph/_mcp.pyx +++ b/skimage/graph/_mcp.pyx @@ -1,5 +1,7 @@ -# -*- python -*- - +#cython: cdivision=True +#cython: boundscheck=False +#cython: nonecheck=False +#cython: wraparound=False """Cython implementation of Dijkstra's minimum cost path algorithm, for use with data on a n-dimensional lattice. @@ -32,19 +34,19 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -import cython -cimport numpy as np import numpy as np -cimport heap import heap -ctypedef np.int8_t OFFSET_T +cimport numpy as cnp +cimport heap + +ctypedef cnp.int8_t OFFSET_T OFFSET_D = np.int8 -ctypedef np.int16_t OFFSETS_INDEX_T +ctypedef cnp.int16_t OFFSETS_INDEX_T OFFSETS_INDEX_D = np.int16 -ctypedef np.int8_t EDGE_T +ctypedef cnp.int8_t EDGE_T EDGE_D = np.int8 -ctypedef np.intp_t INDEX_T +ctypedef cnp.intp_t INDEX_T INDEX_D = np.intp FLOAT_D = np.float64 @@ -317,7 +319,6 @@ cdef class MCP: FLOAT_T new_cost, FLOAT_T offset_length): return new_cost - @cython.boundscheck(False) def find_costs(self, starts, ends=None, find_all_ends=True): """ Find the minimum-cost path from the given starting points. @@ -366,7 +367,7 @@ cdef class MCP: cdef BOOL_T use_ends = 0 cdef INDEX_T num_ends cdef BOOL_T all_ends = find_all_ends - cdef np.ndarray[INDEX_T, ndim=1] flat_ends + cdef cnp.ndarray[INDEX_T, ndim=1] flat_ends starts = _normalize_indices(starts, self.costs_shape) if starts is None: raise ValueError('start points must all be within the costs array') @@ -385,18 +386,18 @@ cdef class MCP: # lookup and array-ify object attributes for fast use cdef heap.FastUpdateBinaryHeap costs_heap = self.costs_heap - cdef np.ndarray[FLOAT_T, ndim=1] flat_costs = self.flat_costs - cdef np.ndarray[FLOAT_T, ndim=1] flat_cumulative_costs = \ + cdef cnp.ndarray[FLOAT_T, ndim=1] flat_costs = self.flat_costs + cdef cnp.ndarray[FLOAT_T, ndim=1] flat_cumulative_costs = \ self.flat_cumulative_costs - cdef np.ndarray[OFFSETS_INDEX_T, ndim=1] traceback_offsets = \ + cdef cnp.ndarray[OFFSETS_INDEX_T, ndim=1] traceback_offsets = \ self.traceback_offsets - cdef np.ndarray[EDGE_T, ndim=2] flat_pos_edge_map = \ + cdef cnp.ndarray[EDGE_T, ndim=2] flat_pos_edge_map = \ self.flat_pos_edge_map - cdef np.ndarray[EDGE_T, ndim=2] flat_neg_edge_map = \ + cdef cnp.ndarray[EDGE_T, ndim=2] flat_neg_edge_map = \ self.flat_neg_edge_map - cdef np.ndarray[OFFSET_T, ndim=2] offsets = self.offsets - cdef np.ndarray[INDEX_T, ndim=1] flat_offsets = self.flat_offsets - cdef np.ndarray[FLOAT_T, ndim=1] offset_lengths = self.offset_lengths + cdef cnp.ndarray[OFFSET_T, ndim=2] offsets = self.offsets + cdef cnp.ndarray[INDEX_T, ndim=1] flat_offsets = self.flat_offsets + cdef cnp.ndarray[FLOAT_T, ndim=1] offset_lengths = self.offset_lengths cdef DIM_T dim = self.dim cdef int num_offsets = len(flat_offsets) @@ -514,7 +515,6 @@ cdef class MCP: self.dirty = 1 return cumulative_costs, traceback - @cython.boundscheck(False) def traceback(self, end): """traceback(end) @@ -555,12 +555,12 @@ cdef class MCP: raise ValueError('no minimum-cost path was found ' 'to the specified end point') - cdef np.ndarray[INDEX_T, ndim=1] position = \ + cdef cnp.ndarray[INDEX_T, ndim=1] position = \ np.array(ends[0], dtype=INDEX_D) - cdef np.ndarray[OFFSETS_INDEX_T, ndim=1] traceback_offsets = \ + cdef cnp.ndarray[OFFSETS_INDEX_T, ndim=1] traceback_offsets = \ self.traceback_offsets - cdef np.ndarray[OFFSET_T, ndim=2] offsets = self.offsets - cdef np.ndarray[INDEX_T, ndim=1] flat_offsets = self.flat_offsets + cdef cnp.ndarray[OFFSET_T, ndim=2] offsets = self.offsets + cdef cnp.ndarray[INDEX_T, ndim=1] flat_offsets = self.flat_offsets cdef OFFSETS_INDEX_T offset cdef DIM_T d diff --git a/skimage/io/_plugins/_colormixer.pyx b/skimage/io/_plugins/_colormixer.pyx index ace45c94..4fbe2f18 100644 --- a/skimage/io/_plugins/_colormixer.pyx +++ b/skimage/io/_plugins/_colormixer.pyx @@ -1,4 +1,7 @@ -# -*- python -*- +#cython: cdivision=True +#cython: boundscheck=False +#cython: nonecheck=False +#cython: wraparound=False """Colour Mixer @@ -9,15 +12,14 @@ one. """ import cython -import numpy as np -cimport numpy as np + +cimport numpy as cnp from libc.math cimport exp, pow -@cython.boundscheck(False) -def add(np.ndarray[np.uint8_t, ndim=3] img, - np.ndarray[np.uint8_t, ndim=3] stateimg, - int channel, int amount): +def add(cnp.ndarray[cnp.uint8_t, ndim=3] img, + cnp.ndarray[cnp.uint8_t, ndim=3] stateimg, + Py_ssize_t channel, Py_ssize_t amount): """Add a given amount to a colour channel of `stateimg`, and store the result in `img`. Overflow is clipped. @@ -26,45 +28,44 @@ def add(np.ndarray[np.uint8_t, ndim=3] img, img : (M, N, 3) ndarray of uint8 Output image. stateimg : (M, N, 3) ndarray of uint8 - Input image. + Icnput image. channel : int Channel (0 for "red", 1 for "green", 2 for "blue"). amount : int Value to add. """ - cdef int height = img.shape[0] - cdef int width = img.shape[1] - cdef int k = channel - cdef int n = amount + cdef Py_ssize_t height = img.shape[0] + cdef Py_ssize_t width = img.shape[1] + cdef Py_ssize_t k = channel + cdef Py_ssize_t n = amount - cdef np.int16_t op_result + cdef cnp.int16_t op_result - cdef np.uint8_t lut[256] + cdef cnp.uint8_t lut[256] - cdef int i, j, l + cdef Py_ssize_t i, j, l with nogil: for l from 0 <= l < 256: - op_result = (l + n) + op_result = (l + n) if op_result > 255: op_result = 255 elif op_result < 0: op_result = 0 else: pass - lut[l] = op_result + lut[l] = op_result for i from 0 <= i < height: for j from 0 <= j < width: img[i, j, k] = lut[stateimg[i,j,k]] -@cython.boundscheck(False) -def multiply(np.ndarray[np.uint8_t, ndim=3] img, - np.ndarray[np.uint8_t, ndim=3] stateimg, - int channel, float amount): +def multiply(cnp.ndarray[cnp.uint8_t, ndim=3] img, + cnp.ndarray[cnp.uint8_t, ndim=3] stateimg, + Py_ssize_t channel, float amount): """Multiply a colour channel of `stateimg` by a certain amount, and store the result in `img`. Overflow is clipped. @@ -73,23 +74,23 @@ def multiply(np.ndarray[np.uint8_t, ndim=3] img, img : (M, N, 3) ndarray of uint8 Output image. stateimg : (M, N, 3) ndarray of uint8 - Input image. + Icnput image. channel : int Channel (0 for "red", 1 for "green", 2 for "blue"). amount : float Multiplication factor. """ - cdef int height = img.shape[0] - cdef int width = img.shape[1] - cdef int k = channel + cdef Py_ssize_t height = img.shape[0] + cdef Py_ssize_t width = img.shape[1] + cdef Py_ssize_t k = channel cdef float n = amount cdef float op_result - cdef np.uint8_t lut[256] + cdef cnp.uint8_t lut[256] - cdef int i, j, l + cdef Py_ssize_t i, j, l with nogil: @@ -101,17 +102,16 @@ def multiply(np.ndarray[np.uint8_t, ndim=3] img, op_result = 0 else: pass - lut[l] = op_result + lut[l] = op_result for i from 0 <= i < height: for j from 0 <= j < width: img[i,j,k] = lut[stateimg[i,j,k]] -@cython.boundscheck(False) -def brightness(np.ndarray[np.uint8_t, ndim=3] img, - np.ndarray[np.uint8_t, ndim=3] stateimg, - float factor, int offset): +def brightness(cnp.ndarray[cnp.uint8_t, ndim=3] img, + cnp.ndarray[cnp.uint8_t, ndim=3] stateimg, + float factor, Py_ssize_t offset): """Modify the brightness of an image. 'factor' is multiplied to all channels, which are then added by 'amount'. Overflow is clipped. @@ -121,7 +121,7 @@ def brightness(np.ndarray[np.uint8_t, ndim=3] img, img : (M, N, 3) ndarray of uint8 Output image. stateimg : (M, N, 3) ndarray of uint8 - Input image. + Icnput image. factor : float Multiplication factor. offset : int @@ -129,13 +129,13 @@ def brightness(np.ndarray[np.uint8_t, ndim=3] img, """ - cdef int height = img.shape[0] - cdef int width = img.shape[1] + cdef Py_ssize_t height = img.shape[0] + cdef Py_ssize_t width = img.shape[1] cdef float op_result - cdef np.uint8_t lut[256] + cdef cnp.uint8_t lut[256] - cdef int i, j, k + cdef Py_ssize_t i, j, k with nogil: for k from 0 <= k < 256: @@ -146,7 +146,7 @@ def brightness(np.ndarray[np.uint8_t, ndim=3] img, op_result = 0 else: pass - lut[k] = op_result + lut[k] = op_result for i from 0 <= i < height: for j from 0 <= j < width: @@ -155,27 +155,25 @@ def brightness(np.ndarray[np.uint8_t, ndim=3] img, img[i,j,2] = lut[stateimg[i,j,2]] -@cython.boundscheck(False) -@cython.cdivision(True) -def sigmoid_gamma(np.ndarray[np.uint8_t, ndim=3] img, - np.ndarray[np.uint8_t, ndim=3] stateimg, +def sigmoid_gamma(cnp.ndarray[cnp.uint8_t, ndim=3] img, + cnp.ndarray[cnp.uint8_t, ndim=3] stateimg, float alpha, float beta): - cdef int height = img.shape[0] - cdef int width = img.shape[1] + cdef Py_ssize_t height = img.shape[0] + cdef Py_ssize_t width = img.shape[1] - cdef int i, j, k + cdef Py_ssize_t i, j, k cdef float c1 = 1 / (1 + exp(beta)) cdef float c2 = 1 / (1 + exp(beta - alpha)) - c1 - cdef np.uint8_t lut[256] + cdef cnp.uint8_t lut[256] with nogil: # compute the lut for k from 0 <= k < 256: - lut[k] = (((1 / (1 + exp(beta - (k / 255.) * alpha))) + lut[k] = (((1 / (1 + exp(beta - (k / 255.) * alpha))) - c1) * 255 / c2) for i from 0 <= i < height: for j from 0 <= j < width: @@ -184,17 +182,16 @@ def sigmoid_gamma(np.ndarray[np.uint8_t, ndim=3] img, img[i,j,2] = lut[stateimg[i,j,2]] -@cython.boundscheck(False) -def gamma(np.ndarray[np.uint8_t, ndim=3] img, - np.ndarray[np.uint8_t, ndim=3] stateimg, +def gamma(cnp.ndarray[cnp.uint8_t, ndim=3] img, + cnp.ndarray[cnp.uint8_t, ndim=3] stateimg, float gamma): - cdef int height = img.shape[0] - cdef int width = img.shape[1] + cdef Py_ssize_t height = img.shape[0] + cdef Py_ssize_t width = img.shape[1] - cdef np.uint8_t lut[256] + cdef cnp.uint8_t lut[256] - cdef int i, j, k + cdef Py_ssize_t i, j, k if gamma == 0: gamma = 0.00000000000000000001 @@ -204,7 +201,7 @@ def gamma(np.ndarray[np.uint8_t, ndim=3] img, # compute the lut for k from 0 <= k < 256: - lut[k] = ((pow((k / 255.), gamma) * 255)) + lut[k] = ((pow((k / 255.), gamma) * 255)) for i from 0 <= i < height: for j from 0 <= j < width: @@ -213,7 +210,6 @@ def gamma(np.ndarray[np.uint8_t, ndim=3] img, img[i,j,2] = lut[stateimg[i,j,2]] -@cython.cdivision(True) cdef void rgb_2_hsv(float* RGB, float* HSV) nogil: cdef float R, G, B, H, S, V, MAX, MIN R = RGB[0] @@ -277,11 +273,10 @@ cdef void rgb_2_hsv(float* RGB, float* HSV) nogil: HSV[2] = V -@cython.cdivision(True) cdef void hsv_2_rgb(float* HSV, float* RGB) nogil: cdef float H, S, V cdef float f, p, q, t, r, g, b - cdef int hi + cdef Py_ssize_t hi H = HSV[0] S = HSV[1] @@ -422,9 +417,8 @@ def py_rgb_2_hsv(R, G, B): return (H, S, V) -@cython.boundscheck(False) -def hsv_add(np.ndarray[np.uint8_t, ndim=3] img, - np.ndarray[np.uint8_t, ndim=3] stateimg, +def hsv_add(cnp.ndarray[cnp.uint8_t, ndim=3] img, + cnp.ndarray[cnp.uint8_t, ndim=3] stateimg, float h_amt, float s_amt, float v_amt): """Modify the image color by specifying additive HSV Values. @@ -444,7 +438,7 @@ def hsv_add(np.ndarray[np.uint8_t, ndim=3] img, img : (M, N, 3) ndarray of uint8 Output image. stateimg : (M, N, 3) ndarray of uint8 - Input image. + Icnput image. h_amt : float Ammount to add to H channel. s_amt : float @@ -455,13 +449,13 @@ def hsv_add(np.ndarray[np.uint8_t, ndim=3] img, """ - cdef int height = img.shape[0] - cdef int width = img.shape[1] + cdef Py_ssize_t height = img.shape[0] + cdef Py_ssize_t width = img.shape[1] cdef float HSV[3] cdef float RGB[3] - cdef int i, j + cdef Py_ssize_t i, j with nogil: for i from 0 <= i < height: @@ -483,14 +477,13 @@ def hsv_add(np.ndarray[np.uint8_t, ndim=3] img, RGB[1] *= 255 RGB[2] *= 255 - img[i, j, 0] = RGB[0] - img[i, j, 1] = RGB[1] - img[i, j, 2] = RGB[2] + img[i, j, 0] = RGB[0] + img[i, j, 1] = RGB[1] + img[i, j, 2] = RGB[2] -@cython.boundscheck(False) -def hsv_multiply(np.ndarray[np.uint8_t, ndim=3] img, - np.ndarray[np.uint8_t, ndim=3] stateimg, +def hsv_multiply(cnp.ndarray[cnp.uint8_t, ndim=3] img, + cnp.ndarray[cnp.uint8_t, ndim=3] stateimg, float h_amt, float s_amt, float v_amt): """Modify the image color by specifying multiplicative HSV Values. @@ -514,7 +507,7 @@ def hsv_multiply(np.ndarray[np.uint8_t, ndim=3] img, img : (M, N, 3) ndarray of uint8 Output image. stateimg : (M, N, 3) ndarray of uint8 - Input image. + Icnput image. h_amt : float Ammount to add to H channel. s_amt : float @@ -525,13 +518,13 @@ def hsv_multiply(np.ndarray[np.uint8_t, ndim=3] img, """ - cdef int height = img.shape[0] - cdef int width = img.shape[1] + cdef Py_ssize_t height = img.shape[0] + cdef Py_ssize_t width = img.shape[1] cdef float HSV[3] cdef float RGB[3] - cdef int i, j + cdef Py_ssize_t i, j with nogil: for i from 0 <= i < height: @@ -553,6 +546,6 @@ def hsv_multiply(np.ndarray[np.uint8_t, ndim=3] img, RGB[1] *= 255 RGB[2] *= 255 - img[i, j, 0] = RGB[0] - img[i, j, 1] = RGB[1] - img[i, j, 2] = RGB[2] + img[i, j, 0] = RGB[0] + img[i, j, 1] = RGB[1] + img[i, j, 2] = RGB[2] diff --git a/skimage/io/_plugins/_histograms.pyx b/skimage/io/_plugins/_histograms.pyx index 41ec79a1..1f4344d1 100644 --- a/skimage/io/_plugins/_histograms.pyx +++ b/skimage/io/_plugins/_histograms.pyx @@ -1,7 +1,10 @@ +#cython: cdivision=True +#cython: boundscheck=False +#cython: nonecheck=False +#cython: wraparound=False import numpy as np -cimport numpy as np -import cython +cimport numpy as cnp cdef inline float tri_max(float a, float b, float c): @@ -18,8 +21,7 @@ cdef inline float tri_max(float a, float b, float c): return c -@cython.boundscheck(False) -def histograms(np.ndarray[np.uint8_t, ndim=3] img, int nbins): +def histograms(cnp.ndarray[cnp.uint8_t, ndim=3] img, int nbins): '''Calculate the channel histograms of the current image. Parameters @@ -39,10 +41,7 @@ def histograms(np.ndarray[np.uint8_t, ndim=3] img, int nbins): ''' cdef int width = img.shape[1] cdef int height = img.shape[0] - cdef np.ndarray[np.int32_t, ndim=1] r - cdef np.ndarray[np.int32_t, ndim=1] g - cdef np.ndarray[np.int32_t, ndim=1] b - cdef np.ndarray[np.int32_t, ndim=1] v + cdef cnp.ndarray[cnp.int32_t, ndim=1] r, g, b, v r = np.zeros((nbins,), dtype=np.int32) g = np.zeros((nbins,), dtype=np.int32) diff --git a/skimage/measure/_find_contours.pyx b/skimage/measure/_find_contours.pyx index 91ba318c..d05d9aa7 100644 --- a/skimage/measure/_find_contours.pyx +++ b/skimage/measure/_find_contours.pyx @@ -1,6 +1,10 @@ -# cython: cdivision=True +#cython: cdivision=True +#cython: boundscheck=False +#cython: nonecheck=False +#cython: wraparound=False import numpy as np -cimport numpy as np + +cimport numpy as cnp cdef inline double _get_fraction(double from_value, double to_value, @@ -10,7 +14,7 @@ cdef inline double _get_fraction(double from_value, double to_value, return ((level - from_value) / (to_value - from_value)) -def iterate_and_store(np.ndarray[double, ndim=2] array, +def iterate_and_store(cnp.ndarray[double, ndim=2] array, double level, Py_ssize_t vertex_connect_high): """Iterate across the given array in a marching-squares fashion, looking for segments that cross 'level'. If such a segment is @@ -41,7 +45,8 @@ def iterate_and_store(np.ndarray[double, ndim=2] array, coords[1] = 0 # Calculate the number of iterations we'll need - cdef Py_ssize_t num_square_steps = (array.shape[0] - 1) * (array.shape[1] - 1) + cdef Py_ssize_t num_square_steps = (array.shape[0] - 1) \ + * (array.shape[1] - 1) cdef unsigned char square_case = 0 cdef tuple top, bottom, left, right diff --git a/skimage/measure/_moments.pyx b/skimage/measure/_moments.pyx index b94a7b89..145f6052 100644 --- a/skimage/measure/_moments.pyx +++ b/skimage/measure/_moments.pyx @@ -1,14 +1,16 @@ -#cython: boundscheck=False -#cython: wraparound=False #cython: cdivision=True +#cython: boundscheck=False +#cython: nonecheck=False +#cython: wraparound=False import numpy as np -cimport numpy as np + +cimport numpy as cnp -def central_moments(np.ndarray[np.double_t, ndim=2] array, double cr, double cc, - int order): +def central_moments(cnp.ndarray[cnp.double_t, ndim=2] array, double cr, + double cc, int order): cdef Py_ssize_t p, q, r, c - cdef np.ndarray[np.double_t, ndim=2] mu + cdef cnp.ndarray[cnp.double_t, ndim=2] mu mu = np.zeros((order + 1, order + 1), 'double') for p in range(order + 1): for q in range(order + 1): @@ -17,9 +19,10 @@ def central_moments(np.ndarray[np.double_t, ndim=2] array, double cr, double cc, mu[p,q] += array[r,c] * (r - cr) ** q * (c - cc) ** p return mu -def normalized_moments(np.ndarray[np.double_t, ndim=2] mu, int order): + +def normalized_moments(cnp.ndarray[cnp.double_t, ndim=2] mu, int order): cdef Py_ssize_t p, q - cdef np.ndarray[np.double_t, ndim=2] nu + cdef cnp.ndarray[cnp.double_t, ndim=2] nu nu = np.zeros((order + 1, order + 1), 'double') for p in range(order + 1): for q in range(order + 1): @@ -29,8 +32,9 @@ def normalized_moments(np.ndarray[np.double_t, ndim=2] mu, int order): nu[p,q] = np.nan return nu -def hu_moments(np.ndarray[np.double_t, ndim=2] nu): - cdef np.ndarray[np.double_t, ndim=1] hu = np.zeros((7,), 'double') + +def hu_moments(cnp.ndarray[cnp.double_t, ndim=2] nu): + cdef cnp.ndarray[cnp.double_t, ndim=1] hu = np.zeros((7,), 'double') cdef double t0 = nu[3,0] + nu[1,2] cdef double t1 = nu[2,1] + nu[0,3] cdef double q0 = t0 * t0 diff --git a/skimage/morphology/_convex_hull.pyx b/skimage/morphology/_convex_hull.pyx index 9bec825b..e4b6470c 100644 --- a/skimage/morphology/_convex_hull.pyx +++ b/skimage/morphology/_convex_hull.pyx @@ -1,9 +1,13 @@ -# -*- python -*- - -cimport numpy as np +#cython: cdivision=True +#cython: boundscheck=False +#cython: nonecheck=False +#cython: wraparound=False import numpy as np -def possible_hull(np.ndarray[dtype=np.uint8_t, ndim=2, mode="c"] img): +cimport numpy as cnp + + +def possible_hull(cnp.ndarray[dtype=cnp.uint8_t, ndim=2, mode="c"] img): """Return positions of pixels that possibly belong to the convex hull. Parameters @@ -26,7 +30,7 @@ def possible_hull(np.ndarray[dtype=np.uint8_t, ndim=2, mode="c"] img): # cols storage slots for top boundary pixels # rows storage slots for right boundary pixels # cols storage slots for bottom boundary pixels - cdef np.ndarray[dtype=np.intp_t, ndim=2] nonzero = \ + cdef cnp.ndarray[dtype=cnp.intp_t, ndim=2] nonzero = \ np.ones((2 * (rows + cols), 2), dtype=np.int) nonzero *= -1 diff --git a/skimage/morphology/_pnpoly.pyx b/skimage/morphology/_pnpoly.pyx index f6cf386f..f32778cc 100644 --- a/skimage/morphology/_pnpoly.pyx +++ b/skimage/morphology/_pnpoly.pyx @@ -1,7 +1,10 @@ -# -*- python -*- - -cimport numpy as np +#cython: cdivision=True +#cython: boundscheck=False +#cython: nonecheck=False +#cython: wraparound=False import numpy as np + +cimport numpy as cnp from skimage._shared.geometry cimport point_in_polygon, points_in_polygon @@ -26,7 +29,7 @@ def grid_points_inside_poly(shape, verts): True where the grid falls inside the polygon. """ - cdef np.ndarray[np.double_t, ndim=1, mode="c"] vx, vy + cdef cnp.ndarray[cnp.double_t, ndim=1, mode="c"] vx, vy verts = np.asarray(verts) vx = verts[:, 0].astype(np.double) @@ -37,7 +40,7 @@ def grid_points_inside_poly(shape, verts): cdef Py_ssize_t N = shape[1] cdef Py_ssize_t m, n - cdef np.ndarray[dtype=np.uint8_t, ndim=2, mode="c"] out = \ + cdef cnp.ndarray[dtype=cnp.uint8_t, ndim=2, mode="c"] out = \ np.zeros((M, N), dtype=np.uint8) for m in range(M): @@ -65,7 +68,7 @@ def points_inside_poly(points, verts): True if corresponding point is inside the polygon. """ - cdef np.ndarray[np.double_t, ndim=1, mode="c"] x, y, vx, vy + cdef cnp.ndarray[cnp.double_t, ndim=1, mode="c"] x, y, vx, vy points = np.asarray(points) verts = np.asarray(verts) @@ -76,12 +79,12 @@ def points_inside_poly(points, verts): vx = verts[:, 0].astype(np.double) vy = verts[:, 1].astype(np.double) - cdef np.ndarray[np.uint8_t, ndim=1] out = \ + cdef cnp.ndarray[cnp.uint8_t, ndim=1] out = \ np.zeros(x.shape[0], dtype=np.uint8) points_in_polygon(vx.shape[0], vx.data, vy.data, - x.shape[0], x.data, y.data, - out.data) + x.shape[0], x.data, y.data, + out.data) return out.astype(bool) diff --git a/skimage/morphology/_skeletonize_cy.pyx b/skimage/morphology/_skeletonize_cy.pyx index 9bef86b9..13e303d4 100644 --- a/skimage/morphology/_skeletonize_cy.pyx +++ b/skimage/morphology/_skeletonize_cy.pyx @@ -1,3 +1,8 @@ +#cython: cdivision=True +#cython: boundscheck=False +#cython: nonecheck=False +#cython: wraparound=False + ''' Originally part of CellProfiler, code licensed under both GPL and BSD licenses. Website: http://www.cellprofiler.org @@ -10,21 +15,20 @@ Original author: Lee Kamentsky ''' import numpy as np -cimport numpy as np -cimport cython + +cimport numpy as cnp -@cython.boundscheck(False) -def _skeletonize_loop(np.ndarray[dtype=np.uint8_t, ndim=2, - negative_indices=False, mode='c'] result, - np.ndarray[dtype=np.intp_t, ndim=1, - negative_indices=False, mode='c'] i, - np.ndarray[dtype=np.intp_t, ndim=1, - negative_indices=False, mode='c'] j, - np.ndarray[dtype=np.int32_t, ndim=1, - negative_indices=False, mode='c'] order, - np.ndarray[dtype=np.uint8_t, ndim=1, - negative_indices=False, mode='c'] table): +def _skeletonize_loop(cnp.ndarray[dtype=cnp.uint8_t, ndim=2, + negative_indices=False, mode='c'] result, + cnp.ndarray[dtype=cnp.intp_t, ndim=1, + negative_indices=False, mode='c'] i, + cnp.ndarray[dtype=cnp.intp_t, ndim=1, + negative_indices=False, mode='c'] j, + cnp.ndarray[dtype=cnp.int32_t, ndim=1, + negative_indices=False, mode='c'] order, + cnp.ndarray[dtype=cnp.uint8_t, ndim=1, + negative_indices=False, mode='c'] table): """ Inner loop of skeletonize function @@ -61,7 +65,7 @@ def _skeletonize_loop(np.ndarray[dtype=np.uint8_t, ndim=2, pixels. """ cdef: - np.int32_t accumulator + cnp.int32_t accumulator Py_ssize_t index, order_index Py_ssize_t ii, jj @@ -92,9 +96,10 @@ def _skeletonize_loop(np.ndarray[dtype=np.uint8_t, ndim=2, # Assign the value of table corresponding to the configuration result[ii, jj] = table[accumulator] -@cython.boundscheck(False) -def _table_lookup_index(np.ndarray[dtype=np.uint8_t, ndim=2, - negative_indices=False, mode='c'] image): + + +def _table_lookup_index(cnp.ndarray[dtype=cnp.uint8_t, ndim=2, + negative_indices=False, mode='c'] image): """ Return an index into a table per pixel of a binary image @@ -115,10 +120,10 @@ def _table_lookup_index(np.ndarray[dtype=np.uint8_t, ndim=2, hardwired kernel. """ cdef: - np.ndarray[dtype=np.int32_t, ndim=2, - negative_indices=False, mode='c'] indexer - np.int32_t *p_indexer - np.uint8_t *p_image + cnp.ndarray[dtype=cnp.int32_t, ndim=2, + negative_indices=False, mode='c'] indexer + cnp.int32_t *p_indexer + cnp.uint8_t *p_image Py_ssize_t i_stride Py_ssize_t i_shape Py_ssize_t j_shape @@ -129,8 +134,8 @@ def _table_lookup_index(np.ndarray[dtype=np.uint8_t, ndim=2, i_shape = image.shape[0] j_shape = image.shape[1] indexer = np.zeros((i_shape, j_shape), np.int32) - p_indexer = indexer.data - p_image = image.data + p_indexer = indexer.data + p_image = image.data i_stride = image.strides[0] assert i_shape >= 3 and j_shape >= 3, \ "Please use the slow method for arrays < 3x3" diff --git a/skimage/morphology/ccomp.pxd b/skimage/morphology/ccomp.pxd index b50703e9..569921fa 100644 --- a/skimage/morphology/ccomp.pxd +++ b/skimage/morphology/ccomp.pxd @@ -1,8 +1,8 @@ """Export fast union find in Cython""" -cimport numpy as np +cimport numpy as cnp -DTYPE = np.intp -ctypedef np.intp_t DTYPE_t +DTYPE = cnp.intp +ctypedef cnp.intp_t DTYPE_t cdef DTYPE_t find_root(DTYPE_t *forest, DTYPE_t n) cdef set_root(DTYPE_t *forest, DTYPE_t n, DTYPE_t root) diff --git a/skimage/morphology/ccomp.pyx b/skimage/morphology/ccomp.pyx index dc85196b..bbf6ed44 100644 --- a/skimage/morphology/ccomp.pyx +++ b/skimage/morphology/ccomp.pyx @@ -1,8 +1,11 @@ -# -*- python -*- #cython: cdivision=True +#cython: boundscheck=False +#cython: nonecheck=False +#cython: wraparound=False import numpy as np -cimport numpy as np + +cimport numpy as cnp """ See also: @@ -140,9 +143,9 @@ def label(input, DTYPE_t neighbors=8, DTYPE_t background=-1): cdef DTYPE_t rows = input.shape[0] cdef DTYPE_t cols = input.shape[1] - cdef np.ndarray[DTYPE_t, ndim=2] data = np.array(input, copy=True, - dtype=DTYPE) - cdef np.ndarray[DTYPE_t, ndim=2] forest + cdef cnp.ndarray[DTYPE_t, ndim=2] data = np.array(input, copy=True, + dtype=DTYPE) + cdef cnp.ndarray[DTYPE_t, ndim=2] forest forest = np.arange(data.size, dtype=DTYPE).reshape((rows, cols)) diff --git a/skimage/morphology/heap_watershed.pxi b/skimage/morphology/heap_watershed.pxi index 573d32bb..07b29f5c 100644 --- a/skimage/morphology/heap_watershed.pxi +++ b/skimage/morphology/heap_watershed.pxi @@ -9,14 +9,12 @@ All rights reserved. Original author: Lee Kamentsky """ -import numpy as np -cimport numpy as np -cimport cython +cimport numpy as cnp cdef struct Heapitem: - np.int32_t value - np.int32_t age + cnp.int32_t value + cnp.int32_t age Py_ssize_t index diff --git a/skimage/segmentation/_felzenszwalb_cy.pyx b/skimage/segmentation/_felzenszwalb_cy.pyx index 5a8ac012..f285e3db 100644 --- a/skimage/segmentation/_felzenszwalb_cy.pyx +++ b/skimage/segmentation/_felzenszwalb_cy.pyx @@ -1,15 +1,17 @@ +#cython: cdivision=True +#cython: boundscheck=False +#cython: nonecheck=False +#cython: wraparound=False import numpy as np -cimport numpy as np import scipy -cimport cython +cimport cython +cimport numpy as cnp from skimage.morphology.ccomp cimport find_root, join_trees from ..util import img_as_float -@cython.boundscheck(False) -@cython.wraparound(False) -@cython.cdivision(True) + def _felzenszwalb_grey(image, double scale=1, sigma=0.8, Py_ssize_t min_size=20): """Felzenszwalb's efficient graph based segmentation for a single channel. @@ -49,31 +51,31 @@ def _felzenszwalb_grey(image, double scale=1, sigma=0.8, Py_ssize_t min_size=20) down_cost = np.abs((image[:, 1:] - image[:, :-1])) dright_cost = np.abs((image[1:, 1:] - image[:-1, :-1])) uright_cost = np.abs((image[1:, :-1] - image[:-1, 1:])) - cdef np.ndarray[np.float_t, ndim=1] costs = np.hstack([right_cost.ravel(), + cdef cnp.ndarray[cnp.float_t, ndim=1] costs = np.hstack([right_cost.ravel(), down_cost.ravel(), dright_cost.ravel(), uright_cost.ravel()]).astype(np.float) # compute edges between pixels: height, width = image.shape[:2] - cdef np.ndarray[np.intp_t, ndim=2] segments \ + cdef cnp.ndarray[cnp.intp_t, ndim=2] segments \ = np.arange(width * height, dtype=np.intp).reshape(height, width) right_edges = np.c_[segments[1:, :].ravel(), segments[:-1, :].ravel()] down_edges = np.c_[segments[:, 1:].ravel(), segments[:, :-1].ravel()] dright_edges = np.c_[segments[1:, 1:].ravel(), segments[:-1, :-1].ravel()] uright_edges = np.c_[segments[:-1, 1:].ravel(), segments[1:, :-1].ravel()] - cdef np.ndarray[np.intp_t, ndim=2] edges \ + cdef cnp.ndarray[cnp.intp_t, ndim=2] edges \ = np.vstack([right_edges, down_edges, dright_edges, uright_edges]) # initialize data structures for segment size # and inner cost, then start greedy iteration over edges. edge_queue = np.argsort(costs) edges = np.ascontiguousarray(edges[edge_queue]) costs = np.ascontiguousarray(costs[edge_queue]) - cdef np.intp_t *segments_p = segments.data - cdef np.intp_t *edges_p = edges.data - cdef np.float_t *costs_p = costs.data - cdef np.ndarray[np.intp_t, ndim=1] segment_size \ + cdef cnp.intp_t *segments_p = segments.data + cdef cnp.intp_t *edges_p = edges.data + cdef cnp.float_t *costs_p = costs.data + cdef cnp.ndarray[cnp.intp_t, ndim=1] segment_size \ = np.ones(width * height, dtype=np.int) # inner cost of segments - cdef np.ndarray[np.float_t, ndim=1] cint = np.zeros(width * height) + cdef cnp.ndarray[cnp.float_t, ndim=1] cint = np.zeros(width * height) cdef int seg0, seg1, seg_new, e cdef float cost, inner_cost0, inner_cost1 # set costs_p back one. we increase it before we use it @@ -96,7 +98,7 @@ def _felzenszwalb_grey(image, double scale=1, sigma=0.8, Py_ssize_t min_size=20) cint[seg_new] = costs_p[0] # postprocessing to remove small segments - edges_p = edges.data + edges_p = edges.data for e in range(costs.size): seg0 = find_root(segments_p, edges_p[0]) seg1 = find_root(segments_p, edges_p[1]) diff --git a/skimage/segmentation/_quickshift.pyx b/skimage/segmentation/_quickshift.pyx index 61e4d0e0..cc649e8f 100644 --- a/skimage/segmentation/_quickshift.pyx +++ b/skimage/segmentation/_quickshift.pyx @@ -1,18 +1,18 @@ +#cython: cdivision=True +#cython: boundscheck=False +#cython: nonecheck=False +#cython: wraparound=False import numpy as np -cimport numpy as np -cimport cython -from libc.math cimport exp, sqrt - -from itertools import product from scipy import ndimage +from itertools import product + +cimport numpy as cnp +from libc.math cimport exp, sqrt from ..util import img_as_float from ..color import rgb2lab -@cython.boundscheck(False) -@cython.wraparound(False) -@cython.cdivision(True) def quickshift(image, ratio=1., float kernel_size=5, max_dist=10, return_tree=False, sigma=0, convert2lab=True, random_seed=None): """Segments image using quickshift clustering in Color-(x,y) space. @@ -69,7 +69,7 @@ def quickshift(image, ratio=1., float kernel_size=5, max_dist=10, image = rgb2lab(image) image = ndimage.gaussian_filter(img_as_float(image), [sigma, sigma, 0]) - cdef np.ndarray[dtype=np.float_t, ndim=3, mode="c"] image_c \ + cdef cnp.ndarray[dtype=cnp.float_t, ndim=3, mode="c"] image_c \ = np.ascontiguousarray(image) * ratio random_state = np.random.RandomState(random_seed) @@ -92,10 +92,10 @@ def quickshift(image, ratio=1., float kernel_size=5, max_dist=10, cdef Py_ssize_t r, c, r_, c_, channel, r_min, c_min - cdef np.float_t* image_p = image_c.data - cdef np.float_t* current_pixel_p = image_p + cdef cnp.float_t* image_p = image_c.data + cdef cnp.float_t* current_pixel_p = image_p - cdef np.ndarray[dtype=np.float_t, ndim=2] densities \ + cdef cnp.ndarray[dtype=cnp.float_t, ndim=2] densities \ = np.zeros((height, width)) # compute densities @@ -117,9 +117,9 @@ def quickshift(image, ratio=1., float kernel_size=5, max_dist=10, densities += random_state.normal(scale=0.00001, size=(height, width)) # default parent to self: - cdef np.ndarray[dtype=np.int_t, ndim=2] parent \ + cdef cnp.ndarray[dtype=cnp.int_t, ndim=2] parent \ = np.arange(width * height).reshape(height, width) - cdef np.ndarray[dtype=np.float_t, ndim=2] dist_parent \ + cdef cnp.ndarray[dtype=cnp.float_t, ndim=2] dist_parent \ = np.zeros((height, width)) # find nearest node with higher density diff --git a/skimage/segmentation/_slic.pyx b/skimage/segmentation/_slic.pyx index e91ed21d..7db072c0 100644 --- a/skimage/segmentation/_slic.pyx +++ b/skimage/segmentation/_slic.pyx @@ -1,8 +1,13 @@ +#cython: cdivision=True #cython: boundscheck=False +#cython: nonecheck=False +#cython: wraparound=False import numpy as np -cimport numpy as np from time import time from scipy import ndimage + +cimport numpy as cnp + from ..util import img_as_float from ..color import rgb2lab, gray2rgb @@ -72,32 +77,32 @@ def slic(image, n_segments=100, ratio=10., max_iter=10, sigma=1, means_x = grid_x[::step, ::step] means_color = np.zeros((means_y.shape[0], means_y.shape[1], 3)) - cdef np.ndarray[dtype=np.float_t, ndim=2] means \ + cdef cnp.ndarray[dtype=cnp.float_t, ndim=2] means \ = np.dstack([means_y, means_x, means_color]).reshape(-1, 5) - cdef np.float_t* current_mean - cdef np.float_t* mean_entry + cdef cnp.float_t* current_mean + cdef cnp.float_t* mean_entry n_means = means.shape[0] # we do the scaling of ratio in the same way as in the SLIC paper # so the values have the same meaning ratio = (ratio / float(step)) ** 2 - cdef np.ndarray[dtype=np.float_t, ndim=3] image_yx \ + cdef cnp.ndarray[dtype=cnp.float_t, ndim=3] image_yx \ = np.dstack([grid_y, grid_x, image / ratio]).copy("C") cdef Py_ssize_t i, k, x, y, x_min, x_max, y_min, y_max, changes cdef double dist_mean - cdef np.ndarray[dtype=np.intp_t, ndim=2] nearest_mean \ + cdef cnp.ndarray[dtype=cnp.intp_t, ndim=2] nearest_mean \ = np.zeros((height, width), dtype=np.intp) - cdef np.ndarray[dtype=np.float_t, ndim=2] distance \ + cdef cnp.ndarray[dtype=cnp.float_t, ndim=2] distance \ = np.empty((height, width)) - cdef np.float_t* image_p = image_yx.data - cdef np.float_t* distance_p = distance.data - cdef np.float_t* current_distance - cdef np.float_t* current_pixel + cdef cnp.float_t* image_p = image_yx.data + cdef cnp.float_t* distance_p = distance.data + cdef cnp.float_t* current_distance + cdef cnp.float_t* current_pixel cdef double tmp for i in range(max_iter): distance.fill(np.inf) changes = 0 - current_mean = means.data + current_mean = means.data # assign pixels to means for k in range(n_means): # compute windows: diff --git a/skimage/transform/_hough_transform.pyx b/skimage/transform/_hough_transform.pyx index 662b3b88..5d1f27af 100644 --- a/skimage/transform/_hough_transform.pyx +++ b/skimage/transform/_hough_transform.pyx @@ -2,9 +2,9 @@ #cython: boundscheck=False #cython: nonecheck=False #cython: wraparound=False -cimport cython import numpy as np -cimport numpy as np + +cimport numpy as cnp from libc.math cimport abs, fabs, sqrt, ceil from libc.stdlib cimport rand @@ -17,14 +17,14 @@ cdef inline Py_ssize_t round(double r): return ((r + 0.5) if (r > 0.0) else (r - 0.5)) -def _hough(np.ndarray img, np.ndarray[ndim=1, dtype=np.double_t] theta=None): +def _hough(cnp.ndarray img, cnp.ndarray[ndim=1, dtype=cnp.double_t] theta=None): if img.ndim != 2: raise ValueError('The input image must be 2D.') # Compute the array of angles and their sine and cosine - cdef np.ndarray[ndim=1, dtype=np.double_t] ctheta - cdef np.ndarray[ndim=1, dtype=np.double_t] stheta + cdef cnp.ndarray[ndim=1, dtype=cnp.double_t] ctheta + cdef cnp.ndarray[ndim=1, dtype=cnp.double_t] stheta if theta is None: theta = np.linspace(PI_2, NEG_PI_2, 180) @@ -33,8 +33,8 @@ def _hough(np.ndarray img, np.ndarray[ndim=1, dtype=np.double_t] theta=None): stheta = np.sin(theta) # compute the bins and allocate the accumulator array - cdef np.ndarray[ndim=2, dtype=np.uint64_t] accum - cdef np.ndarray[ndim=1, dtype=np.double_t] bins + cdef cnp.ndarray[ndim=2, dtype=cnp.uint64_t] accum + cdef cnp.ndarray[ndim=1, dtype=cnp.double_t] bins cdef Py_ssize_t max_distance, offset max_distance = 2 * ceil(sqrt(img.shape[0] * img.shape[0] + @@ -44,7 +44,7 @@ def _hough(np.ndarray img, np.ndarray[ndim=1, dtype=np.double_t] theta=None): offset = max_distance / 2 # compute the nonzero indexes - cdef np.ndarray[ndim=1, dtype=np.npy_intp] x_idxs, y_idxs + cdef cnp.ndarray[ndim=1, dtype=cnp.npy_intp] x_idxs, y_idxs y_idxs, x_idxs = np.nonzero(img) # finally, run the transform @@ -60,9 +60,9 @@ def _hough(np.ndarray img, np.ndarray[ndim=1, dtype=np.double_t] theta=None): return accum, theta, bins -def _probabilistic_hough(np.ndarray img, int value_threshold, +def _probabilistic_hough(cnp.ndarray img, int value_threshold, int line_length, int line_gap, - np.ndarray[ndim=1, dtype=np.double_t] theta=None): + cnp.ndarray[ndim=1, dtype=cnp.double_t] theta=None): if img.ndim != 2: raise ValueError('The input image must be 2D.') @@ -74,11 +74,11 @@ def _probabilistic_hough(np.ndarray img, int value_threshold, cdef Py_ssize_t width = img.shape[1] # compute the bins and allocate the accumulator array - cdef np.ndarray[ndim=2, dtype=np.int64_t] accum - cdef np.ndarray[ndim=1, dtype=np.double_t] ctheta, stheta - cdef np.ndarray[ndim=2, dtype=np.uint8_t] mask = \ + cdef cnp.ndarray[ndim=2, dtype=cnp.int64_t] accum + cdef cnp.ndarray[ndim=1, dtype=cnp.double_t] ctheta, stheta + cdef cnp.ndarray[ndim=2, dtype=cnp.uint8_t] mask = \ np.zeros((height, width), dtype=np.uint8) - cdef np.ndarray[ndim=2, dtype=np.int32_t] line_end = \ + cdef cnp.ndarray[ndim=2, dtype=cnp.int32_t] line_end = \ np.zeros((2, 2), dtype=np.int32) cdef Py_ssize_t max_distance, offset, num_indexes, index cdef double a, b diff --git a/skimage/transform/_warps_cy.pyx b/skimage/transform/_warps_cy.pyx index 951eb861..968f643a 100644 --- a/skimage/transform/_warps_cy.pyx +++ b/skimage/transform/_warps_cy.pyx @@ -2,9 +2,9 @@ #cython: boundscheck=False #cython: nonecheck=False #cython: wraparound=False - -cimport numpy as np import numpy as np + +cimport numpy as cnp from skimage._shared.interpolation cimport (nearest_neighbour_interpolation, bilinear_interpolation, biquadratic_interpolation, @@ -35,7 +35,7 @@ cdef inline void _matrix_transform(double x, double y, double* H, double *x_, y_[0] = yy / zz -def _warp_fast(np.ndarray image, np.ndarray H, output_shape=None, int order=1, +def _warp_fast(cnp.ndarray image, cnp.ndarray H, output_shape=None, int order=1, mode='constant', double cval=0): """Projective transformation (homography). @@ -83,9 +83,9 @@ def _warp_fast(np.ndarray image, np.ndarray H, output_shape=None, int order=1, """ - cdef np.ndarray[dtype=np.double_t, ndim=2, mode="c"] img = \ + cdef cnp.ndarray[dtype=cnp.double_t, ndim=2, mode="c"] img = \ np.ascontiguousarray(image, dtype=np.double) - cdef np.ndarray[dtype=np.double_t, ndim=2, mode="c"] M = \ + cdef cnp.ndarray[dtype=cnp.double_t, ndim=2, mode="c"] M = \ np.ascontiguousarray(H) if mode not in ('constant', 'wrap', 'reflect', 'nearest'): @@ -101,7 +101,7 @@ def _warp_fast(np.ndarray image, np.ndarray H, output_shape=None, int order=1, out_r = output_shape[0] out_c = output_shape[1] - cdef np.ndarray[dtype=np.double_t, ndim=2] out = \ + cdef cnp.ndarray[dtype=cnp.double_t, ndim=2] out = \ np.zeros((out_r, out_c), dtype=np.double) cdef Py_ssize_t tfr, tfc