diff --git a/skimage/rank/__init__.py b/skimage/rank/__init__.py index 09812649..30d936db 100644 --- a/skimage/rank/__init__.py +++ b/skimage/rank/__init__.py @@ -1,3 +1,3 @@ from .rank import * from .percentile_rank import * -from .bilateral_rank import * \ No newline at end of file +from .bilateral_rank import * diff --git a/skimage/rank/_core16.pxd b/skimage/rank/_core16.pxd index f9bb47b3..a2843f76 100644 --- a/skimage/rank/_core16.pxd +++ b/skimage/rank/_core16.pxd @@ -8,10 +8,11 @@ cimport numpy as np cdef inline int int_max(int a, int b) cdef inline int int_min(int a, int b) -cdef inline _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), +cdef inline _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, - char shift_x, char shift_y,Py_ssize_t bitdepth, - float p0, float p1, Py_ssize_t s0, Py_ssize_t s1) \ No newline at end of file + char shift_x, char shift_y, Py_ssize_t bitdepth, + float p0, float p1, Py_ssize_t s0, Py_ssize_t s1) diff --git a/skimage/rank/_core16.pyx b/skimage/rank/_core16.pyx index edef264b..81fab0b4 100644 --- a/skimage/rank/_core16.pyx +++ b/skimage/rank/_core16.pyx @@ -19,18 +19,20 @@ from libc.stdlib cimport malloc, free #--------------------------------------------------------------------------- # generic cdef functions -cdef inline int int_max(int a, int b): return a if a >= b else b -cdef inline int int_min(int a, int b): return a if a <= b else b +cdef inline int int_max(int a, int b): + return a if a >= b else b +cdef inline int int_min(int a, int b): + return a if a <= b else b -cdef inline void histogram_increment(Py_ssize_t* histo,float *pop,np.uint16_t value): +cdef inline void histogram_increment(Py_ssize_t * histo, float * pop, np.uint16_t value): histo[value] += 1 pop[0] += 1. -cdef inline void histogram_decrement(Py_ssize_t* histo,float *pop,np.uint16_t value): +cdef inline void histogram_decrement(Py_ssize_t * histo, float * pop, np.uint16_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 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): """ returns 1 if given(r,c) coordinate are within the image frame ([0-rows],[0-cols]) and inside the given mask returns 0 otherwise @@ -38,19 +40,20 @@ cdef inline np.uint8_t is_in_mask(Py_ssize_t rows, Py_ssize_t cols,Py_ssize_t r, if r < 0 or r > rows - 1 or c < 0 or c > cols - 1: return 0 else: - if mask[r*cols+c]: + if mask[r * cols + c]: return 1 else: return 0 -cdef inline _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), +cdef inline _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, - char shift_x, char shift_y,Py_ssize_t bitdepth, - float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): + char shift_x, char shift_y, Py_ssize_t bitdepth, + float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): """ Main loop, this function computes the histogram for each image point - data is uint8 - result is uint8 casted @@ -69,16 +72,15 @@ cdef inline _core16(np.uint16_t kernel(Py_ssize_t*, float, np.uint16_t,Py_ssize_ assert centre_c >= 0 assert centre_r < srows assert centre_c < scols - assert bitdepth in range(2,13) - - maxbin_list = [0,0,4,8,16,32,64,128,256,512,1024,2048,4096] - midbin_list = [0,0,2,4,8,16,32,64,128,256,512,1024,2048] + assert bitdepth in range(2, 13) + maxbin_list = [0, 0, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096] + midbin_list = [0, 0, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048] #set maxbin and midbin - cdef Py_ssize_t maxbin=maxbin_list[bitdepth],midbin=midbin_list[bitdepth] + cdef Py_ssize_t maxbin = maxbin_list[bitdepth], midbin = midbin_list[bitdepth] - assert (imageout.data - cdef np.uint16_t* image_data = image.data - cdef np.uint8_t* mask_data = mask.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 # define local variable types 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 Py_ssize_t max_se = srows*scols + cdef Py_ssize_t max_se = srows * scols # number of element in each attack border cdef Py_ssize_t num_se_n, num_se_s, num_se_e, num_se_w # the current local histogram distribution - cdef Py_ssize_t* histo = malloc(maxbin * sizeof(Py_ssize_t)) + 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 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)) + 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 - t = np.hstack((selem,np.zeros((selem.shape[0],1)))) - t_e = np.diff(t,axis=1)==-1 + t = np.hstack((selem, np.zeros((selem.shape[0], 1)))) + t_e = np.diff(t, axis=1) == -1 - t = np.hstack((np.zeros((selem.shape[0],1)),selem)) - t_w = np.diff(t,axis=1)==1 + t = np.hstack((np.zeros((selem.shape[0], 1)), selem)) + t_w = np.diff(t, axis=1) == 1 - t = np.vstack((selem,np.zeros((1,selem.shape[1])))) - t_s = np.diff(t,axis=0)==-1 + t = np.vstack((selem, np.zeros((1, selem.shape[1])))) + t_s = np.diff(t, axis=0) == -1 - t = np.vstack((np.zeros((1,selem.shape[1])),selem)) - t_n = np.diff(t,axis=0)==1 + t = np.vstack((np.zeros((1, selem.shape[1])), selem)) + t_n = np.diff(t, axis=0) == 1 num_se_n = num_se_s = num_se_e = num_se_w = 0 for r in range(srows): for c in range(scols): - if t_e[r,c]: + if t_e[r, c]: se_e_r[num_se_e] = r - centre_r se_e_c[num_se_e] = c - centre_c num_se_e += 1 - if t_w[r,c]: + if t_w[r, c]: se_w_r[num_se_w] = r - centre_r se_w_c[num_se_w] = c - centre_c num_se_w += 1 - if t_n[r,c]: + if t_n[r, c]: se_n_r[num_se_n] = r - centre_r se_n_c[num_se_n] = c - centre_c num_se_n += 1 - if t_s[r,c]: + if t_s[r, c]: se_s_r[num_se_s] = r - centre_r se_s_c[num_se_s] = c - centre_c num_se_s += 1 @@ -175,99 +177,101 @@ cdef inline _core16(np.uint16_t kernel(Py_ssize_t*, float, np.uint16_t,Py_ssize_ rr = r - centre_r cc = c - centre_c if selem[r, c]: - if is_in_mask(rows,cols,rr,cc,mask_data): - histogram_increment(histo,&pop,image_data[rr * cols + cc]) + if is_in_mask(rows, cols, rr, cc, mask_data): + histogram_increment(histo, & pop, image_data[rr * cols + cc]) r = 0 c = 0 # kernel ------------------------------------------- - out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c], - bitdepth,maxbin,midbin,p0,p1,s0,s1) + out_data[r * cols + c] = kernel(histo, pop, image_data[r * cols + c], + bitdepth, maxbin, midbin, p0, p1, s0, s1) # kernel ------------------------------------------- # main loop r = 0 - for even_row in range(0,rows,2): + for even_row in range(0, rows, 2): # ---> west to east - for c in range(1,cols): + for c in range(1, cols): for s in range(num_se_e): rr = r + se_e_r[s] cc = c + se_e_c[s] - if is_in_mask(rows,cols,rr,cc,mask_data): - histogram_increment(histo,&pop,image_data[rr * cols + cc]) + if is_in_mask(rows, cols, rr, cc, mask_data): + histogram_increment(histo, & pop, image_data[rr * cols + cc]) for s in range(num_se_w): rr = r + se_w_r[s] cc = c + se_w_c[s] - 1 - if is_in_mask(rows,cols,rr,cc,mask_data): - histogram_decrement(histo,&pop,image_data[rr * cols + cc]) + if is_in_mask(rows, cols, rr, cc, mask_data): + histogram_decrement(histo, & pop, image_data[rr * cols + cc]) # kernel ------------------------------------------- - out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c ], - bitdepth,maxbin,midbin,p0,p1,s0,s1) + out_data[r * cols + c] = kernel( + histo, pop, image_data[r * cols + c], + bitdepth, maxbin, midbin, p0, p1, s0, s1) # kernel ------------------------------------------- r += 1 # pass to the next row - if r>=rows: + if r >= rows: break # ---> north to south for s in range(num_se_s): rr = r + se_s_r[s] cc = c + se_s_c[s] - if is_in_mask(rows,cols,rr,cc,mask_data): - histogram_increment(histo,&pop,image_data[rr * cols + cc]) + if is_in_mask(rows, cols, rr, cc, mask_data): + histogram_increment(histo, & pop, image_data[rr * cols + cc]) for s in range(num_se_n): rr = r + se_n_r[s] - 1 cc = c + se_n_c[s] - if is_in_mask(rows,cols,rr,cc,mask_data): - histogram_decrement(histo,&pop,image_data[rr * cols + cc]) + if is_in_mask(rows, cols, rr, cc, mask_data): + histogram_decrement(histo, & pop, image_data[rr * cols + cc]) # kernel ------------------------------------------- - out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c], - bitdepth,maxbin,midbin,p0,p1,s0,s1) + out_data[r * cols + c] = kernel(histo, pop, image_data[r * cols + c], + bitdepth, maxbin, midbin, p0, p1, s0, s1) # kernel ------------------------------------------- # ---> east to west - for c in range(cols-2,-1,-1): + for c in range(cols - 2, -1, -1): for s in range(num_se_w): rr = r + se_w_r[s] cc = c + se_w_c[s] - if is_in_mask(rows,cols,rr,cc,mask_data): - histogram_increment(histo,&pop,image_data[rr * cols + cc]) + if is_in_mask(rows, cols, rr, cc, mask_data): + histogram_increment(histo, & pop, image_data[rr * cols + cc]) for s in range(num_se_e): rr = r + se_e_r[s] cc = c + se_e_c[s] + 1 - if is_in_mask(rows,cols,rr,cc,mask_data): - histogram_decrement(histo,&pop,image_data[rr * cols + cc]) + if is_in_mask(rows, cols, rr, cc, mask_data): + histogram_decrement(histo, & pop, image_data[rr * cols + cc]) # kernel ------------------------------------------- - out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c ], - bitdepth,maxbin,midbin,p0,p1,s0,s1) + out_data[r * cols + c] = kernel( + histo, pop, image_data[r * cols + c], + bitdepth, maxbin, midbin, p0, p1, s0, s1) # kernel ------------------------------------------- r += 1 # pass to the next row - if r>=rows: + if r >= rows: break # ---> north to south for s in range(num_se_s): rr = r + se_s_r[s] cc = c + se_s_c[s] - if is_in_mask(rows,cols,rr,cc,mask_data): - histogram_increment(histo,&pop,image_data[rr * cols + cc]) + if is_in_mask(rows, cols, rr, cc, mask_data): + histogram_increment(histo, & pop, image_data[rr * cols + cc]) for s in range(num_se_n): rr = r + se_n_r[s] - 1 cc = c + se_n_c[s] - if is_in_mask(rows,cols,rr,cc,mask_data): - histogram_decrement(histo,&pop,image_data[rr * cols + cc]) + if is_in_mask(rows, cols, rr, cc, mask_data): + histogram_decrement(histo, & pop, image_data[rr * cols + cc]) # kernel ------------------------------------------- - out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c ], - bitdepth,maxbin,midbin,p0,p1,s0,s1) + out_data[r * cols + c] = kernel(histo, pop, image_data[r * cols + c], + bitdepth, maxbin, midbin, p0, p1, s0, s1) # kernel ------------------------------------------- # release memory allocated by malloc diff --git a/skimage/rank/_core8.pxd b/skimage/rank/_core8.pxd index a677e915..1a170500 100644 --- a/skimage/rank/_core8.pxd +++ b/skimage/rank/_core8.pxd @@ -8,10 +8,10 @@ cdef inline np.uint8_t uint8_min(np.uint8_t a, np.uint8_t b) # 8 bit core kernel receives extra information about data inferior and superior percentiles #--------------------------------------------------------------------------- -cdef inline _core8(np.uint8_t kernel(Py_ssize_t*, float, np.uint8_t, float, float, Py_ssize_t, Py_ssize_t), +cdef inline _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, char shift_x, char shift_y, float p0, float p1, Py_ssize_t s0, Py_ssize_t s1) - diff --git a/skimage/rank/_core8.pyx b/skimage/rank/_core8.pyx index 86c40a6d..7851388d 100644 --- a/skimage/rank/_core8.pyx +++ b/skimage/rank/_core8.pyx @@ -15,23 +15,25 @@ cimport numpy as np from libc.stdlib cimport malloc, free # generic cdef functions -cdef inline np.uint8_t uint8_max(np.uint8_t a, np.uint8_t b): return a if a >= b else b -cdef inline np.uint8_t uint8_min(np.uint8_t a, np.uint8_t b): return a if a <= b else b +cdef inline np.uint8_t uint8_max(np.uint8_t a, np.uint8_t b): + return a if a >= b else b +cdef inline np.uint8_t uint8_min(np.uint8_t a, np.uint8_t b): + return a if a <= b else b #--------------------------------------------------------------------------- # 8 bit core kernel #--------------------------------------------------------------------------- -cdef inline void histogram_increment(Py_ssize_t* histo,float *pop,np.uint8_t value): +cdef inline void histogram_increment(Py_ssize_t * histo, float * pop, np.uint8_t value): histo[value] += 1 pop[0] += 1. -cdef inline void histogram_decrement(Py_ssize_t* histo,float *pop,np.uint8_t value): +cdef inline void histogram_decrement(Py_ssize_t * histo, float * pop, np.uint8_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 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): """ returns 1 if given(r,c) coordinate are within the image frame ([0-rows],[0-cols]) and inside the given mask returns 0 otherwise @@ -39,17 +41,18 @@ cdef inline np.uint8_t is_in_mask(Py_ssize_t rows, Py_ssize_t cols,Py_ssize_t r, if r < 0 or r > rows - 1 or c < 0 or c > cols - 1: return 0 else: - if mask[r*cols+c]: + if mask[r * cols + c]: return 1 else: return 0 -cdef inline _core8(np.uint8_t kernel(Py_ssize_t*, float, np.uint8_t, float, float, Py_ssize_t, Py_ssize_t), +cdef inline _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, - char shift_x, char shift_y, float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): + char shift_x, char shift_y, float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): """ Main loop, this function computes the histogram for each image point - data is uint8 - result is uint8 casted @@ -88,9 +91,9 @@ cdef inline _core8(np.uint8_t kernel(Py_ssize_t*, float, np.uint8_t, float, floa # 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 np.uint8_t * out_data = out.data + cdef np.uint8_t * image_data = image.data + cdef np.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 @@ -99,59 +102,59 @@ cdef inline _core8(np.uint8_t kernel(Py_ssize_t*, float, np.uint8_t, float, floa cdef float pop # allocate memory with malloc - cdef Py_ssize_t max_se = srows*scols + cdef Py_ssize_t max_se = srows * scols # number of element in each attack border cdef Py_ssize_t num_se_n, num_se_s, num_se_e, num_se_w # the current local histogram distribution - cdef Py_ssize_t* histo = malloc(256 * sizeof(Py_ssize_t)) + cdef Py_ssize_t * histo = malloc(256 * 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 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)) + 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 - t = np.hstack((selem,np.zeros((selem.shape[0],1)))) - t_e = np.diff(t,axis=1)==-1 + t = np.hstack((selem, np.zeros((selem.shape[0], 1)))) + t_e = np.diff(t, axis=1) == -1 - t = np.hstack((np.zeros((selem.shape[0],1)),selem)) - t_w = np.diff(t,axis=1)==1 + t = np.hstack((np.zeros((selem.shape[0], 1)), selem)) + t_w = np.diff(t, axis=1) == 1 - t = np.vstack((selem,np.zeros((1,selem.shape[1])))) - t_s = np.diff(t,axis=0)==-1 + t = np.vstack((selem, np.zeros((1, selem.shape[1])))) + t_s = np.diff(t, axis=0) == -1 - t = np.vstack((np.zeros((1,selem.shape[1])),selem)) - t_n = np.diff(t,axis=0)==1 + t = np.vstack((np.zeros((1, selem.shape[1])), selem)) + t_n = np.diff(t, axis=0) == 1 num_se_n = num_se_s = num_se_e = num_se_w = 0 for r in range(srows): for c in range(scols): - if t_e[r,c]: + if t_e[r, c]: se_e_r[num_se_e] = r - centre_r se_e_c[num_se_e] = c - centre_c num_se_e += 1 - if t_w[r,c]: + if t_w[r, c]: se_w_r[num_se_w] = r - centre_r se_w_c[num_se_w] = c - centre_c num_se_w += 1 - if t_n[r,c]: + if t_n[r, c]: se_n_r[num_se_n] = r - centre_r se_n_c[num_se_n] = c - centre_c num_se_n += 1 - if t_s[r,c]: + if t_s[r, c]: se_s_r[num_se_s] = r - centre_r se_s_c[num_se_s] = c - centre_c num_se_s += 1 @@ -167,94 +170,99 @@ cdef inline _core8(np.uint8_t kernel(Py_ssize_t*, float, np.uint8_t, float, floa rr = r - centre_r cc = c - centre_c if selem[r, c]: - if is_in_mask(rows,cols,rr,cc,mask_data): - histogram_increment(histo,&pop,image_data[rr * cols + cc]) + if is_in_mask(rows, cols, rr, cc, mask_data): + histogram_increment(histo, & pop, image_data[rr * cols + cc]) r = 0 c = 0 # kernel -------------------------------------------------------------------- - out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c],p0,p1,s0,s1) + out_data[r * cols + c] = kernel(histo, pop, image_data[r * cols + + c], p0, p1, s0, s1) # kernel -------------------------------------------------------------------- # main loop r = 0 - for even_row in range(0,rows,2): + for even_row in range(0, rows, 2): # ---> west to east - for c in range(1,cols): + for c in range(1, cols): for s in range(num_se_e): rr = r + se_e_r[s] cc = c + se_e_c[s] - if is_in_mask(rows,cols,rr,cc,mask_data): - histogram_increment(histo,&pop,image_data[rr * cols + cc]) + if is_in_mask(rows, cols, rr, cc, mask_data): + histogram_increment(histo, & pop, image_data[rr * cols + cc]) for s in range(num_se_w): rr = r + se_w_r[s] cc = c + se_w_c[s] - 1 - if is_in_mask(rows,cols,rr,cc,mask_data): - histogram_decrement(histo,&pop,image_data[rr * cols + cc]) + if is_in_mask(rows, cols, rr, cc, mask_data): + histogram_decrement(histo, & pop, image_data[rr * cols + cc]) # kernel -------------------------------------------------------------------- - out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c],p0,p1,s0,s1) + out_data[r * cols + c] = kernel( + histo, pop, image_data[r * cols + c], p0, p1, s0, s1) # kernel -------------------------------------------------------------------- r += 1 # pass to the next row - if r>=rows: + if r >= rows: break # ---> north to south for s in range(num_se_s): rr = r + se_s_r[s] cc = c + se_s_c[s] - if is_in_mask(rows,cols,rr,cc,mask_data): - histogram_increment(histo,&pop,image_data[rr * cols + cc]) + if is_in_mask(rows, cols, rr, cc, mask_data): + histogram_increment(histo, & pop, image_data[rr * cols + cc]) for s in range(num_se_n): rr = r + se_n_r[s] - 1 cc = c + se_n_c[s] - if is_in_mask(rows,cols,rr,cc,mask_data): - histogram_decrement(histo,&pop,image_data[rr * cols + cc]) + if is_in_mask(rows, cols, rr, cc, mask_data): + histogram_decrement(histo, & pop, image_data[rr * cols + cc]) # kernel -------------------------------------------------------------------- - out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c],p0,p1,s0,s1) + out_data[r * cols + c] = kernel(histo, pop, image_data[r * + cols + c], p0, p1, s0, s1) # kernel -------------------------------------------------------------------- # ---> east to west - for c in range(cols-2,-1,-1): + for c in range(cols - 2, -1, -1): for s in range(num_se_w): rr = r + se_w_r[s] cc = c + se_w_c[s] - if is_in_mask(rows,cols,rr,cc,mask_data): - histogram_increment(histo,&pop,image_data[rr * cols + cc]) + if is_in_mask(rows, cols, rr, cc, mask_data): + histogram_increment(histo, & pop, image_data[rr * cols + cc]) for s in range(num_se_e): rr = r + se_e_r[s] cc = c + se_e_c[s] + 1 - if is_in_mask(rows,cols,rr,cc,mask_data): - histogram_decrement(histo,&pop,image_data[rr * cols + cc]) + if is_in_mask(rows, cols, rr, cc, mask_data): + histogram_decrement(histo, & pop, image_data[rr * cols + cc]) # kernel -------------------------------------------------------------------- - out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c],p0,p1,s0,s1) + out_data[r * cols + c] = kernel( + histo, pop, image_data[r * cols + c], p0, p1, s0, s1) # kernel -------------------------------------------------------------------- r += 1 # pass to the next row - if r>=rows: + if r >= rows: break # ---> north to south for s in range(num_se_s): rr = r + se_s_r[s] cc = c + se_s_c[s] - if is_in_mask(rows,cols,rr,cc,mask_data): - histogram_increment(histo,&pop,image_data[rr * cols + cc]) + if is_in_mask(rows, cols, rr, cc, mask_data): + histogram_increment(histo, & pop, image_data[rr * cols + cc]) for s in range(num_se_n): rr = r + se_n_r[s] - 1 cc = c + se_n_c[s] - if is_in_mask(rows,cols,rr,cc,mask_data): - histogram_decrement(histo,&pop,image_data[rr * cols + cc]) + if is_in_mask(rows, cols, rr, cc, mask_data): + histogram_decrement(histo, & pop, image_data[rr * cols + cc]) # kernel -------------------------------------------------------------------- - out_data[r * cols + c] = kernel(histo,pop,image_data[r * cols + c],p0,p1,s0,s1) + out_data[r * cols + c] = kernel(histo, pop, image_data[r * + cols + c], p0, p1, s0, s1) # kernel -------------------------------------------------------------------- # release memory allocated by malloc @@ -271,4 +279,3 @@ cdef inline _core8(np.uint8_t kernel(Py_ssize_t*, float, np.uint8_t, float, floa free(histo) return out - diff --git a/skimage/rank/_crank16.pyx b/skimage/rank/_crank16.pyx index 2fdda1e3..ce8510db 100644 --- a/skimage/rank/_crank16.pyx +++ b/skimage/rank/_crank16.pyx @@ -21,13 +21,14 @@ from _core16 cimport _core16 # kernels uint16 take extra parameter for defining the bitdepth # ----------------------------------------------------------------- -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): - cdef Py_ssize_t 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, + float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): + cdef Py_ssize_t i, imin, imax, delta if pop: - for i in range(maxbin-1,-1,-1): + for i in range(maxbin - 1, -1, -1): if histo[i]: imax = i break @@ -35,47 +36,50 @@ float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): if histo[i]: imin = i break - delta = imax-imin - if delta>0: - return (1.*(maxbin-1)*(g-imin)/delta) + delta = imax - imin + if delta > 0: + return < np.uint16_t > (1. * (maxbin - 1) * (g - imin) / delta) else: - return (imax-imin) + return < np.uint16_t > (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 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 Py_ssize_t i for i in range(maxbin): if histo[i]: break - return (g-i) + return < np.uint16_t > (g - 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, -float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): +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 Py_ssize_t i cdef float sum = 0. if pop: for i in range(maxbin): sum += histo[i] - if i>=g: + if i >= g: break - return (((maxbin-1)*sum)/pop) + return < np.uint16_t > (((maxbin - 1) * sum) / pop) else: - return (0) + return < np.uint16_t > (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 Py_ssize_t 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, + float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): + cdef Py_ssize_t i, imin, imax if pop: - for i in range(maxbin-1,-1,-1): + for i in range(maxbin - 1, -1, -1): if histo[i]: imax = i break @@ -83,96 +87,103 @@ float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): if histo[i]: imin = i break - return (imax-imin) + return < np.uint16_t > (imax - imin) else: - return (0) + return < np.uint16_t > (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 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 Py_ssize_t i if pop: - for i in range(maxbin-1,-1,-1): + for i in range(maxbin - 1, -1, -1): if histo[i]: - return (i) + return < np.uint16_t > (i) - return (0) + return < np.uint16_t > (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 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 Py_ssize_t i cdef float mean = 0. if pop: for i in range(maxbin): - mean += histo[i]*i - return (mean/pop) + mean += histo[i] * i + return < np.uint16_t > (mean / pop) else: - return (0) + return < np.uint16_t > (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 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 Py_ssize_t i cdef float mean = 0. if pop: for i in range(maxbin): - mean += histo[i]*i - return ((g-mean/pop)/2.+(midbin-1)) + mean += histo[i] * i + return < np.uint16_t > ((g - mean / pop) / 2. + (midbin - 1)) else: - return (0) + return < np.uint16_t > (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 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 Py_ssize_t i - cdef float sum = pop/2.0 + cdef float sum = pop / 2.0 if pop: for i in range(maxbin): if histo[i]: sum -= histo[i] - if sum<0: - return (i) + if sum < 0: + return < np.uint16_t > (i) - return (0) + return < np.uint16_t > (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 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 Py_ssize_t i if pop: for i in range(maxbin): if histo[i]: - return (i) + return < np.uint16_t > (i) - return (0) + return < np.uint16_t > (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 Py_ssize_t 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, + float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): + cdef Py_ssize_t hmax = 0, imax = 0 if pop: for i in range(maxbin): - if histo[i]>hmax: + if histo[i] > hmax: hmax = histo[i] imax = i - return (imax) + return < np.uint16_t > (imax) - return (0) + return < np.uint16_t > (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 Py_ssize_t 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, + float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): + cdef Py_ssize_t i, imin, imax if pop: - for i in range(maxbin-1,-1,-1): + for i in range(maxbin - 1, -1, -1): if histo[i]: imax = i break @@ -180,80 +191,89 @@ float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): if histo[i]: imin = i break - if imax-g < g-imin: - return (imax) + if imax - g < g - imin: + return < np.uint16_t > (imax) else: - return (imin) + return < np.uint16_t > (imin) else: - return (0) + return < np.uint16_t > (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 (pop) +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 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 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 Py_ssize_t i cdef float mean = 0. if pop: for i in range(maxbin): - mean += histo[i]*i - return (g>(mean/pop)) + mean += histo[i] * i + return < np.uint16_t > (g > (mean / pop)) else: - return (0) + return < np.uint16_t > (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 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 Py_ssize_t i - for i in range(maxbin-1,-1,-1): + for i in range(maxbin - 1, -1, -1): if histo[i]: break - return (i-g) + return < np.uint16_t > (i - g) # ----------------------------------------------------------------- # 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, - char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): + 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, Py_ssize_t bitdepth=8): """bottom hat """ - return _core16(kernel_autolevel,image,selem,mask,out,shift_x,shift_y,bitdepth,.0,.0,0,0) + return _core16(kernel_autolevel, image, selem, mask, out, shift_x, shift_y, bitdepth, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): + 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, Py_ssize_t bitdepth=8): """bottom hat """ - return _core16(kernel_bottomhat,image,selem,mask,out,shift_x,shift_y,bitdepth,.0,.0,0,0) + return _core16(kernel_bottomhat, image, selem, mask, out, shift_x, shift_y, bitdepth, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): + 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, Py_ssize_t bitdepth=8): """local egalisation of the gray level """ - return _core16(kernel_equalize,image,selem,mask,out,shift_x,shift_y,bitdepth,.0,.0,0,0) + return _core16(kernel_equalize, image, selem, mask, out, shift_x, shift_y, bitdepth, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): + 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, Py_ssize_t bitdepth=8): """local maximum - local minimum gray level """ - return _core16(kernel_gradient,image,selem,mask,out,shift_x,shift_y,bitdepth,.0,.0,0,0) + return _core16(kernel_gradient, image, selem, mask, out, shift_x, shift_y, bitdepth, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 0) + def maximum(np.ndarray[np.uint16_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] selem, @@ -262,34 +282,38 @@ def maximum(np.ndarray[np.uint16_t, ndim=2] image, 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,.0,.0,0,0) + return _core16(kernel_maximum, image, selem, mask, out, shift_x, shift_y, bitdepth, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): + 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, Py_ssize_t bitdepth=8): """average gray level (clipped on uint8) """ - return _core16(kernel_mean,image,selem,mask,out,shift_x,shift_y,bitdepth,.0,.0,0,0) + return _core16(kernel_mean, image, selem, mask, out, shift_x, shift_y, bitdepth, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): + 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, 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,.0,.0,0,0) + return _core16(kernel_meansubstraction, image, selem, mask, out, shift_x, shift_y, bitdepth, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): + 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, Py_ssize_t bitdepth=8): """local median """ - return _core16(kernel_median,image,selem,mask,out,shift_x,shift_y,bitdepth,.0,.0,0,0) + return _core16(kernel_median, image, selem, mask, out, shift_x, shift_y, bitdepth, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 0) + def minimum(np.ndarray[np.uint16_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] selem, @@ -298,49 +322,54 @@ def minimum(np.ndarray[np.uint16_t, ndim=2] image, 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,.0,.0,0,0) + return _core16(kernel_minimum, image, selem, mask, out, shift_x, shift_y, bitdepth, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): + 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, Py_ssize_t bitdepth=8): """morphological contrast enhancement """ - return _core16(kernel_morph_contr_enh,image,selem,mask,out,shift_x,shift_y,bitdepth,.0,.0,0,0) + return _core16(kernel_morph_contr_enh, image, selem, mask, out, shift_x, shift_y, bitdepth, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): + 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, Py_ssize_t bitdepth=8): """local mode """ - return _core16(kernel_modal,image,selem,mask,out,shift_x,shift_y,bitdepth,.0,.0,0,0) + return _core16(kernel_modal, image, selem, mask, out, shift_x, shift_y, bitdepth, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): + 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, 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,.0,.0,0,0) + return _core16(kernel_pop, image, selem, mask, out, shift_x, shift_y, bitdepth, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): + 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, 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,.0,.0,0,0) + return _core16(kernel_threshold, image, selem, mask, out, shift_x, shift_y, bitdepth, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): + 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, Py_ssize_t bitdepth=8): """top hat """ - return _core16(kernel_tophat,image,selem,mask,out,shift_x,shift_y,bitdepth,.0,.0,0,0) + return _core16(kernel_tophat, image, selem, mask, out, shift_x, shift_y, bitdepth, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 0) diff --git a/skimage/rank/_crank16_bilateral.pyx b/skimage/rank/_crank16_bilateral.pyx index 46028ad3..b5103be4 100644 --- a/skimage/rank/_crank16_bilateral.pyx +++ b/skimage/rank/_crank16_bilateral.pyx @@ -22,54 +22,53 @@ from _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): - cdef int i,bilat_pop=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 int i, bilat_pop = 0 cdef float mean = 0. if pop: for i in range(maxbin): - if (g>(i-s0)) and (g<(i+s1)): + if (g > (i - s0)) and (g < (i + s1)): bilat_pop += histo[i] - mean += histo[i]*i + mean += histo[i] * i if bilat_pop: - return (mean/bilat_pop) + return < np.uint16_t > (mean / bilat_pop) else: - return (0) + return < np.uint16_t > (0) else: - return (0) + return < np.uint16_t > (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 int i,bilat_pop=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 int i, bilat_pop = 0 if pop: for i in range(maxbin): - if (g>(i-s0)) and (g<(i+s1)): + if (g > (i - s0)) and (g < (i + s1)): bilat_pop += histo[i] - return (bilat_pop) + return < np.uint16_t > (bilat_pop) else: - return (0) + return < np.uint16_t > (0) # ----------------------------------------------------------------- # python wrappers # ----------------------------------------------------------------- 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, int s0=1, int s1=1): + 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, int s0=1, int s1=1): """average gray level (clipped on uint8) """ - return _core16(kernel_mean,image,selem,mask,out,shift_x,shift_y,bitdepth,0.,0.,s0,s1) + return _core16(kernel_mean, image, selem, mask, out, shift_x, shift_y, 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, - char shift_x=0, char shift_y=0, int bitdepth=8, int s0=1, int s1=1): + 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, int s0=1, int s1=1): """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,.0,.0,s0,s1) - + return _core16(kernel_pop, image, selem, mask, out, shift_x, shift_y, bitdepth, .0, .0, s0, s1) diff --git a/skimage/rank/_crank16_percentiles.pyx b/skimage/rank/_crank16_percentiles.pyx index 4fa3661c..527d2aed 100644 --- a/skimage/rank/_crank16_percentiles.pyx +++ b/skimage/rank/_crank16_percentiles.pyx @@ -7,64 +7,64 @@ import numpy as np cimport numpy as np # import main loop -from _core16 cimport _core16,int_min,int_max +from _core16 cimport _core16, int_min, int_max # ----------------------------------------------------------------- # kernels uint16 (SOFT version using percentiles) # ----------------------------------------------------------------- -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): - cdef int i,imin,imax,sum,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, float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): + cdef int i, imin, imax, sum, delta if pop: sum = 0 - p1 = 1.0-p1 + p1 = 1.0 - p1 for i in range(maxbin): sum += histo[i] - if sum>p0*pop: + if sum > p0 * pop: imin = i break sum = 0 - for i in range(maxbin-1,-1,-1): + for i in range(maxbin - 1, -1, -1): sum += histo[i] - if sum>p1*pop: + if sum > p1 * pop: imax = i break - delta = imax-imin - if delta>0: - return (1.0*(maxbin-1)*(int_min(int_max(imin,g),imax)-imin)/delta) + delta = imax - imin + if delta > 0: + return < np.uint16_t > (1.0 * (maxbin - 1) * (int_min(int_max(imin, g), imax) - imin) / delta) else: - return (imax-imin) + return < np.uint16_t > (imax - imin) else: - return (0) + return < np.uint16_t > (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 int i,imin,imax,sum,delta +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 int i, imin, imax, sum, delta if pop: sum = 0 - p1 = 1.0-p1 + p1 = 1.0 - p1 for i in range(maxbin): sum += histo[i] - if sum>=p0*pop: + if sum >= p0 * pop: imin = i break sum = 0 - for i in range((maxbin-1),-1,-1): + for i in range((maxbin - 1), -1, -1): sum += histo[i] - if sum>=p1*pop: + if sum >= p1 * pop: imax = i break - return (imax-imin) + return < np.uint16_t > (imax - imin) else: - return (0) + return < np.uint16_t > (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 int i,sum,mean,n +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 int i, sum, mean, n if pop: sum = 0 @@ -72,19 +72,19 @@ cdef inline np.uint16_t kernel_mean(Py_ssize_t* histo, float pop, np.uint16_t g, n = 0 for i in range(maxbin): sum += histo[i] - if (sum>=p0*pop) and (sum<=p1*pop): + if (sum >= p0 * pop) and (sum <= p1 * pop): n += histo[i] - mean += histo[i]*i + mean += histo[i] * i - if n>0: - return (1.0*mean/n) + if n > 0: + return < np.uint16_t > (1.0 * mean / n) else: - return (0) + return < np.uint16_t > (0) else: - return (0) + return < np.uint16_t > (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 int i,sum,mean,n +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 int i, sum, mean, n if pop: sum = 0 @@ -92,160 +92,166 @@ cdef inline np.uint16_t kernel_mean_substraction(Py_ssize_t* histo, float pop, n n = 0 for i in range(maxbin): sum += histo[i] - if (sum>=p0*pop) and (sum<=p1*pop): + if (sum >= p0 * pop) and (sum <= p1 * pop): n += histo[i] - mean += histo[i]*i - if n>0: - return ((g-(mean/n))*.5+midbin) + mean += histo[i] * i + if n > 0: + return < np.uint16_t > ((g - (mean / n)) * .5 + midbin) else: - return (0) + return < np.uint16_t > (0) else: - return (0) + return < np.uint16_t > (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 int i,imin,imax,sum,delta +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 int i, imin, imax, sum, delta if pop: sum = 0 - p1 = 1.0-p1 + p1 = 1.0 - p1 for i in range(maxbin): sum += histo[i] - if sum>p0*pop: + if sum > p0 * pop: imin = i break sum = 0 - for i in range((maxbin-1),-1,-1): + for i in range((maxbin - 1), -1, -1): sum += histo[i] - if sum>p1*pop: + if sum > p1 * pop: imax = i break - if g>imax: - return imax - if gimin - if imax-g < g-imin: - return imax + if g > imax: + return < np.uint16_t > imax + if g < imin: + return < np.uint16_t > imin + if imax - g < g - imin: + return < np.uint16_t > imax else: - return imin + return < np.uint16_t > imin else: - return (0) + return < np.uint16_t > (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 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 int i cdef float sum = 0. if pop: for i in range(maxbin): sum += histo[i] - if sum>=p0*pop: + if sum >= p0 * pop: break - return (i) + return < np.uint16_t > (i) else: - return (0) + return < np.uint16_t > (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 int i,sum,n +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 int i, sum, n if pop: sum = 0 n = 0 for i in range(maxbin): sum += histo[i] - if (sum>=p0*pop) and (sum<=p1*pop): + if (sum >= p0 * pop) and (sum <= p1 * pop): n += histo[i] - return (n) + return < np.uint16_t > (n) else: - return (0) + return < np.uint16_t > (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 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 int i cdef float sum = 0. if pop: for i in range(maxbin): sum += histo[i] - if sum>=p0*pop: + if sum >= p0 * pop: break - return ((maxbin-1)*(g>=i)) + return < np.uint16_t > ((maxbin - 1) * (g >= i)) else: - return (0) + return < np.uint16_t > (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, - char shift_x=0, char shift_y=0, int bitdepth=8, float p0=0., float p1=0.): + 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, float p0=0., float p1=0.): """bottom hat """ - return _core16(kernel_autolevel,image,selem,mask,out,shift_x,shift_y,bitdepth,p0,p1,0,0) + return _core16(kernel_autolevel, image, selem, mask, out, shift_x, shift_y, bitdepth, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, int bitdepth=8, float p0=0., float p1=0.): + 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, float p0=0., float p1=0.): """return p0,p1 percentile gradient """ - return _core16(kernel_gradient,image,selem,mask,out,shift_x,shift_y,bitdepth,p0,p1,0,0) + return _core16(kernel_gradient, image, selem, mask, out, shift_x, shift_y, bitdepth, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, int bitdepth=8, float p0=0., float p1=0.): + 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, float p0=0., float p1=0.): """return mean between [p0 and p1] percentiles """ - return _core16(kernel_mean,image,selem,mask,out,shift_x,shift_y,bitdepth,p0,p1,0,0) + return _core16(kernel_mean, image, selem, mask, out, shift_x, shift_y, bitdepth, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, int bitdepth=8, float p0=0., float p1=0.): + 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, float p0=0., float p1=0.): """return original - mean between [p0 and p1] percentiles *.5 +127 """ - return _core16(kernel_mean_substraction,image,selem,mask,out,shift_x,shift_y,bitdepth,p0,p1,0,0) + return _core16(kernel_mean_substraction, image, selem, mask, out, shift_x, shift_y, bitdepth, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, int bitdepth=8, float p0=0., float p1=0.): + 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, float p0=0., float p1=0.): """reforce contrast using percentiles """ - return _core16(kernel_morph_contr_enh,image,selem,mask,out,shift_x,shift_y,bitdepth,p0,p1,0,0) + return _core16(kernel_morph_contr_enh, image, selem, mask, out, shift_x, shift_y, bitdepth, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, int bitdepth=8, float p0=0., float p1=0.): + 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, float p0=0., float p1=0.): """return p0 percentile """ - return _core16(kernel_percentile,image,selem,mask,out,shift_x,shift_y,bitdepth,p0,p1,0,0) + return _core16(kernel_percentile, image, selem, mask, out, shift_x, shift_y, bitdepth, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, int bitdepth=8, float p0=0., float p1=0.): + 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, float p0=0., float p1=0.): """return nb of pixels between [p0 and p1] """ - return _core16(kernel_pop,image,selem,mask,out,shift_x,shift_y,bitdepth,p0,p1,0,0) + return _core16(kernel_pop, image, selem, mask, out, shift_x, shift_y, bitdepth, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, int bitdepth=8, float p0=0., float p1=0.): + 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, float p0=0., float p1=0.): """return (maxbin-1) if g > percentile p0 """ - return _core16(kernel_threshold,image,selem,mask,out,shift_x,shift_y,bitdepth,p0,p1,0,0) + return _core16(kernel_threshold, image, selem, mask, out, shift_x, shift_y, bitdepth, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 0) diff --git a/skimage/rank/_crank8.pyx b/skimage/rank/_crank8.pyx index a0b20073..94124cf7 100644 --- a/skimage/rank/_crank8.pyx +++ b/skimage/rank/_crank8.pyx @@ -21,12 +21,13 @@ from _core8 cimport _core8 # kernels uint8 # ----------------------------------------------------------------- -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): - cdef Py_ssize_t i,imin,imax,delta +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): + cdef Py_ssize_t i, imin, imax, delta if pop: - for i in range(255,-1,-1): + for i in range(255, -1, -1): if histo[i]: imax = i break @@ -34,47 +35,49 @@ float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): if histo[i]: imin = i break - delta = imax-imin - if delta>0: - return (255.*(g-imin)/delta) + delta = imax - imin + if delta > 0: + return < np.uint8_t > (255. * (g - imin) / delta) else: - return (imax-imin) + return < np.uint8_t > (imax - imin) else: - return (0) + return < np.uint8_t > (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 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 Py_ssize_t i for i in range(256): if histo[i]: break - return (g-i) + return < np.uint8_t > (g - i) -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 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 Py_ssize_t i cdef float sum = 0. if pop: for i in range(256): sum += histo[i] - if i>=g: + if i >= g: break - return ((255*sum)/pop) + return < np.uint8_t > ((255 * sum) / pop) else: - 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 Py_ssize_t i,imin,imax + return < np.uint8_t > (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 Py_ssize_t i, imin, imax if pop: - for i in range(255,-1,-1): + for i in range(255, -1, -1): if histo[i]: imax = i break @@ -82,89 +85,95 @@ float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): if histo[i]: imin = i break - return (imax-imin) + return < np.uint8_t > (imax - imin) else: - return (0) + return < np.uint8_t > (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 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 Py_ssize_t i if pop: - for i in range(255,-1,-1): + for i in range(255, -1, -1): if histo[i]: - return (i) + return < np.uint8_t > (i) - return (0) + return < np.uint8_t > (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 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 Py_ssize_t i cdef float mean = 0. if pop: for i in range(256): - mean += histo[i]*i - return (mean/pop) + mean += histo[i] * i + return < np.uint8_t > (mean / pop) else: - return (0) + return < np.uint8_t > (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 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 Py_ssize_t i cdef float mean = 0. if pop: for i in range(256): - mean += histo[i]*i - return ((g-mean/pop)/2.+127) + mean += histo[i] * i + return < np.uint8_t > ((g - mean / pop) / 2. + 127) else: - return (0) + return < np.uint8_t > (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 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 Py_ssize_t i - cdef float sum = pop/2.0 + cdef float sum = pop / 2.0 if pop: for i in range(256): if histo[i]: sum -= histo[i] - if sum<0: - return (i) + if sum < 0: + return < np.uint8_t > (i) - return (0) + return < np.uint8_t > (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 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 Py_ssize_t i if pop: for i in range(256): if histo[i]: - return (i) + return < np.uint8_t > (i) - return (0) + return < np.uint8_t > (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 Py_ssize_t hmax=0,imax=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 Py_ssize_t hmax = 0, imax = 0 if pop: for i in range(256): - if histo[i]>hmax: + if histo[i] > hmax: hmax = histo[i] imax = i - return (imax) + return < np.uint8_t > (imax) - return (0) + return < np.uint8_t > (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 Py_ssize_t i,imin,imax +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 Py_ssize_t i, imin, imax if pop: - for i in range(255,-1,-1): + for i in range(255, -1, -1): if histo[i]: imax = i break @@ -172,77 +181,85 @@ float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): if histo[i]: imin = i break - if imax-g < g-imin: - return (imax) + if imax - g < g - imin: + return < np.uint8_t > (imax) else: - return (imin) + return < np.uint8_t > (imin) else: - return (0) + return < np.uint8_t > (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): - return (pop) +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): + return < np.uint8_t > (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 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 Py_ssize_t i cdef float mean = 0. if pop: for i in range(256): - mean += histo[i]*i - return (g>(mean/pop)) + mean += histo[i] * i + return < np.uint8_t > (g > (mean / pop)) else: - return (0) + return < np.uint8_t > (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 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 Py_ssize_t i - for i in range(255,-1,-1): + for i in range(255, -1, -1): if histo[i]: break - return (i-g) + return < np.uint8_t > (i - g) # ----------------------------------------------------------------- # python wrappers # ----------------------------------------------------------------- + + 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, - char shift_x=0, char shift_y=0): + 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, + char shift_x=0, char shift_y=0): """bottom hat """ - return _core8(kernel_autolevel,image,selem,mask,out,shift_x,shift_y,.0,.0,0,0) + return _core8(kernel_autolevel, image, selem, mask, out, shift_x, shift_y, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0): + 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, + char shift_x=0, char shift_y=0): """bottom hat """ - return _core8(kernel_bottomhat,image,selem,mask,out,shift_x,shift_y,.0,.0,0,0) + return _core8(kernel_bottomhat, image, selem, mask, out, shift_x, shift_y, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0): + 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, + char shift_x=0, char shift_y=0): """local egalisation of the gray level """ - return _core8(kernel_equalize,image,selem,mask,out,shift_x,shift_y,.0,.0,0,0) + return _core8(kernel_equalize, image, selem, mask, out, shift_x, shift_y, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0): + 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, + char shift_x=0, char shift_y=0): """local maximum - local minimum gray level """ - return _core8(kernel_gradient,image,selem,mask,out,shift_x,shift_y,.0,.0,0,0) + return _core8(kernel_gradient, image, selem, mask, out, shift_x, shift_y, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 0) + def maximum(np.ndarray[np.uint8_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] selem, @@ -251,34 +268,38 @@ def maximum(np.ndarray[np.uint8_t, ndim=2] image, char shift_x=0, char shift_y=0): """local maximum gray level """ - return _core8(kernel_maximum,image,selem,mask,out,shift_x,shift_y,.0,.0,0,0) + return _core8(kernel_maximum, image, selem, mask, out, shift_x, shift_y, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0): + 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, + char shift_x=0, char shift_y=0): """average gray level (clipped on uint8) """ - return _core8(kernel_mean,image,selem,mask,out,shift_x,shift_y,.0,.0,0,0) + return _core8(kernel_mean, image, selem, mask, out, shift_x, shift_y, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0): + 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, + char shift_x=0, char shift_y=0): """(g - average gray level)/2+127 (clipped on uint8) """ - return _core8(kernel_meansubstraction,image,selem,mask,out,shift_x,shift_y,.0,.0,0,0) + return _core8(kernel_meansubstraction, image, selem, mask, out, shift_x, shift_y, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0): + 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, + char shift_x=0, char shift_y=0): """local median """ - return _core8(kernel_median,image,selem,mask,out,shift_x,shift_y,.0,.0,0,0) + return _core8(kernel_median, image, selem, mask, out, shift_x, shift_y, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 0) + def minimum(np.ndarray[np.uint8_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] selem, @@ -287,50 +308,54 @@ def minimum(np.ndarray[np.uint8_t, ndim=2] image, char shift_x=0, char shift_y=0): """local minimum gray level """ - return _core8(kernel_minimum,image,selem,mask,out,shift_x,shift_y,.0,.0,0,0) + return _core8(kernel_minimum, image, selem, mask, out, shift_x, shift_y, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0): + 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, + char shift_x=0, char shift_y=0): """morphological contrast enhancement """ - return _core8(kernel_morph_contr_enh,image,selem,mask,out,shift_x,shift_y,.0,.0,0,0) + return _core8(kernel_morph_contr_enh, image, selem, mask, out, shift_x, shift_y, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0): + 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, + char shift_x=0, char shift_y=0): """local mode """ - return _core8(kernel_modal,image,selem,mask,out,shift_x,shift_y,.0,.0,0,0) + return _core8(kernel_modal, image, selem, mask, out, shift_x, shift_y, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0): + 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, + char shift_x=0, char shift_y=0): """returns the number of actual pixels of the structuring element inside the mask """ - return _core8(kernel_pop,image,selem,mask,out,shift_x,shift_y,.0,.0,0,0) + return _core8(kernel_pop, image, selem, mask, out, shift_x, shift_y, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0): + 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, + char shift_x=0, char shift_y=0): """returns 255 if gray level higher than local mean, 0 else """ - return _core8(kernel_threshold,image,selem,mask,out,shift_x,shift_y,.0,.0,0,0) + return _core8(kernel_threshold, image, selem, mask, out, shift_x, shift_y, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0): + 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, + char shift_x=0, char shift_y=0): """top hat """ - return _core8(kernel_tophat,image,selem,mask,out,shift_x,shift_y,.0,.0,0,0) - + return _core8(kernel_tophat, image, selem, mask, out, shift_x, shift_y, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 0) diff --git a/skimage/rank/_crank8_percentiles.pyx b/skimage/rank/_crank8_percentiles.pyx index 16ad49d5..5441eac7 100644 --- a/skimage/rank/_crank8_percentiles.pyx +++ b/skimage/rank/_crank8_percentiles.pyx @@ -7,66 +7,66 @@ import numpy as np cimport numpy as np # import main loop -from _core8 cimport _core8,uint8_max,uint8_min +from _core8 cimport _core8, uint8_max, uint8_min # ----------------------------------------------------------------- # kernels uint8 (SOFT version using percentiles) # ----------------------------------------------------------------- -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): - cdef int i,imin,imax,sum,delta +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): + cdef int i, imin, imax, sum, delta if pop: sum = 0 - p1 = 1.0-p1 + p1 = 1.0 - p1 imin = 0 imax = 255 for i in range(256): sum += histo[i] - if sum>(p0*pop): + if sum > (p0 * pop): imin = i break sum = 0 - for i in range(255,-1,-1): + for i in range(255, -1, -1): sum += histo[i] - if sum>(p1*pop): + if sum > (p1 * pop): imax = i break - delta = imax-imin - if delta>0: - return (255*(uint8_min(uint8_max(imin,g),imax)-imin)/delta) + delta = imax - imin + if delta > 0: + return < np.uint8_t > (255 * (uint8_min(uint8_max(imin, g), imax) - imin) / delta) else: - return (imax-imin) + return < np.uint8_t > (imax - imin) else: - return (128) + return < np.uint8_t > (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 int i,imin,imax,sum,delta +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 int i, imin, imax, sum, delta if pop: sum = 0 - p1 = 1.0-p1 + p1 = 1.0 - p1 for i in range(256): sum += histo[i] - if sum>=p0*pop: + if sum >= p0 * pop: imin = i break sum = 0 - for i in range(255,-1,-1): + for i in range(255, -1, -1): sum += histo[i] - if sum>=p1*pop: + if sum >= p1 * pop: imax = i break - return (imax-imin) + return < np.uint8_t > (imax - imin) else: - return (0) + return < np.uint8_t > (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 int i,sum,mean,n +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 int i, sum, mean, n if pop: sum = 0 @@ -74,18 +74,18 @@ cdef inline np.uint8_t kernel_mean(Py_ssize_t* histo, float pop, np.uint8_t g, f n = 0 for i in range(256): sum += histo[i] - if (sum>=p0*pop) and (sum<=p1*pop): + if (sum >= p0 * pop) and (sum <= p1 * pop): n += histo[i] - mean += histo[i]*i - if n>0: - return (1.0*mean/n) + mean += histo[i] * i + if n > 0: + return < np.uint8_t > (1.0 * mean / n) else: - return (0) + return < np.uint8_t > (0) else: - return (0) + return < np.uint8_t > (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 int i,sum,mean,n +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 int i, sum, mean, n if pop: sum = 0 @@ -93,160 +93,166 @@ cdef inline np.uint8_t kernel_mean_substraction(Py_ssize_t* histo, float pop, np n = 0 for i in range(256): sum += histo[i] - if (sum>=p0*pop) and (sum<=p1*pop): + if (sum >= p0 * pop) and (sum <= p1 * pop): n += histo[i] - mean += histo[i]*i - if n>0: - return ((g-(mean/n))*.5+127) + mean += histo[i] * i + if n > 0: + return < np.uint8_t > ((g - (mean / n)) * .5 + 127) else: - return (0) + return < np.uint8_t > (0) else: - return (0) + return < np.uint8_t > (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 int i,imin,imax,sum,delta +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 int i, imin, imax, sum, delta if pop: sum = 0 - p1 = 1.0-p1 + p1 = 1.0 - p1 for i in range(256): sum += histo[i] - if sum>=p0*pop: + if sum >= p0 * pop: imin = i break sum = 0 - for i in range(255,-1,-1): + for i in range(255, -1, -1): sum += histo[i] - if sum>=p1*pop: + if sum >= p1 * pop: imax = i break - if g>imax: - return imax - if gimin - if imax-g < g-imin: - return imax + if g > imax: + return < np.uint8_t > imax + if g < imin: + return < np.uint8_t > imin + if imax - g < g - imin: + return < np.uint8_t > imax else: - return imin + return < np.uint8_t > imin else: - return (0) + return < np.uint8_t > (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 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 int i cdef float sum = 0. if pop: for i in range(256): sum += histo[i] - if sum>=p0*pop: + if sum >= p0 * pop: break - return (i) + return < np.uint8_t > (i) else: - return (0) + return < np.uint8_t > (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 int i,sum,n +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 int i, sum, n if pop: sum = 0 n = 0 for i in range(256): sum += histo[i] - if (sum>=p0*pop) and (sum<=p1*pop): + if (sum >= p0 * pop) and (sum <= p1 * pop): n += histo[i] - return (n) + return < np.uint8_t > (n) else: - return (0) + return < np.uint8_t > (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 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 int i cdef float sum = 0. if pop: for i in range(256): sum += histo[i] - if sum>=p0*pop: + if sum >= p0 * pop: break - return (255*(g>=i)) + return < np.uint8_t > (255 * (g >= i)) else: - return (0) + return < np.uint8_t > (0) # ----------------------------------------------------------------- # python wrappers # ----------------------------------------------------------------- + + 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, - char shift_x=0, char shift_y=0, float p0=0., float p1=0.): + 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, + char shift_x=0, char shift_y=0, float p0=0., float p1=0.): """autolevel """ - return _core8(kernel_autolevel,image,selem,mask,out,shift_x,shift_y,p0,p1,0,0) + return _core8(kernel_autolevel, image, selem, mask, out, shift_x, shift_y, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, float p0=0., float p1=0.): + 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, + char shift_x=0, char shift_y=0, float p0=0., float p1=0.): """return p0,p1 percentile gradient """ - return _core8(kernel_gradient,image,selem,mask,out,shift_x,shift_y,p0,p1,0,0) + return _core8(kernel_gradient, image, selem, mask, out, shift_x, shift_y, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, float p0=0., float p1=0.): + 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, + char shift_x=0, char shift_y=0, float p0=0., float p1=0.): """return mean between [p0 and p1] percentiles """ - return _core8(kernel_mean,image,selem,mask,out,shift_x,shift_y,p0,p1,0,0) + return _core8(kernel_mean, image, selem, mask, out, shift_x, shift_y, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, float p0=0., float p1=0.): + 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, + char shift_x=0, char shift_y=0, float p0=0., float p1=0.): """return original - mean between [p0 and p1] percentiles *.5 +127 """ - return _core8(kernel_mean_substraction,image,selem,mask,out,shift_x,shift_y,p0,p1,0,0) + return _core8(kernel_mean_substraction, image, selem, mask, out, shift_x, shift_y, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, float p0=0., float p1=0.): + 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, + char shift_x=0, char shift_y=0, float p0=0., float p1=0.): """reforce contrast using percentiles """ - return _core8(kernel_morph_contr_enh,image,selem,mask,out,shift_x,shift_y,p0,p1,0,0) + return _core8(kernel_morph_contr_enh, image, selem, mask, out, shift_x, shift_y, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, float p0=0., float p1=0.): + 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, + char shift_x=0, char shift_y=0, float p0=0., float p1=0.): """return p0 percentile """ - return _core8(kernel_percentile,image,selem,mask,out,shift_x,shift_y,p0,p1,0,0) + return _core8(kernel_percentile, image, selem, mask, out, shift_x, shift_y, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, float p0=0., float p1=0.): + 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, + char shift_x=0, char shift_y=0, float p0=0., float p1=0.): """return nb of pixels between [p0 and p1] """ - return _core8(kernel_pop,image,selem,mask,out,shift_x,shift_y,p0,p1,0,0) + return _core8(kernel_pop, image, selem, mask, out, shift_x, shift_y, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 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, - char shift_x=0, char shift_y=0, float p0=0., float p1=0.): + 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, + char shift_x=0, char shift_y=0, float p0=0., float p1=0.): """return 255 if g > percentile p0 """ - return _core8(kernel_threshold,image,selem,mask,out,shift_x,shift_y,p0,p1,0,0) + return _core8(kernel_threshold, image, selem, mask, out, shift_x, shift_y, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 0) diff --git a/skimage/rank/bilateral_rank.py b/skimage/rank/bilateral_rank.py index 5fd66e21..97db8f99 100644 --- a/skimage/rank/bilateral_rank.py +++ b/skimage/rank/bilateral_rank.py @@ -17,7 +17,8 @@ from generic import find_bitdepth import _crank16_bilateral -__all__ = ['bilateral_mean','bilateral_pop'] +__all__ = ['bilateral_mean', 'bilateral_pop'] + def _apply(func8, func16, image, selem, out, mask, shift_x, shift_y, s0, s1): selem = img_as_ubyte(selem) @@ -30,9 +31,9 @@ def _apply(func8, func16, image, selem, out, mask, shift_x, shift_y, s0, s1): else: raise TypeError("only uint8 and uint16 image supported!") bitdepth = find_bitdepth(image) - if bitdepth>11: + if bitdepth > 11: raise ValueError("only uint16 <4096 image (12bit) supported!") - return func16(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,bitdepth=bitdepth+1,out=out,s0=s0,s1=s1) + return func16(image, selem, shift_x=shift_x, shift_y=shift_y, mask=mask, bitdepth=bitdepth + 1, out=out, s0=s0, s1=s1) def bilateral_mean(image, selem, out=None, mask=None, shift_x=False, shift_y=False, s0=10, s1=10): @@ -69,12 +70,13 @@ def bilateral_mean(image, selem, out=None, mask=None, shift_x=False, shift_y=Fal to be updated >>> # Local mean >>> from skimage.morphology import square + >>> import skimage.rank as rank >>> ima8 = 255*np.array([[0, 0, 0, 0, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint8) - >>> bilateral_mean(ima8, square(3), s0=10,s1=10) + >>> rank.bilateral_mean(ima8, square(3), s0=10,s1=10) array([[ 0, 0, 0, 0, 0], [ 0, 255, 255, 255, 0], [ 0, 255, 255, 255, 0], @@ -86,7 +88,7 @@ def bilateral_mean(image, selem, out=None, mask=None, shift_x=False, shift_y=Fal ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint16) - >>> bilateral_mean(ima16, square(3), s0=10,s1=10) + >>> rank.bilateral_mean(ima16, square(3), s0=10,s1=10) array([[ 0, 0, 0, 0, 0], [ 0, 4095, 4095, 4095, 0], [ 0, 4095, 4095, 4095, 0], @@ -132,12 +134,13 @@ def bilateral_pop(image, selem, out=None, mask=None, shift_x=False, shift_y=Fals to be updated >>> # Local mean >>> from skimage.morphology import square + >>> import skimage.rank as rank >>> ima8 = 255*np.array([[0, 0, 0, 0, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint8) - >>> bilateral_pop(ima8, square(3), s0=10,s1=10) + >>> rank.bilateral_pop(ima8, square(3), s0=10,s1=10) array([[3, 4, 3, 4, 3], [4, 4, 6, 4, 4], [3, 6, 9, 6, 3], @@ -149,7 +152,7 @@ def bilateral_pop(image, selem, out=None, mask=None, shift_x=False, shift_y=Fals ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint16) - >>> bilateral_pop(ima16, square(3), s0=10,s1=10) + >>> rank.bilateral_pop(ima16, square(3), s0=10,s1=10) array([[3, 4, 3, 4, 3], [4, 4, 6, 4, 4], [3, 6, 9, 6, 3], @@ -160,3 +163,9 @@ def bilateral_pop(image, selem, out=None, mask=None, shift_x=False, shift_y=Fals return _apply(None, _crank16_bilateral.pop, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y, s0=s0, s1=s1) +if __name__ == "__main__": + import sys + sys.path.append('.') + + import doctest + doctest.testmod(verbose=True) diff --git a/skimage/rank/generic.py b/skimage/rank/generic.py index e8808e5e..94fc3130 100644 --- a/skimage/rank/generic.py +++ b/skimage/rank/generic.py @@ -1,10 +1,11 @@ import numpy as np + def find_bitdepth(image): """returns the max bith depth of a uint16 image """ umax = np.max(image) - if umax>2: + if umax > 2: return int(np.log2(umax)) else: return 1 diff --git a/skimage/rank/percentile_rank.py b/skimage/rank/percentile_rank.py index e7d760da..6ceb503d 100644 --- a/skimage/rank/percentile_rank.py +++ b/skimage/rank/percentile_rank.py @@ -23,26 +23,29 @@ from skimage import img_as_ubyte import numpy as np from generic import find_bitdepth -import _crank16_percentiles,_crank8_percentiles +import _crank16_percentiles +import _crank8_percentiles + +__all__ = ['percentile_autolevel', 'percentile_gradient', + 'percentile_mean', 'percentile_mean_substraction', + 'percentile_morph_contr_enh', 'percentile', 'percentile_pop', 'percentile_threshold'] -__all__ = ['percentile_autolevel','percentile_gradient', - 'percentile_mean','percentile_mean_substraction', - 'percentile_morph_contr_enh','percentile_pop'] def _apply(func8, func16, image, selem, out, mask, shift_x, shift_y, p0, p1): selem = img_as_ubyte(selem) if mask is not None: mask = img_as_ubyte(mask) if image.dtype == np.uint8: - return func8(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,out=out,p0=p0,p1=p1) + return func8(image, selem, shift_x=shift_x, shift_y=shift_y, mask=mask, out=out, p0=p0, p1=p1) elif image.dtype == np.uint16: bitdepth = find_bitdepth(image) - if bitdepth>11: + if bitdepth > 11: raise ValueError("only uint16 <4096 image (12bit) supported!") - return func16(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,bitdepth=bitdepth+1,out=out,p0=p0,p1=p1) + return func16(image, selem, shift_x=shift_x, shift_y=shift_y, mask=mask, bitdepth=bitdepth + 1, out=out, p0=p0, p1=p1) else: raise TypeError("only uint8 and uint16 image supported!") + def percentile_autolevel(image, selem, out=None, mask=None, shift_x=False, shift_y=False, p0=.0, p1=1.): """Return greyscale local autolevel of an image. @@ -77,15 +80,16 @@ def percentile_autolevel(image, selem, out=None, mask=None, shift_x=False, shift to be updated >>> # Local mean >>> from skimage.morphology import square + >>> import skimage.rank as rank >>> ima8 = 255*np.array([[0, 0, 0, 0, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint8) - >>> percentile_autolevel(ima8, square(3), p0=0.,p1=1.) + >>> rank.percentile_autolevel(ima8, square(3), p0=0.,p1=1.) array([[ 0, 0, 0, 0, 0], [ 0, 255, 255, 255, 0], - [ 0, 255, 255, 255, 0], + [ 0, 255, 0, 255, 0], [ 0, 255, 255, 255, 0], [ 0, 0, 0, 0, 0]], dtype=uint8) @@ -94,10 +98,10 @@ def percentile_autolevel(image, selem, out=None, mask=None, shift_x=False, shift ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint16) - >>> percentile_autolevel(ima16, square(3), p0=0.,p1=1.) + >>> rank.percentile_autolevel(ima16, square(3), p0=0.,p1=1.) array([[ 0, 0, 0, 0, 0], [ 0, 4095, 4095, 4095, 0], - [ 0, 4095, 4095, 4095, 0], + [ 0, 4095, 0, 4095, 0], [ 0, 4095, 4095, 4095, 0], [ 0, 0, 0, 0, 0]], dtype=uint16) @@ -105,6 +109,7 @@ def percentile_autolevel(image, selem, out=None, mask=None, shift_x=False, shift return _apply(_crank8_percentiles.autolevel, _crank16_percentiles.autolevel, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y, p0=p0, p1=p1) + def percentile_gradient(image, selem, out=None, mask=None, shift_x=False, shift_y=False, p0=.0, p1=1.): """Return greyscale local percentile_gradient of an image. @@ -139,12 +144,13 @@ def percentile_gradient(image, selem, out=None, mask=None, shift_x=False, shift_ to be updated >>> # Local gradient >>> from skimage.morphology import square + >>> import skimage.rank as rank >>> ima8 = 255*np.array([[0, 0, 0, 0, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint8) - >>> percentile_gradient(ima8, square(3), p0=0.,p1=1.) + >>> rank.percentile_gradient(ima8, square(3), p0=0.,p1=1.) array([[255, 255, 255, 255, 255], [255, 255, 255, 255, 255], [255, 255, 255, 255, 255], @@ -156,7 +162,7 @@ def percentile_gradient(image, selem, out=None, mask=None, shift_x=False, shift_ ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint16) - >>> percentile_gradient(ima16, square(3), p0=0.,p1=1.) + >>> rank.percentile_gradient(ima16, square(3), p0=0.,p1=1.) array([[4095, 4095, 4095, 4095, 4095], [4095, 4095, 4095, 4095, 4095], [4095, 4095, 4095, 4095, 4095], @@ -167,6 +173,7 @@ def percentile_gradient(image, selem, out=None, mask=None, shift_x=False, shift_ return _apply(_crank8_percentiles.gradient, _crank16_percentiles.gradient, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y, p0=p0, p1=p1) + def percentile_mean(image, selem, out=None, mask=None, shift_x=False, shift_y=False, p0=.0, p1=1.): """Return greyscale local mean of an image. @@ -201,12 +208,13 @@ def percentile_mean(image, selem, out=None, mask=None, shift_x=False, shift_y=Fa to be updated >>> # Local mean >>> from skimage.morphology import square + >>> import skimage.rank as rank >>> ima8 = 255*np.array([[0, 0, 0, 0, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint8) - >>> percentile_mean(ima8, square(3),p0=0.,p1=1.) + >>> rank.percentile_mean(ima8, square(3),p0=0.,p1=1.) array([[ 63, 85, 127, 85, 63], [ 85, 113, 170, 113, 85], [127, 170, 255, 170, 127], @@ -218,7 +226,7 @@ def percentile_mean(image, selem, out=None, mask=None, shift_x=False, shift_y=Fa ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint16) - >>> percentile_mean(ima16, square(3),p0=0.,p1=1.) + >>> rank.percentile_mean(ima16, square(3),p0=0.,p1=1.) array([[1023, 1365, 2047, 1365, 1023], [1365, 1820, 2730, 1820, 1365], [2047, 2730, 4095, 2730, 2047], @@ -229,6 +237,7 @@ def percentile_mean(image, selem, out=None, mask=None, shift_x=False, shift_y=Fa return _apply(_crank8_percentiles.mean, _crank16_percentiles.mean, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y, p0=p0, p1=p1) + def percentile_mean_substraction(image, selem, out=None, mask=None, shift_x=False, shift_y=False, p0=.0, p1=1.): """Return greyscale local mean_substraction of an image. @@ -263,12 +272,13 @@ def percentile_mean_substraction(image, selem, out=None, mask=None, shift_x=Fals to be updated >>> # Local mean_substraction >>> from skimage.morphology import square + >>> import skimage.rank as rank >>> ima8 = 255*np.array([[0, 0, 0, 0, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint8) - >>> percentile_mean_substraction(ima8, square(3), p0=0.,p1=1.) + >>> rank.percentile_mean_substraction(ima8, square(3), p0=0.,p1=1.) array([[ 95, 84, 63, 84, 95], [ 84, 198, 169, 198, 84], [ 63, 169, 127, 169, 63], @@ -280,7 +290,7 @@ def percentile_mean_substraction(image, selem, out=None, mask=None, shift_x=Fals ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint16) - >>> percentile_mean_substraction(ima16, square(3), p0=0.,p1=1.) + >>> rank.percentile_mean_substraction(ima16, square(3), p0=0.,p1=1.) array([[1536, 1365, 1024, 1365, 1536], [1365, 3185, 2730, 3185, 1365], [1024, 2730, 2048, 2730, 1024], @@ -291,6 +301,7 @@ def percentile_mean_substraction(image, selem, out=None, mask=None, shift_x=Fals return _apply(_crank8_percentiles.mean_substraction, _crank16_percentiles.mean_substraction, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y, p0=p0, p1=p1) + def percentile_morph_contr_enh(image, selem, out=None, mask=None, shift_x=False, shift_y=False, p0=.0, p1=1.): """Return greyscale local morph_contr_enh of an image. @@ -325,12 +336,13 @@ def percentile_morph_contr_enh(image, selem, out=None, mask=None, shift_x=False, to be updated >>> # Local mean >>> from skimage.morphology import square + >>> import skimage.rank as rank >>> ima8 = 255*np.array([[0, 0, 0, 0, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint8) - >>> percentile_morph_contr_enh(ima8, square(3), p0=0.,p1=1.) + >>> rank.percentile_morph_contr_enh(ima8, square(3), p0=0.,p1=1.) array([[ 0, 0, 0, 0, 0], [ 0, 255, 255, 255, 0], [ 0, 255, 255, 255, 0], @@ -342,7 +354,7 @@ def percentile_morph_contr_enh(image, selem, out=None, mask=None, shift_x=False, ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint16) - >>> percentile_morph_contr_enh(ima16, square(3), p0=0.,p1=1.) + >>> rank.percentile_morph_contr_enh(ima16, square(3), p0=0.,p1=1.) array([[ 0, 0, 0, 0, 0], [ 0, 4095, 4095, 4095, 0], [ 0, 4095, 4095, 4095, 0], @@ -353,6 +365,7 @@ def percentile_morph_contr_enh(image, selem, out=None, mask=None, shift_x=False, return _apply(_crank8_percentiles.morph_contr_enh, _crank16_percentiles.morph_contr_enh, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y, p0=p0, p1=p1) + def percentile(image, selem, out=None, mask=None, shift_x=False, shift_y=False, p0=.0, p1=1.): """Return greyscale local percentile of an image. @@ -387,12 +400,13 @@ def percentile(image, selem, out=None, mask=None, shift_x=False, shift_y=False, to be updated >>> # Local mean >>> from skimage.morphology import square + >>> import skimage.rank as rank >>> ima8 = 128*np.array([[0, 0, 0, 0, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint8) - >>> percentile(ima8, square(3), p0=0.,p1=1.) + >>> rank.percentile(ima8, square(3), p0=0.,p1=1.) array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], @@ -404,7 +418,7 @@ def percentile(image, selem, out=None, mask=None, shift_x=False, shift_y=False, ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint16) - >>> percentile(ima16, square(3), p0=0.,p1=1.) + >>> rank.percentile(ima16, square(3), p0=0.,p1=1.) array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], @@ -416,6 +430,7 @@ def percentile(image, selem, out=None, mask=None, shift_x=False, shift_y=False, return _apply(_crank8_percentiles.percentile, _crank16_percentiles.percentile, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y, p0=p0, p1=p1) + def percentile_pop(image, selem, out=None, mask=None, shift_x=False, shift_y=False, p0=.0, p1=1.): """Return greyscale local pop of an image. @@ -450,12 +465,13 @@ def percentile_pop(image, selem, out=None, mask=None, shift_x=False, shift_y=Fal to be updated >>> # Local mean >>> from skimage.morphology import square + >>> import skimage.rank as rank >>> ima8 = 255*np.array([[0, 0, 0, 0, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint8) - >>> percentile_pop(ima8, square(3), p0=0.,p1=1.) + >>> rank.percentile_pop(ima8, square(3), p0=0.,p1=1.) array([[4, 6, 6, 6, 4], [6, 9, 9, 9, 6], [6, 9, 9, 9, 6], @@ -467,7 +483,7 @@ def percentile_pop(image, selem, out=None, mask=None, shift_x=False, shift_y=Fal ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint16) - >>> percentile_pop(ima16, square(3), p0=0.,p1=1.) + >>> rank.percentile_pop(ima16, square(3), p0=0.,p1=1.) array([[4, 6, 6, 6, 4], [6, 9, 9, 9, 6], [6, 9, 9, 9, 6], @@ -478,6 +494,7 @@ def percentile_pop(image, selem, out=None, mask=None, shift_x=False, shift_y=Fal return _apply(_crank8_percentiles.pop, _crank16_percentiles.pop, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y, p0=p0, p1=p1) + def percentile_threshold(image, selem, out=None, mask=None, shift_x=False, shift_y=False, p0=.0, p1=1.): """Return greyscale local threshold of an image. @@ -512,12 +529,13 @@ def percentile_threshold(image, selem, out=None, mask=None, shift_x=False, shift to be updated >>> # Local mean >>> from skimage.morphology import square + >>> import skimage.rank as rank >>> ima8 = 255*np.array([[0, 0, 0, 0, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint8) - >>> percentile_threshold(ima8, square(3), p0=0.,p1=1.) + >>> rank.percentile_threshold(ima8, square(3), p0=0.,p1=1.) array([[255, 255, 255, 255, 255], [255, 255, 255, 255, 255], [255, 255, 255, 255, 255], @@ -529,7 +547,7 @@ def percentile_threshold(image, selem, out=None, mask=None, shift_x=False, shift ... [0, 1, 1, 1, 0], ... [0, 1, 1, 1, 0], ... [0, 0, 0, 0, 0]], dtype=np.uint16) - >>> percentile_threshold(ima16, square(3), p0=0.,p1=1.) + >>> rank.percentile_threshold(ima16, square(3), p0=0.,p1=1.) array([[4095, 4095, 4095, 4095, 4095], [4095, 4095, 4095, 4095, 4095], [4095, 4095, 4095, 4095, 4095], @@ -539,4 +557,12 @@ def percentile_threshold(image, selem, out=None, mask=None, shift_x=False, shift """ - return _apply(_crank8_percentiles.threshold, _crank16_percentiles.threshold, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y, p0=p0, p1=p1) \ No newline at end of file + return _apply(_crank8_percentiles.threshold, _crank16_percentiles.threshold, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y, p0=p0, p1=p1) + + +if __name__ == "__main__": + import sys + sys.path.append('.') + + import doctest + doctest.testmod(verbose=True) diff --git a/skimage/rank/rank.py b/skimage/rank/rank.py index c080c291..d07c9037 100644 --- a/skimage/rank/rank.py +++ b/skimage/rank/rank.py @@ -21,25 +21,27 @@ from skimage import img_as_ubyte import numpy as np from generic import find_bitdepth -import _crank16,_crank8 +import _crank16 +import _crank8 + +__all__ = ['autolevel', 'bottomhat', 'equalize', 'gradient', 'maximum', 'mean', 'meansubstraction', 'median', 'minimum', 'modal', 'morph_contr_enh', 'pop', 'threshold', 'tophat'] -__all__ = ['autolevel','bottomhat','equalize','gradient','maximum','mean' - ,'meansubstraction','median','minimum','modal','morph_contr_enh','pop','threshold', 'tophat'] def _apply(func8, func16, image, selem, out, mask, shift_x, shift_y): selem = img_as_ubyte(selem) if mask is not None: mask = img_as_ubyte(mask) if image.dtype == np.uint8: - return func8(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,out=out) + return func8(image, selem, shift_x=shift_x, shift_y=shift_y, mask=mask, out=out) elif image.dtype == np.uint16: bitdepth = find_bitdepth(image) - if bitdepth>11: + if bitdepth > 11: raise ValueError("only uint16 <4096 image (12bit) supported!") - return func16(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,bitdepth=bitdepth+1,out=out) + return func16(image, selem, shift_x=shift_x, shift_y=shift_y, mask=mask, bitdepth=bitdepth + 1, out=out) else: raise TypeError("only uint8 and uint16 image supported!") + def autolevel(image, selem, out=None, mask=None, shift_x=False, shift_y=False): """Return greyscale local autolevel of an image. @@ -101,6 +103,7 @@ def autolevel(image, selem, out=None, mask=None, shift_x=False, shift_y=False): return _apply(_crank8.autolevel, _crank16.autolevel, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y) + def bottomhat(image, selem, out=None, mask=None, shift_x=False, shift_y=False): """Return greyscale local bottomhat of an image. @@ -161,6 +164,7 @@ def bottomhat(image, selem, out=None, mask=None, shift_x=False, shift_y=False): return _apply(_crank8.bottomhat, _crank16.bottomhat, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y) + def equalize(image, selem, out=None, mask=None, shift_x=False, shift_y=False): """Return greyscale local equalize of an image. @@ -221,6 +225,7 @@ def equalize(image, selem, out=None, mask=None, shift_x=False, shift_y=False): return _apply(_crank8.equalize, _crank16.equalize, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y) + def gradient(image, selem, out=None, mask=None, shift_x=False, shift_y=False): """Return greyscale local gradient of an image. @@ -282,6 +287,7 @@ def gradient(image, selem, out=None, mask=None, shift_x=False, shift_y=False): return _apply(_crank8.gradient, _crank16.gradient, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y) + def maximum(image, selem, out=None, mask=None, shift_x=False, shift_y=False): """Return greyscale local maximum of an image. @@ -343,6 +349,7 @@ def maximum(image, selem, out=None, mask=None, shift_x=False, shift_y=False): return _apply(_crank8.maximum, _crank16.maximum, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y) + def mean(image, selem, out=None, mask=None, shift_x=False, shift_y=False): """Return greyscale local mean of an image. @@ -404,6 +411,7 @@ def mean(image, selem, out=None, mask=None, shift_x=False, shift_y=False): return _apply(_crank8.mean, _crank16.mean, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y) + def meansubstraction(image, selem, out=None, mask=None, shift_x=False, shift_y=False): """Return greyscale local meansubstraction of an image. @@ -465,6 +473,7 @@ def meansubstraction(image, selem, out=None, mask=None, shift_x=False, shift_y=F return _apply(_crank8.meansubstraction, _crank16.meansubstraction, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y) + def median(image, selem, out=None, mask=None, shift_x=False, shift_y=False): """Return greyscale local median of an image. @@ -526,6 +535,7 @@ def median(image, selem, out=None, mask=None, shift_x=False, shift_y=False): return _apply(_crank8.median, _crank16.median, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y) + def minimum(image, selem, out=None, mask=None, shift_x=False, shift_y=False): """Return greyscale local minimum of an image. @@ -588,6 +598,7 @@ def minimum(image, selem, out=None, mask=None, shift_x=False, shift_y=False): return _apply(_crank8.minimum, _crank16.minimum, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y) + def modal(image, selem, out=None, mask=None, shift_x=False, shift_y=False): """Return greyscale local modal of an image. @@ -650,6 +661,7 @@ def modal(image, selem, out=None, mask=None, shift_x=False, shift_y=False): return _apply(_crank8.modal, _crank16.modal, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y) + def morph_contr_enh(image, selem, out=None, mask=None, shift_x=False, shift_y=False): """Return greyscale local morph_contr_enh of an image. @@ -711,6 +723,7 @@ def morph_contr_enh(image, selem, out=None, mask=None, shift_x=False, shift_y=Fa return _apply(_crank8.morph_contr_enh, _crank16.morph_contr_enh, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y) + def pop(image, selem, out=None, mask=None, shift_x=False, shift_y=False): """Return greyscale local pop of an image. @@ -772,6 +785,7 @@ def pop(image, selem, out=None, mask=None, shift_x=False, shift_y=False): return _apply(_crank8.pop, _crank16.pop, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y) + def threshold(image, selem, out=None, mask=None, shift_x=False, shift_y=False): """Return greyscale local threshold of an image. @@ -834,6 +848,7 @@ def threshold(image, selem, out=None, mask=None, shift_x=False, shift_y=False): return _apply(_crank8.threshold, _crank16.threshold, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y) + def tophat(image, selem, out=None, mask=None, shift_x=False, shift_y=False): """Return greyscale local tophat of an image. @@ -899,4 +914,4 @@ if __name__ == "__main__": sys.path.append('.') import doctest - doctest.testmod(verbose=True) \ No newline at end of file + doctest.testmod(verbose=True) diff --git a/skimage/rank/setup.py b/skimage/rank/setup.py index e1f996f7..c6a3dbb9 100644 --- a/skimage/rank/setup.py +++ b/skimage/rank/setup.py @@ -5,13 +5,13 @@ from skimage._build import cython base_path = os.path.abspath(os.path.dirname(__file__)) + def configuration(parent_package='', top_path=None): from numpy.distutils.misc_util import Configuration, get_numpy_include_dirs config = Configuration('rank', parent_package, top_path) # config.add_data_dir('tests') - cython(['_core8.pyx'], working_path=base_path) cython(['_core16.pyx'], working_path=base_path) cython(['_crank8.pyx'], working_path=base_path) @@ -21,18 +21,21 @@ def configuration(parent_package='', top_path=None): cython(['_crank16_bilateral.pyx'], working_path=base_path) config.add_extension('_core8', sources=['_core8.c'], - include_dirs=[get_numpy_include_dirs()]) + include_dirs=[get_numpy_include_dirs()]) config.add_extension('_core16', sources=['_core16.c'], - include_dirs=[get_numpy_include_dirs()]) + include_dirs=[get_numpy_include_dirs()]) config.add_extension('_crank8', sources=['_crank8.c'], - include_dirs=[get_numpy_include_dirs()]) - config.add_extension('_crank8_percentiles', sources=['_crank8_percentiles.c'], + include_dirs=[get_numpy_include_dirs()]) + config.add_extension( + '_crank8_percentiles', sources=['_crank8_percentiles.c'], include_dirs=[get_numpy_include_dirs()]) config.add_extension('_crank16', sources=['_crank16.c'], + include_dirs=[get_numpy_include_dirs()]) + config.add_extension( + '_crank16_percentiles', sources=['_crank16_percentiles.c'], include_dirs=[get_numpy_include_dirs()]) - config.add_extension('_crank16_percentiles', sources=['_crank16_percentiles.c'], - include_dirs=[get_numpy_include_dirs()]) - config.add_extension('_crank16_bilateral', sources=['_crank16_bilateral.c'], + config.add_extension( + '_crank16_bilateral', sources=['_crank16_bilateral.c'], include_dirs=[get_numpy_include_dirs()]) return config @@ -40,10 +43,10 @@ def configuration(parent_package='', top_path=None): if __name__ == '__main__': from numpy.distutils.core import setup setup(maintainer='scikits-image Developers', - author='Olivier Debeir', - maintainer_email='scikits-image@googlegroups.com', - description='Rank filters', - url='https://github.com/scikits-image/scikits-image', - license='SciPy License (BSD Style)', - **(configuration(top_path='').todict()) - ) + author='Olivier Debeir', + maintainer_email='scikits-image@googlegroups.com', + description='Rank filters', + url='https://github.com/scikits-image/scikits-image', + license='SciPy License (BSD Style)', + **(configuration(top_path='').todict()) + )