remplace int by Py_ssize_t and for rank16

This commit is contained in:
Olivier Debeir
2012-10-15 13:36:41 +02:00
parent c3a3f39bbc
commit b78a0460fb
3 changed files with 66 additions and 66 deletions
+2 -2
View File
@@ -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)
char shift_x, char shift_y,Py_ssize_t bitdepth)
+23 -23
View File
@@ -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 (image<maxbin).all()
@@ -66,8 +66,8 @@ char shift_x, char shift_y,int bitdepth):
out = np.ascontiguousarray(out)
# create extended image and mask
cdef int erows = rows+srows-1
cdef int ecols = cols+scols-1
cdef Py_ssize_t erows = rows+srows-1
cdef Py_ssize_t ecols = cols+scols-1
cdef np.ndarray emask = np.zeros((erows, ecols), dtype=np.uint8)
cdef np.ndarray eimage = np.zeros((erows, ecols), dtype=np.uint16)
@@ -86,30 +86,30 @@ char shift_x, char shift_y,int bitdepth):
cdef np.uint8_t* mask_data = <np.uint8_t*>mask.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 = <int*>malloc(maxbin * sizeof(int))
cdef Py_ssize_t* histo = <Py_ssize_t*>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 = <int*>malloc(max_se * sizeof(int))
cdef int* se_e_c = <int*>malloc(max_se * sizeof(int))
cdef int* se_w_r = <int*>malloc(max_se * sizeof(int))
cdef int* se_w_c = <int*>malloc(max_se * sizeof(int))
cdef int* se_n_r = <int*>malloc(max_se * sizeof(int))
cdef int* se_n_c = <int*>malloc(max_se * sizeof(int))
cdef int* se_s_r = <int*>malloc(max_se * sizeof(int))
cdef int* se_s_c = <int*>malloc(max_se * sizeof(int))
cdef Py_ssize_t* se_e_r = <Py_ssize_t*>malloc(max_se * sizeof(Py_ssize_t))
cdef Py_ssize_t* se_e_c = <Py_ssize_t*>malloc(max_se * sizeof(Py_ssize_t))
cdef Py_ssize_t* se_w_r = <Py_ssize_t*>malloc(max_se * sizeof(Py_ssize_t))
cdef Py_ssize_t* se_w_c = <Py_ssize_t*>malloc(max_se * sizeof(Py_ssize_t))
cdef Py_ssize_t* se_n_r = <Py_ssize_t*>malloc(max_se * sizeof(Py_ssize_t))
cdef Py_ssize_t* se_n_c = <Py_ssize_t*>malloc(max_se * sizeof(Py_ssize_t))
cdef Py_ssize_t* se_s_r = <Py_ssize_t*>malloc(max_se * sizeof(Py_ssize_t))
cdef Py_ssize_t* se_s_c = <Py_ssize_t*>malloc(max_se * sizeof(Py_ssize_t))
# build attack and release borders
# by using difference along axis
+41 -41
View File
@@ -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 <np.uint16_t>(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 <np.uint16_t>(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 <np.uint16_t>(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 <np.uint16_t>(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 <np.uint16_t>(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 <np.uint16_t>(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 <np.uint16_t>(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 <np.uint16_t>(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 <np.uint16_t>(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 <np.uint16_t>(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 <np.uint16_t>(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 <np.uint16_t>(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 <np.uint16_t>(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)