From b78a0460fbb43bcc942742b3a6b41f8d315977c8 Mon Sep 17 00:00:00 2001 From: Olivier Debeir Date: Mon, 15 Oct 2012 13:36:41 +0200 Subject: [PATCH] remplace int by Py_ssize_t and for rank16 --- skimage/rank/_core16.pxd | 4 +- skimage/rank/_core16.pyx | 46 +++++++++++----------- skimage/rank/_crank16.pyx | 82 +++++++++++++++++++-------------------- 3 files changed, 66 insertions(+), 66 deletions(-) diff --git a/skimage/rank/_core16.pxd b/skimage/rank/_core16.pxd index d00ef37e..0efd4e04 100644 --- a/skimage/rank/_core16.pxd +++ b/skimage/rank/_core16.pxd @@ -4,9 +4,9 @@ cimport numpy as np # 16 bit core kernel receives extra information about data bitdepth #--------------------------------------------------------------------------- -cdef inline _core16(np.uint16_t kernel(int*, float, np.uint16_t, int ,int,int ), +cdef inline _core16(np.uint16_t kernel(Py_ssize_t*, float, np.uint16_t, Py_ssize_t ,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, -char shift_x, char shift_y,int bitdepth) \ No newline at end of file +char shift_x, char shift_y,Py_ssize_t bitdepth) \ No newline at end of file diff --git a/skimage/rank/_core16.pyx b/skimage/rank/_core16.pyx index 1a3194de..00e229d9 100644 --- a/skimage/rank/_core16.pyx +++ b/skimage/rank/_core16.pyx @@ -18,24 +18,24 @@ from libc.stdlib cimport malloc, free # 16 bit core kernel receives extra information about data bitdepth #--------------------------------------------------------------------------- -cdef inline _core16(np.uint16_t kernel(int*, float, np.uint16_t, int ,int,int ), +cdef inline _core16(np.uint16_t kernel(Py_ssize_t*, float, np.uint16_t, Py_ssize_t ,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, -char shift_x, char shift_y,int bitdepth): +char shift_x, char shift_y,Py_ssize_t bitdepth): """ Main loop, this function computes the histogram for each image point - data is uint8 - result is uint8 casted """ - cdef int rows = image.shape[0] - cdef int cols = image.shape[1] - cdef int srows = selem.shape[0] - cdef int scols = selem.shape[1] + cdef Py_ssize_t rows = image.shape[0] + cdef Py_ssize_t cols = image.shape[1] + cdef Py_ssize_t srows = selem.shape[0] + cdef Py_ssize_t scols = selem.shape[1] - cdef int centre_r = int(selem.shape[0] / 2) + shift_y - cdef int centre_c = int(selem.shape[1] / 2) + shift_x + cdef Py_ssize_t centre_r = int(selem.shape[0] / 2) + shift_y + cdef Py_ssize_t centre_c = int(selem.shape[1] / 2) + shift_x # check that structuring element center is inside the element bounding box assert centre_r >= 0 @@ -49,7 +49,7 @@ char shift_x, char shift_y,int bitdepth): #set maxbin and midbin - cdef int maxbin=maxbin_list[bitdepth],midbin=midbin_list[bitdepth] + cdef Py_ssize_t maxbin=maxbin_list[bitdepth],midbin=midbin_list[bitdepth] assert (imagemask.data # define local variable types - cdef int r, c, rr, cc, s, value, local_max, i, even_row + cdef Py_ssize_t r, c, rr, cc, s, value, local_max, i, even_row cdef float pop # number of pixels actually inside the neighborhood (float) # allocate memory with malloc - cdef int max_se = srows*scols + cdef Py_ssize_t max_se = srows*scols # number of element in each attack border - cdef int num_se_n, num_se_s, num_se_e, num_se_w + cdef Py_ssize_t num_se_n, num_se_s, num_se_e, num_se_w # the current local histogram distribution - cdef int* histo = malloc(maxbin * sizeof(int)) + cdef Py_ssize_t* histo = malloc(maxbin * sizeof(Py_ssize_t)) # these lists contain the relative pixel row and column for each of the 4 attack borders # east, west, north and south # e.g. se_e_r lists the rows of the east structuring element border - cdef int* se_e_r = malloc(max_se * sizeof(int)) - cdef int* se_e_c = malloc(max_se * sizeof(int)) - cdef int* se_w_r = malloc(max_se * sizeof(int)) - cdef int* se_w_c = malloc(max_se * sizeof(int)) - cdef int* se_n_r = malloc(max_se * sizeof(int)) - cdef int* se_n_c = malloc(max_se * sizeof(int)) - cdef int* se_s_r = malloc(max_se * sizeof(int)) - cdef int* se_s_c = malloc(max_se * sizeof(int)) + cdef Py_ssize_t* se_e_r = malloc(max_se * sizeof(Py_ssize_t)) + cdef Py_ssize_t* se_e_c = malloc(max_se * sizeof(Py_ssize_t)) + cdef Py_ssize_t* se_w_r = malloc(max_se * sizeof(Py_ssize_t)) + cdef Py_ssize_t* se_w_c = malloc(max_se * sizeof(Py_ssize_t)) + cdef Py_ssize_t* se_n_r = malloc(max_se * sizeof(Py_ssize_t)) + cdef Py_ssize_t* se_n_c = malloc(max_se * sizeof(Py_ssize_t)) + cdef Py_ssize_t* se_s_r = malloc(max_se * sizeof(Py_ssize_t)) + cdef Py_ssize_t* se_s_c = malloc(max_se * sizeof(Py_ssize_t)) # build attack and release borders # by using difference along axis diff --git a/skimage/rank/_crank16.pyx b/skimage/rank/_crank16.pyx index 90a7e8bb..eb08326b 100644 --- a/skimage/rank/_crank16.pyx +++ b/skimage/rank/_crank16.pyx @@ -21,8 +21,8 @@ from _core16 cimport _core16 # kernels uint16 take extra parameter for defining the bitdepth # ----------------------------------------------------------------- -cdef inline np.uint16_t kernel_autolevel(int* histo, float pop, np.uint16_t g,int bitdepth,int maxbin, int midbin): - cdef int i,imin,imax,delta +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): + cdef Py_ssize_t i,imin,imax,delta if pop: for i in range(maxbin-1,-1,-1): @@ -39,8 +39,8 @@ cdef inline np.uint16_t kernel_autolevel(int* histo, float pop, np.uint16_t g,in else: return (imax-imin) -cdef inline np.uint16_t kernel_bottomhat(int* histo, float pop, np.uint16_t g,int bitdepth,int maxbin, int midbin): - cdef int i +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): + cdef Py_ssize_t i for i in range(maxbin): if histo[i]: @@ -49,8 +49,8 @@ cdef inline np.uint16_t kernel_bottomhat(int* histo, float pop, np.uint16_t g,in return (g-i) -cdef inline np.uint16_t kernel_equalize(int* histo, float pop, np.uint16_t g,int bitdepth,int maxbin, int midbin): - cdef int i +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): + cdef Py_ssize_t i cdef float sum = 0. if pop: @@ -63,8 +63,8 @@ cdef inline np.uint16_t kernel_equalize(int* histo, float pop, np.uint16_t g,int else: return (0) -cdef inline np.uint16_t kernel_gradient(int* histo, float pop, np.uint16_t g,int bitdepth,int maxbin, int midbin): - cdef int i,imin,imax +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): + cdef Py_ssize_t i,imin,imax if pop: for i in range(maxbin-1,-1,-1): @@ -79,8 +79,8 @@ cdef inline np.uint16_t kernel_gradient(int* histo, float pop, np.uint16_t g,int else: return (0) -cdef inline np.uint16_t kernel_maximum(int* histo, float pop, np.uint16_t g,int bitdepth,int maxbin, int midbin): - cdef int i +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): + cdef Py_ssize_t i if pop: for i in range(maxbin-1,-1,-1): @@ -89,8 +89,8 @@ cdef inline np.uint16_t kernel_maximum(int* histo, float pop, np.uint16_t g,int return (0) -cdef inline np.uint16_t kernel_mean(int* histo, float pop, np.uint16_t g,int bitdepth,int maxbin, int midbin): - cdef int i +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): + cdef Py_ssize_t i cdef float mean = 0. if pop: @@ -100,8 +100,8 @@ cdef inline np.uint16_t kernel_mean(int* histo, float pop, np.uint16_t g,int bit else: return (0) -cdef inline np.uint16_t kernel_meansubstraction(int* histo, float pop, np.uint16_t g,int bitdepth,int maxbin, int midbin): - cdef int i +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): + cdef Py_ssize_t i cdef float mean = 0. if pop: @@ -111,8 +111,8 @@ cdef inline np.uint16_t kernel_meansubstraction(int* histo, float pop, np.uint16 else: return (0) -cdef inline np.uint16_t kernel_median(int* histo, float pop, np.uint16_t g,int bitdepth,int maxbin, int midbin): - cdef int i +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): + cdef Py_ssize_t i cdef float sum = pop/2.0 if pop: @@ -124,8 +124,8 @@ cdef inline np.uint16_t kernel_median(int* histo, float pop, np.uint16_t g,int b return (0) -cdef inline np.uint16_t kernel_minimum(int* histo, float pop, np.uint16_t g,int bitdepth,int maxbin, int midbin): - cdef int i +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): + cdef Py_ssize_t i if pop: for i in range(maxbin): @@ -134,8 +134,8 @@ cdef inline np.uint16_t kernel_minimum(int* histo, float pop, np.uint16_t g,int return (0) -cdef inline np.uint16_t kernel_modal(int* histo, float pop, np.uint16_t g,int bitdepth,int maxbin, int midbin): - cdef int hmax=0,imax=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): + cdef Py_ssize_t hmax=0,imax=0 if pop: for i in range(maxbin): @@ -146,8 +146,8 @@ cdef inline np.uint16_t kernel_modal(int* histo, float pop, np.uint16_t g,int bi return (0) -cdef inline np.uint16_t kernel_morph_contr_enh(int* histo, float pop, np.uint16_t g,int bitdepth,int maxbin, int midbin): - cdef int i,imin,imax +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): + cdef Py_ssize_t i,imin,imax if pop: for i in range(maxbin-1,-1,-1): @@ -165,11 +165,11 @@ cdef inline np.uint16_t kernel_morph_contr_enh(int* histo, float pop, np.uint16_ else: return (0) -cdef inline np.uint16_t kernel_pop(int* histo, float pop, np.uint16_t g,int bitdepth,int maxbin, int midbin): +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): return (pop) -cdef inline np.uint16_t kernel_threshold(int* histo, float pop, np.uint16_t g,int bitdepth,int maxbin, int midbin): - cdef int i +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): + cdef Py_ssize_t i cdef float mean = 0. if pop: @@ -179,8 +179,8 @@ cdef inline np.uint16_t kernel_threshold(int* histo, float pop, np.uint16_t g,in else: return (0) -cdef inline np.uint16_t kernel_tophat(int* histo, float pop, np.uint16_t g,int bitdepth,int maxbin, int midbin): - cdef int i +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): + cdef Py_ssize_t i for i in range(maxbin-1,-1,-1): if histo[i]: @@ -195,7 +195,7 @@ 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, - char shift_x=0, char shift_y=0, int bitdepth=8): + char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): """bottom hat """ return _core16(kernel_autolevel,image,selem,mask,out,shift_x,shift_y,bitdepth) @@ -204,7 +204,7 @@ 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, - char shift_x=0, char shift_y=0, int bitdepth=8): + char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): """bottom hat """ return _core16(kernel_bottomhat,image,selem,mask,out,shift_x,shift_y,bitdepth) @@ -213,7 +213,7 @@ 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, - char shift_x=0, char shift_y=0, int bitdepth=8): + char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): """local egalisation of the gray level """ return _core16(kernel_equalize,image,selem,mask,out,shift_x,shift_y,bitdepth) @@ -222,7 +222,7 @@ 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, - char shift_x=0, char shift_y=0, int bitdepth=8): + char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): """local maximum - local minimum gray level """ return _core16(kernel_gradient,image,selem,mask,out,shift_x,shift_y,bitdepth) @@ -231,7 +231,7 @@ 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, - char shift_x=0, char shift_y=0, int bitdepth=8): + char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): """local maximum gray level """ return _core16(kernel_maximum,image,selem,mask,out,shift_x,shift_y,bitdepth) @@ -240,7 +240,7 @@ 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, - char shift_x=0, char shift_y=0, int bitdepth=8): + char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): """average gray level (clipped on uint8) """ return _core16(kernel_mean,image,selem,mask,out,shift_x,shift_y,bitdepth) @@ -249,7 +249,7 @@ 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, - char shift_x=0, char shift_y=0, int bitdepth=8): + char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): """(g - average gray level)/2+midbin (clipped on uint8) """ return _core16(kernel_meansubstraction,image,selem,mask,out,shift_x,shift_y,bitdepth) @@ -258,7 +258,7 @@ 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, - char shift_x=0, char shift_y=0, int bitdepth=8): + char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): """local median """ return _core16(kernel_median,image,selem,mask,out,shift_x,shift_y,bitdepth) @@ -267,7 +267,7 @@ 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, - char shift_x=0, char shift_y=0, int bitdepth=8): + char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): """local minimum gray level """ return _core16(kernel_minimum,image,selem,mask,out,shift_x,shift_y,bitdepth) @@ -276,7 +276,7 @@ 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, - char shift_x=0, char shift_y=0, int bitdepth=8): + char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): """morphological contrast enhancement """ return _core16(kernel_morph_contr_enh,image,selem,mask,out,shift_x,shift_y,bitdepth) @@ -285,7 +285,7 @@ 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, - char shift_x=0, char shift_y=0, int bitdepth=8): + char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): """local mode """ return _core16(kernel_modal,image,selem,mask,out,shift_x,shift_y,bitdepth) @@ -294,7 +294,7 @@ 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, - char shift_x=0, char shift_y=0, int bitdepth=8): + char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): """returns the number of actual pixels of the structuring element inside the mask """ return _core16(kernel_pop,image,selem,mask,out,shift_x,shift_y,bitdepth) @@ -303,7 +303,7 @@ 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, - char shift_x=0, char shift_y=0, int bitdepth=8): + char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): """returns maxbin-1 if gray level higher than local mean, 0 else """ return _core16(kernel_threshold,image,selem,mask,out,shift_x,shift_y,bitdepth) @@ -312,7 +312,7 @@ 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, - char shift_x=0, char shift_y=0, int bitdepth=8): + char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): """top hat """ return _core16(kernel_tophat,image,selem,mask,out,shift_x,shift_y,bitdepth)