diff --git a/doc/examples/plot_16bitbilateral.py b/doc/examples/plot_16bitbilateral.py index fc30aa6b..473fcadd 100644 --- a/doc/examples/plot_16bitbilateral.py +++ b/doc/examples/plot_16bitbilateral.py @@ -27,21 +27,21 @@ from skimage import data from skimage.morphology import disk import skimage.filter.rank as rank -a16 = (data.coins()).astype('uint16')*16 +a16 = (data.coins()).astype('uint16') * 16 selem = disk(20) -f1 = rank.percentile_mean(a16,selem = selem,p0=.1,p1=.9) -f2 = rank.bilateral_mean(a16,selem = selem,s0=500,s1=500) -f3 = rank.mean(a16,selem = selem) +f1 = rank.percentile_mean(a16, selem=selem, p0=.1, p1=.9) +f2 = rank.bilateral_mean(a16, selem=selem, s0=500, s1=500) +f3 = rank.mean(a16, selem=selem) # display results -fig, axes = plt.subplots(nrows=3, figsize=(15,10)) +fig, axes = plt.subplots(nrows=3, figsize=(15, 10)) ax0, ax1, ax2 = axes -ax0.imshow(np.hstack((a16,f1))) +ax0.imshow(np.hstack((a16, f1))) ax0.set_title('percentile mean') -ax1.imshow(np.hstack((a16,f2))) +ax1.imshow(np.hstack((a16, f2))) ax1.set_title('bilateral mean') -ax2.imshow(np.hstack((a16,f3))) +ax2.imshow(np.hstack((a16, f3))) ax2.set_title('local mean') plt.show() diff --git a/doc/examples/plot_local_equalize.py b/doc/examples/plot_local_equalize.py index 1a431f5c..bc458505 100644 --- a/doc/examples/plot_local_equalize.py +++ b/doc/examples/plot_local_equalize.py @@ -24,6 +24,7 @@ import matplotlib.pyplot as plt import numpy as np from skimage.filter import rank + def plot_img_and_hist(img, axes, bins=256): """Plot an image along with its histogram and cumulative histogram. @@ -60,7 +61,7 @@ img_rescale = exposure.equalize(img) # Equalization selem = disk(30) -img_eq = rank.equalize(img,selem=selem) +img_eq = rank.equalize(img, selem=selem) # Display results @@ -81,4 +82,3 @@ ax_cdf.set_ylabel('Fraction of total intensity') # prevent overlap of y-axis labels plt.subplots_adjust(wspace=0.4) plt.show() - diff --git a/doc/examples/plot_local_otsu.py b/doc/examples/plot_local_otsu.py index ae1ead48..968ce6e1 100644 --- a/doc/examples/plot_local_otsu.py +++ b/doc/examples/plot_local_otsu.py @@ -26,24 +26,24 @@ p8 = data.page() radius = 10 selem = disk(radius) -loc_otsu = rank.otsu(p8,selem) +loc_otsu = rank.otsu(p8, selem) t_glob_otsu = threshold_otsu(p8) -glob_otsu = p8>=t_glob_otsu +glob_otsu = p8 >= t_glob_otsu plt.figure() -plt.subplot(2,2,1) -plt.imshow(p8,cmap=plt.cm.gray) +plt.subplot(2, 2, 1) +plt.imshow(p8, cmap=plt.cm.gray) plt.xlabel('original') plt.colorbar() -plt.subplot(2,2,2) -plt.imshow(loc_otsu,cmap=plt.cm.gray) -plt.xlabel('local Otsu ($radius=%d$)'%radius) +plt.subplot(2, 2, 2) +plt.imshow(loc_otsu, cmap=plt.cm.gray) +plt.xlabel('local Otsu ($radius=%d$)' % radius) plt.colorbar() -plt.subplot(2,2,3) -plt.imshow(p8>=loc_otsu,cmap=plt.cm.gray) -plt.xlabel('original>=local Otsu'%t_glob_otsu) -plt.subplot(2,2,4) -plt.imshow(glob_otsu,cmap=plt.cm.gray) -plt.xlabel('global Otsu ($t=%d$)'%t_glob_otsu) +plt.subplot(2, 2, 3) +plt.imshow(p8 >= loc_otsu, cmap=plt.cm.gray) +plt.xlabel('original>=local Otsu' % t_glob_otsu) +plt.subplot(2, 2, 4) +plt.imshow(glob_otsu, cmap=plt.cm.gray) +plt.xlabel('global Otsu ($t=%d$)' % t_glob_otsu) plt.show() diff --git a/doc/examples/plot_marked_watershed.py b/doc/examples/plot_marked_watershed.py index 738a3d24..e97280c7 100644 --- a/doc/examples/plot_marked_watershed.py +++ b/doc/examples/plot_marked_watershed.py @@ -16,7 +16,7 @@ See Wikipedia_ for more details on the algorithm. from scipy import ndimage import matplotlib.pyplot as plt -from skimage.morphology import watershed,disk +from skimage.morphology import watershed, disk from skimage import data # original data @@ -25,14 +25,14 @@ from skimage.filter import rank image = data.camera() # denoise image -denoised = rank.median(image,disk(2)) +denoised = rank.median(image, disk(2)) # find continuous region (low gradient) --> markers -markers = rank.gradient(denoised,disk(5))<10 +markers = rank.gradient(denoised, disk(5)) < 10 markers = ndimage.label(markers)[0] #local gradient -gradient = rank.gradient(denoised,disk(2)) +gradient = rank.gradient(denoised, disk(2)) # process the watershed labels = watershed(gradient, markers) @@ -45,7 +45,7 @@ ax0.imshow(image, cmap=plt.cm.gray, interpolation='nearest') ax1.imshow(gradient, cmap=plt.cm.spectral, interpolation='nearest') ax2.imshow(markers, cmap=plt.cm.spectral, interpolation='nearest') ax3.imshow(image, cmap=plt.cm.gray, interpolation='nearest') -ax3.imshow(labels, cmap=plt.cm.spectral, interpolation='nearest',alpha=.7) +ax3.imshow(labels, cmap=plt.cm.spectral, interpolation='nearest', alpha=.7) for ax in axes: ax.axis('off') diff --git a/skimage/filter/rank/_core16.pyx b/skimage/filter/rank/_core16.pyx index d3715f47..1575e24e 100644 --- a/skimage/filter/rank/_core16.pyx +++ b/skimage/filter/rank/_core16.pyx @@ -67,9 +67,9 @@ cdef void _core16(np.uint16_t kernel(Py_ssize_t *, float, np.uint16_t, assert (image < maxbin).all() # define pointers to the data - cdef np.uint16_t * out_data = out.data - cdef np.uint16_t * image_data = image.data - cdef np.uint8_t * mask_data = mask.data + cdef 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 @@ -83,19 +83,19 @@ cdef void _core16(np.uint16_t kernel(Py_ssize_t *, float, np.uint16_t, 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 @@ -144,7 +144,7 @@ cdef void _core16(np.uint16_t kernel(Py_ssize_t *, float, np.uint16_t, 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]) + histogram_increment(histo, & pop, image_data[rr * cols + cc]) r = 0 c = 0 @@ -162,13 +162,13 @@ cdef void _core16(np.uint16_t kernel(Py_ssize_t *, float, np.uint16_t, 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]) + 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]) + histogram_decrement(histo, & pop, image_data[rr * cols + cc]) # kernel ------------------------------------------- out_data[r * cols + c] = kernel( @@ -185,13 +185,13 @@ cdef void _core16(np.uint16_t kernel(Py_ssize_t *, float, np.uint16_t, 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]) + 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]) + histogram_decrement(histo, & pop, image_data[rr * cols + cc]) # kernel ------------------------------------------- out_data[r * cols + c] = kernel(histo, pop, image_data[r * cols + c], @@ -204,13 +204,13 @@ cdef void _core16(np.uint16_t kernel(Py_ssize_t *, float, np.uint16_t, 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]) + 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]) + histogram_decrement(histo, & pop, image_data[rr * cols + cc]) # kernel ------------------------------------------- out_data[r * cols + c] = kernel( @@ -227,13 +227,13 @@ cdef void _core16(np.uint16_t kernel(Py_ssize_t *, float, np.uint16_t, 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]) + 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]) + histogram_decrement(histo, & pop, image_data[rr * cols + cc]) # kernel ------------------------------------------- out_data[r * cols + c] = kernel(histo, pop, image_data[r * cols + c], diff --git a/skimage/filter/rank/_core8.pyx b/skimage/filter/rank/_core8.pyx index cfb011ac..6a9701a0 100644 --- a/skimage/filter/rank/_core8.pyx +++ b/skimage/filter/rank/_core8.pyx @@ -69,9 +69,9 @@ cdef void _core8(np.uint8_t kernel(Py_ssize_t *, float, np.uint8_t, float, # define pointers to the data - cdef np.uint8_t * out_data = out.data - cdef np.uint8_t * image_data = image.data - cdef np.uint8_t * mask_data = mask.data + cdef 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 @@ -86,19 +86,19 @@ cdef void _core8(np.uint8_t kernel(Py_ssize_t *, float, np.uint8_t, float, 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 @@ -148,7 +148,7 @@ cdef void _core8(np.uint8_t kernel(Py_ssize_t *, float, np.uint8_t, float, 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]) + histogram_increment(histo, & pop, image_data[rr * cols + cc]) r = 0 c = 0 @@ -166,13 +166,13 @@ cdef void _core8(np.uint8_t kernel(Py_ssize_t *, float, np.uint8_t, float, 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]) + 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]) + histogram_decrement(histo, & pop, image_data[rr * cols + cc]) # kernel ----------------------------------------------------------- out_data[r * cols + c] = \ @@ -188,13 +188,13 @@ cdef void _core8(np.uint8_t kernel(Py_ssize_t *, float, np.uint8_t, float, 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]) + 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]) + histogram_decrement(histo, & pop, image_data[rr * cols + cc]) # kernel --------------------------------------------------------------- out_data[r * cols + c] = kernel(histo, pop, image_data[r * cols + c], @@ -207,13 +207,13 @@ cdef void _core8(np.uint8_t kernel(Py_ssize_t *, float, np.uint8_t, float, 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]) + 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]) + histogram_decrement(histo, & pop, image_data[rr * cols + cc]) # kernel ----------------------------------------------------------- out_data[r * cols + c] = kernel( @@ -229,13 +229,13 @@ cdef void _core8(np.uint8_t kernel(Py_ssize_t *, float, np.uint8_t, float, 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]) + 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]) + histogram_decrement(histo, & pop, image_data[rr * cols + cc]) # kernel --------------------------------------------------------------- out_data[r * cols + c] = kernel(histo, pop, image_data[r * cols + c], diff --git a/skimage/filter/rank/_crank16.pyx b/skimage/filter/rank/_crank16.pyx index 67db0c69..3a9b5775 100644 --- a/skimage/filter/rank/_crank16.pyx +++ b/skimage/filter/rank/_crank16.pyx @@ -32,9 +32,9 @@ cdef inline np.uint16_t kernel_autolevel(Py_ssize_t * histo, float pop, break delta = imax - imin if delta > 0: - return (1. * (maxbin - 1) * (g - imin) / delta) + 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, @@ -49,9 +49,9 @@ cdef inline np.uint16_t kernel_bottomhat(Py_ssize_t * histo, float pop, if histo[i]: break - return (g - i) + return < np.uint16_t > (g - i) else: - return (0) + return < np.uint16_t > (0) cdef inline np.uint16_t kernel_equalize(Py_ssize_t * histo, float pop, np.uint16_t g, Py_ssize_t bitdepth, @@ -67,9 +67,9 @@ cdef inline np.uint16_t kernel_equalize(Py_ssize_t * histo, float pop, 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, @@ -88,9 +88,9 @@ cdef inline np.uint16_t kernel_gradient(Py_ssize_t * histo, float pop, 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, @@ -103,9 +103,9 @@ cdef inline np.uint16_t kernel_maximum(Py_ssize_t * histo, float pop, if pop: 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, @@ -119,9 +119,9 @@ cdef inline np.uint16_t kernel_mean(Py_ssize_t * histo, float pop, if pop: for i in range(maxbin): mean += histo[i] * i - return (mean / pop) + 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, @@ -138,9 +138,9 @@ cdef inline np.uint16_t kernel_meansubstraction(Py_ssize_t * histo, if pop: for i in range(maxbin): mean += histo[i] * i - return ((g - mean / pop) / 2. + (midbin - 1)) + 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, @@ -156,9 +156,9 @@ cdef inline np.uint16_t kernel_median(Py_ssize_t * histo, float pop, if histo[i]: sum -= histo[i] if sum < 0: - return (i) + return < np.uint16_t > (i) else: - return (0) + return < np.uint16_t > (0) cdef inline np.uint16_t kernel_minimum(Py_ssize_t * histo, float pop, @@ -171,9 +171,9 @@ cdef inline np.uint16_t kernel_minimum(Py_ssize_t * histo, float pop, if pop: for i in range(maxbin): if histo[i]: - return (i) + return < np.uint16_t > (i) else: - return (0) + return < np.uint16_t > (0) cdef inline np.uint16_t kernel_modal(Py_ssize_t * histo, float pop, @@ -188,9 +188,9 @@ cdef inline np.uint16_t kernel_modal(Py_ssize_t * histo, float pop, if histo[i] > hmax: hmax = histo[i] imax = i - return (imax) + return < np.uint16_t > (imax) else: - return (0) + return < np.uint16_t > (0) cdef inline np.uint16_t kernel_morph_contr_enh(Py_ssize_t * histo, @@ -213,11 +213,11 @@ cdef inline np.uint16_t kernel_morph_contr_enh(Py_ssize_t * histo, imin = i break if imax - g < g - imin: - return (imax) + 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, @@ -225,7 +225,7 @@ cdef inline np.uint16_t kernel_pop(Py_ssize_t * histo, float pop, Py_ssize_t maxbin, Py_ssize_t midbin, float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): - return (pop) + return < np.uint16_t > (pop) cdef inline np.uint16_t kernel_threshold(Py_ssize_t * histo, float pop, @@ -239,9 +239,9 @@ cdef inline np.uint16_t kernel_threshold(Py_ssize_t * histo, float pop, if pop: for i in range(maxbin): mean += histo[i] * i - return (g > (mean / pop)) + 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, @@ -256,9 +256,9 @@ cdef inline np.uint16_t kernel_tophat(Py_ssize_t * histo, float pop, if histo[i]: break - return (i - g) + return < np.uint16_t > (i - g) else: - return (0) + return < np.uint16_t > (0) cdef inline np.uint16_t kernel_entropy(Py_ssize_t * histo, float pop, np.uint16_t g, Py_ssize_t bitdepth, @@ -266,19 +266,19 @@ cdef inline np.uint16_t kernel_entropy(Py_ssize_t * histo, float pop, float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i - cdef float e,p + cdef float e, p if pop: e = 0. for i in range(maxbin): - p = histo[i]/pop - if p>0: - e -= p*log2(p) + p = histo[i] / pop + if p > 0: + e -= p * log2(p) - return e*1000 + return < np.uint16_t > e * 1000 else: - return (0) + return < np.uint16_t > (0) # ----------------------------------------------------------------- # python wrappers @@ -291,7 +291,7 @@ def autolevel(np.ndarray[np.uint16_t, ndim=2] image, np.ndarray[np.uint16_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_autolevel, image, selem, mask, out, shift_x, shift_y, - bitdepth, 0, 0, 0, 0) + bitdepth, 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def bottomhat(np.ndarray[np.uint16_t, ndim=2] image, @@ -300,7 +300,7 @@ def bottomhat(np.ndarray[np.uint16_t, ndim=2] image, np.ndarray[np.uint16_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_bottomhat, image, selem, mask, out, shift_x, shift_y, - bitdepth, 0, 0, 0, 0) + bitdepth, 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def equalize(np.ndarray[np.uint16_t, ndim=2] image, @@ -309,7 +309,7 @@ def equalize(np.ndarray[np.uint16_t, ndim=2] image, np.ndarray[np.uint16_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_equalize, image, selem, mask, out, shift_x, shift_y, - bitdepth, 0, 0, 0, 0) + bitdepth, 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def gradient(np.ndarray[np.uint16_t, ndim=2] image, @@ -318,7 +318,7 @@ def gradient(np.ndarray[np.uint16_t, ndim=2] image, np.ndarray[np.uint16_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_gradient, image, selem, mask, out, shift_x, shift_y, - bitdepth, 0, 0, 0, 0) + bitdepth, 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def maximum(np.ndarray[np.uint16_t, ndim=2] image, @@ -327,7 +327,7 @@ def maximum(np.ndarray[np.uint16_t, ndim=2] image, np.ndarray[np.uint16_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_maximum, image, selem, mask, out, shift_x, shift_y, - bitdepth, 0, 0, 0, 0) + bitdepth, 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def mean(np.ndarray[np.uint16_t, ndim=2] image, @@ -336,7 +336,7 @@ def mean(np.ndarray[np.uint16_t, ndim=2] image, np.ndarray[np.uint16_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_mean, image, selem, mask, out, shift_x, shift_y, - bitdepth, 0, 0, 0, 0) + bitdepth, 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def meansubstraction(np.ndarray[np.uint16_t, ndim=2] image, @@ -345,7 +345,7 @@ def meansubstraction(np.ndarray[np.uint16_t, ndim=2] image, np.ndarray[np.uint16_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_meansubstraction, image, selem, mask, out, shift_x, shift_y, - bitdepth, 0, 0, 0, 0) + bitdepth, 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def median(np.ndarray[np.uint16_t, ndim=2] image, @@ -354,7 +354,7 @@ def median(np.ndarray[np.uint16_t, ndim=2] image, np.ndarray[np.uint16_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_median, image, selem, mask, out, shift_x, shift_y, - bitdepth, 0, 0, 0, 0) + bitdepth, 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def minimum(np.ndarray[np.uint16_t, ndim=2] image, @@ -363,7 +363,7 @@ def minimum(np.ndarray[np.uint16_t, ndim=2] image, np.ndarray[np.uint16_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_minimum, image, selem, mask, out, shift_x, shift_y, - bitdepth, 0, 0, 0, 0) + bitdepth, 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def morph_contr_enh(np.ndarray[np.uint16_t, ndim=2] image, @@ -372,7 +372,7 @@ def morph_contr_enh(np.ndarray[np.uint16_t, ndim=2] image, np.ndarray[np.uint16_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_morph_contr_enh, image, selem, mask, out, shift_x, shift_y, - bitdepth, 0, 0, 0, 0) + bitdepth, 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def modal(np.ndarray[np.uint16_t, ndim=2] image, @@ -381,7 +381,7 @@ def modal(np.ndarray[np.uint16_t, ndim=2] image, np.ndarray[np.uint16_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_modal, image, selem, mask, out, shift_x, shift_y, - bitdepth, 0, 0, 0, 0) + bitdepth, 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def pop(np.ndarray[np.uint16_t, ndim=2] image, @@ -390,7 +390,7 @@ def pop(np.ndarray[np.uint16_t, ndim=2] image, np.ndarray[np.uint16_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_pop, image, selem, mask, out, shift_x, shift_y, - bitdepth, 0, 0, 0, 0) + bitdepth, 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def threshold(np.ndarray[np.uint16_t, ndim=2] image, @@ -399,7 +399,7 @@ def threshold(np.ndarray[np.uint16_t, ndim=2] image, np.ndarray[np.uint16_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_threshold, image, selem, mask, out, shift_x, shift_y, - bitdepth, 0, 0, 0, 0) + bitdepth, 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def tophat(np.ndarray[np.uint16_t, ndim=2] image, @@ -408,12 +408,13 @@ def tophat(np.ndarray[np.uint16_t, ndim=2] image, np.ndarray[np.uint16_t, ndim=2] out=None, char shift_x=0, char shift_y=0, Py_ssize_t bitdepth=8): _core16(kernel_tophat, image, selem, mask, out, shift_x, shift_y, - bitdepth, 0, 0, 0, 0) + bitdepth, 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) + def entropy(np.ndarray[np.uint16_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint16_t, ndim=2] out=None, - 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): _core16(kernel_entropy, image, selem, mask, out, shift_x, shift_y, - bitdepth, 0, 0, 0, 0) + bitdepth, 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) diff --git a/skimage/filter/rank/_crank16_bilateral.pyx b/skimage/filter/rank/_crank16_bilateral.pyx index 0174b926..c71ccc5c 100644 --- a/skimage/filter/rank/_crank16_bilateral.pyx +++ b/skimage/filter/rank/_crank16_bilateral.pyx @@ -28,11 +28,11 @@ cdef inline np.uint16_t kernel_mean(Py_ssize_t * histo, float pop, bilat_pop += histo[i] mean += histo[i] * i if bilat_pop: - return (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, @@ -47,9 +47,9 @@ cdef inline np.uint16_t kernel_pop(Py_ssize_t * histo, float pop, for i in range(maxbin): if (g > (i - s0)) and (g < (i + s1)): bilat_pop += histo[i] - return (bilat_pop) + return < np.uint16_t > (bilat_pop) else: - return (0) + return < np.uint16_t > (0) # ----------------------------------------------------------------- diff --git a/skimage/filter/rank/_crank16_percentiles.pyx b/skimage/filter/rank/_crank16_percentiles.pyx index 8b45d46a..72d48e53 100644 --- a/skimage/filter/rank/_crank16_percentiles.pyx +++ b/skimage/filter/rank/_crank16_percentiles.pyx @@ -38,12 +38,12 @@ cdef inline np.uint16_t kernel_autolevel(Py_ssize_t * histo, float pop, delta = imax - imin if delta > 0: - return (1.0 * (maxbin - 1) \ - * (int_min(int_max(imin, g), imax) - imin) / delta) + 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, @@ -69,9 +69,9 @@ cdef inline np.uint16_t kernel_gradient(Py_ssize_t * histo, float 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, @@ -93,11 +93,11 @@ cdef inline np.uint16_t kernel_mean(Py_ssize_t * histo, float pop, mean += histo[i] * i if n > 0: - return (1.0 * mean / n) + 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, @@ -121,11 +121,11 @@ cdef inline np.uint16_t kernel_mean_substraction(Py_ssize_t * histo, n += histo[i] mean += histo[i] * i if n > 0: - return ((g - (mean / n)) * .5 + midbin) + 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, @@ -154,15 +154,15 @@ cdef inline np.uint16_t kernel_morph_contr_enh(Py_ssize_t * histo, imax = i break if g > imax: - return imax + return < np.uint16_t > imax if g < imin: - return imin + return < np.uint16_t > imin if imax - g < g - imin: - return imax + 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, @@ -180,9 +180,9 @@ cdef inline np.uint16_t kernel_percentile(Py_ssize_t * histo, float 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, @@ -200,9 +200,9 @@ cdef inline np.uint16_t kernel_pop(Py_ssize_t * histo, float pop, sum += histo[i] if (sum >= p0 * pop) and (sum <= p1 * pop): n += histo[i] - return (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, @@ -220,9 +220,9 @@ cdef inline np.uint16_t kernel_threshold(Py_ssize_t * histo, float 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) # ----------------------------------------------------------------- @@ -239,7 +239,7 @@ def autolevel(np.ndarray[np.uint16_t, ndim=2] image, """bottom hat """ _core16(kernel_autolevel, image, selem, mask, out, shift_x, shift_y, - bitdepth, p0, p1, 0, 0) + bitdepth, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 0) def gradient(np.ndarray[np.uint16_t, ndim=2] image, @@ -251,7 +251,7 @@ def gradient(np.ndarray[np.uint16_t, ndim=2] image, """return p0,p1 percentile gradient """ _core16(kernel_gradient, image, selem, mask, out, shift_x, shift_y, - bitdepth, p0, p1, 0, 0) + bitdepth, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 0) def mean(np.ndarray[np.uint16_t, ndim=2] image, @@ -263,7 +263,7 @@ def mean(np.ndarray[np.uint16_t, ndim=2] image, """return mean between [p0 and p1] percentiles """ _core16(kernel_mean, image, selem, mask, out, shift_x, shift_y, - bitdepth, p0, p1, 0, 0) + bitdepth, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 0) def mean_substraction(np.ndarray[np.uint16_t, ndim=2] image, @@ -274,8 +274,9 @@ def mean_substraction(np.ndarray[np.uint16_t, ndim=2] image, float p0=0., float p1=0.): """return original - mean between [p0 and p1] percentiles *.5 +127 """ - _core16(kernel_mean_substraction, image, selem, mask, out, shift_x, shift_y, - bitdepth, p0, p1, 0, 0) + _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, @@ -287,7 +288,7 @@ def morph_contr_enh(np.ndarray[np.uint16_t, ndim=2] image, """reforce contrast using percentiles """ _core16(kernel_morph_contr_enh, image, selem, mask, out, shift_x, shift_y, - bitdepth, p0, p1, 0, 0) + bitdepth, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 0) def percentile(np.ndarray[np.uint16_t, ndim=2] image, @@ -299,7 +300,7 @@ def percentile(np.ndarray[np.uint16_t, ndim=2] image, """return p0 percentile """ _core16(kernel_percentile, image, selem, mask, out, shift_x, shift_y, - bitdepth, p0, p1, 0, 0) + bitdepth, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 0) def pop(np.ndarray[np.uint16_t, ndim=2] image, @@ -311,7 +312,7 @@ def pop(np.ndarray[np.uint16_t, ndim=2] image, """return nb of pixels between [p0 and p1] """ _core16(kernel_pop, image, selem, mask, out, shift_x, shift_y, - bitdepth, p0, p1, 0, 0) + bitdepth, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 0) def threshold(np.ndarray[np.uint16_t, ndim=2] image, @@ -323,4 +324,4 @@ def threshold(np.ndarray[np.uint16_t, ndim=2] image, """return (maxbin-1) if g > percentile p0 """ _core16(kernel_threshold, image, selem, mask, out, shift_x, shift_y, - bitdepth, p0, p1, 0, 0) + bitdepth, p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 0) diff --git a/skimage/filter/rank/_crank8.pyx b/skimage/filter/rank/_crank8.pyx index 9b69296b..4a25e974 100644 --- a/skimage/filter/rank/_crank8.pyx +++ b/skimage/filter/rank/_crank8.pyx @@ -31,11 +31,11 @@ cdef inline np.uint8_t kernel_autolevel(Py_ssize_t * histo, float pop, break delta = imax - imin if delta > 0: - return (255. * (g - imin) / delta) + 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, @@ -49,9 +49,9 @@ cdef inline np.uint8_t kernel_bottomhat(Py_ssize_t * histo, float pop, if histo[i]: break - return (g - i) + return < np.uint8_t > (g - i) else: - return (0) + return < np.uint8_t > (0) cdef inline np.uint8_t kernel_equalize(Py_ssize_t * histo, float pop, @@ -67,9 +67,9 @@ cdef inline np.uint8_t kernel_equalize(Py_ssize_t * histo, float pop, if i >= g: break - return ((255 * sum) / pop) + return < np.uint8_t > ((255 * sum) / pop) else: - return (0) + return < np.uint8_t > (0) cdef inline np.uint8_t kernel_gradient(Py_ssize_t * histo, float pop, @@ -87,9 +87,9 @@ cdef inline np.uint8_t kernel_gradient(Py_ssize_t * histo, float pop, 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, @@ -101,9 +101,9 @@ cdef inline np.uint8_t kernel_maximum(Py_ssize_t * histo, float pop, if pop: for i in range(255, -1, -1): if histo[i]: - return (i) + return < np.uint8_t > (i) else: - return (0) + return < np.uint8_t > (0) cdef inline np.uint8_t kernel_mean(Py_ssize_t * histo, float pop, @@ -116,9 +116,9 @@ cdef inline np.uint8_t kernel_mean(Py_ssize_t * histo, float pop, if pop: for i in range(256): mean += histo[i] * i - return (mean / pop) + 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, @@ -131,9 +131,9 @@ cdef inline np.uint8_t kernel_meansubstraction(Py_ssize_t * histo, float pop, if pop: for i in range(256): mean += histo[i] * i - return ((g - mean / pop) / 2. + 127) + 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, @@ -148,9 +148,9 @@ cdef inline np.uint8_t kernel_median(Py_ssize_t * histo, float pop, if histo[i]: sum -= histo[i] if sum < 0: - return (i) + return < np.uint8_t > (i) else: - return (0) + return < np.uint8_t > (0) cdef inline np.uint8_t kernel_minimum(Py_ssize_t * histo, float pop, @@ -162,9 +162,9 @@ cdef inline np.uint8_t kernel_minimum(Py_ssize_t * histo, float pop, if pop: for i in range(256): if histo[i]: - return (i) + return < np.uint8_t > (i) else: - return (0) + return < np.uint8_t > (0) cdef inline np.uint8_t kernel_modal(Py_ssize_t * histo, float pop, @@ -178,9 +178,9 @@ cdef inline np.uint8_t kernel_modal(Py_ssize_t * histo, float pop, if histo[i] > hmax: hmax = histo[i] imax = i - return (imax) + return < np.uint8_t > (imax) else: - return (0) + return < np.uint8_t > (0) cdef inline np.uint8_t kernel_morph_contr_enh(Py_ssize_t * histo, float pop, @@ -199,18 +199,18 @@ cdef inline np.uint8_t kernel_morph_contr_enh(Py_ssize_t * histo, float pop, imin = i break if imax - g < g - imin: - return (imax) + 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) + return < np.uint8_t > (pop) cdef inline np.uint8_t kernel_threshold(Py_ssize_t * histo, float pop, @@ -223,9 +223,9 @@ cdef inline np.uint8_t kernel_threshold(Py_ssize_t * histo, float pop, if pop: for i in range(256): mean += histo[i] * i - return (g > (mean / pop)) + 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, @@ -239,9 +239,9 @@ cdef inline np.uint8_t kernel_tophat(Py_ssize_t * histo, float pop, if histo[i]: break - return (i - g) + return < np.uint8_t > (i - g) else: - return (0) + return < np.uint8_t > (0) cdef inline np.uint8_t kernel_noise_filter(Py_ssize_t * histo, float pop, np.uint8_t g, float p0, float p1, @@ -252,7 +252,7 @@ cdef inline np.uint8_t kernel_noise_filter(Py_ssize_t * histo, float pop, # early stop if at least one pixel of the neighborhood has the same g if histo[g] > 0: - return 0 + return < np.uint8_t > 0 for i in range(g, -1, -1): if histo[i]: @@ -262,16 +262,16 @@ cdef inline np.uint8_t kernel_noise_filter(Py_ssize_t * histo, float pop, if histo[i]: break if i - g < min_i: - return (i - g) + return < np.uint8_t > (i - g) else: - return min_i + return < np.uint8_t > min_i cdef inline np.uint8_t kernel_entropy(Py_ssize_t * histo, float pop, np.uint8_t g, float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i - cdef float e,p + cdef float e, p if pop: e = 0. @@ -281,16 +281,16 @@ cdef inline np.uint8_t kernel_entropy(Py_ssize_t * histo, float pop, if p > 0: e -= p * log2(p) - return e*10 + return < np.uint8_t > e * 10 else: - return (0) + return < np.uint8_t > (0) cdef inline np.uint8_t kernel_otsu(Py_ssize_t * histo, float pop, np.uint8_t g, float p0, float p1, Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i cdef Py_ssize_t max_i - cdef float P, mu1, mu2, q1,new_q1, sigma_b, max_sigma_b + cdef float P, mu1, mu2, q1, new_q1, sigma_b, max_sigma_b cdef float mu = 0. # compute local mean @@ -299,27 +299,27 @@ cdef inline np.uint8_t kernel_otsu(Py_ssize_t * histo, float pop, np.uint8_t g, mu += histo[i] * i mu = (mu / pop) else: - return (0) + return < np.uint8_t > (0) # maximizing the between class variance max_i = 0 - q1 = histo[0]/pop + q1 = histo[0] / pop m1 = 0. max_sigma_b = 0. - for i in range(1,256): + for i in range(1, 256): P = histo[i] / pop new_q1 = q1 + P if new_q1 > 0: mu1 = (q1 * mu1 + i * P) / new_q1 - mu2 = (mu - new_q1*mu1) / (1. - new_q1) - sigma_b = new_q1 * (1. - new_q1) * (mu1 - mu2)**2 + mu2 = (mu - new_q1 * mu1) / (1. - new_q1) + sigma_b = new_q1 * (1. - new_q1) * (mu1 - mu2) ** 2 if sigma_b > max_sigma_b: max_sigma_b = sigma_b max_i = i q1 = new_q1 - return max_i + return < np.uint8_t > max_i # ----------------------------------------------------------------- @@ -334,7 +334,7 @@ def autolevel(np.ndarray[np.uint8_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_autolevel, image, selem, mask, out, shift_x, shift_y, - 0, 0, 0, 0) + 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def bottomhat(np.ndarray[np.uint8_t, ndim=2] image, @@ -343,7 +343,7 @@ def bottomhat(np.ndarray[np.uint8_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_bottomhat, image, selem, mask, out, shift_x, shift_y, - 0, 0, 0, 0) + 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def equalize(np.ndarray[np.uint8_t, ndim=2] image, @@ -352,7 +352,7 @@ def equalize(np.ndarray[np.uint8_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_equalize, image, selem, mask, out, shift_x, shift_y, - 0, 0, 0, 0) + 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def gradient(np.ndarray[np.uint8_t, ndim=2] image, @@ -361,7 +361,7 @@ def gradient(np.ndarray[np.uint8_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_gradient, image, selem, mask, out, shift_x, shift_y, - 0, 0, 0, 0) + 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def maximum(np.ndarray[np.uint8_t, ndim=2] image, @@ -370,7 +370,7 @@ def maximum(np.ndarray[np.uint8_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_maximum, image, selem, mask, out, shift_x, shift_y, - 0, 0, 0, 0) + 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def mean(np.ndarray[np.uint8_t, ndim=2] image, @@ -379,7 +379,7 @@ def mean(np.ndarray[np.uint8_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_mean, image, selem, mask, out, shift_x, shift_y, - 0, 0, 0, 0) + 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def meansubstraction(np.ndarray[np.uint8_t, ndim=2] image, @@ -388,7 +388,7 @@ def meansubstraction(np.ndarray[np.uint8_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_meansubstraction, image, selem, mask, out, shift_x, shift_y, - 0, 0, 0, 0) + 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def median(np.ndarray[np.uint8_t, ndim=2] image, @@ -397,7 +397,7 @@ def median(np.ndarray[np.uint8_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_median, image, selem, mask, out, shift_x, shift_y, - 0, 0, 0, 0) + 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def minimum(np.ndarray[np.uint8_t, ndim=2] image, @@ -406,7 +406,7 @@ def minimum(np.ndarray[np.uint8_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_minimum, image, selem, mask, out, shift_x, shift_y, - 0, 0, 0, 0) + 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def morph_contr_enh(np.ndarray[np.uint8_t, ndim=2] image, @@ -415,7 +415,7 @@ def morph_contr_enh(np.ndarray[np.uint8_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_morph_contr_enh, image, selem, mask, out, shift_x, shift_y, - 0, 0, 0, 0) + 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def modal(np.ndarray[np.uint8_t, ndim=2] image, @@ -424,7 +424,7 @@ def modal(np.ndarray[np.uint8_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_modal, image, selem, mask, out, shift_x, shift_y, - 0, 0, 0, 0) + 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def pop(np.ndarray[np.uint8_t, ndim=2] image, @@ -433,7 +433,7 @@ def pop(np.ndarray[np.uint8_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_pop, image, selem, mask, out, shift_x, shift_y, - 0, 0, 0, 0) + 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def threshold(np.ndarray[np.uint8_t, ndim=2] image, @@ -442,7 +442,7 @@ def threshold(np.ndarray[np.uint8_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_threshold, image, selem, mask, out, shift_x, shift_y, 0, 0, - 0, 0) + < Py_ssize_t > 0, < Py_ssize_t > 0) def tophat(np.ndarray[np.uint8_t, ndim=2] image, @@ -451,29 +451,31 @@ def tophat(np.ndarray[np.uint8_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] out=None, char shift_x=0, char shift_y=0): _core8(kernel_tophat, image, selem, mask, out, shift_x, shift_y, - 0, 0, 0, 0) + 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) def noise_filter(np.ndarray[np.uint8_t, ndim=2] image, - np.ndarray[np.uint8_t, ndim=2] selem, - np.ndarray[np.uint8_t, ndim=2] mask=None, - np.ndarray[np.uint8_t, ndim=2] out=None, - char shift_x=0, char shift_y=0): - _core8(kernel_noise_filter, image, selem, mask, out, shift_x, shift_y, - 0, 0, 0, 0) - -def entropy(np.ndarray[np.uint8_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] selem, np.ndarray[np.uint8_t, ndim=2] mask=None, np.ndarray[np.uint8_t, ndim=2] out=None, char shift_x=0, char shift_y=0): - _core8(kernel_entropy, image, selem, mask, out, shift_x, shift_y, - 0, 0, 0, 0) + _core8(kernel_noise_filter, image, selem, mask, out, shift_x, shift_y, + 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) -def otsu(np.ndarray[np.uint8_t, ndim=2] image, + +def entropy(np.ndarray[np.uint8_t, ndim=2] image, np.ndarray[np.uint8_t, ndim=2] selem, np.ndarray[np.uint8_t, ndim=2] mask=None, np.ndarray[np.uint8_t, ndim=2] out=None, char shift_x=0, char shift_y=0): + _core8(kernel_entropy, image, selem, mask, out, shift_x, shift_y, + 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) + + +def otsu(np.ndarray[np.uint8_t, ndim=2] image, + np.ndarray[np.uint8_t, ndim=2] selem, + np.ndarray[np.uint8_t, ndim=2] mask=None, + np.ndarray[np.uint8_t, ndim=2] out=None, + char shift_x=0, char shift_y=0): _core8(kernel_otsu, image, selem, mask, out, shift_x, shift_y, - 0, 0, 0, 0) + 0, 0, < Py_ssize_t > 0, < Py_ssize_t > 0) diff --git a/skimage/filter/rank/_crank8_percentiles.pyx b/skimage/filter/rank/_crank8_percentiles.pyx index d5072c81..ddaec290 100644 --- a/skimage/filter/rank/_crank8_percentiles.pyx +++ b/skimage/filter/rank/_crank8_percentiles.pyx @@ -37,12 +37,12 @@ cdef inline np.uint8_t kernel_autolevel(Py_ssize_t * histo, float pop, break delta = imax - imin if delta > 0: - return (255 \ - * (uint8_min(uint8_max(imin, g), imax) - imin) / delta) + 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, @@ -65,9 +65,9 @@ cdef inline np.uint8_t kernel_gradient(Py_ssize_t * histo, float 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, @@ -85,11 +85,11 @@ cdef inline np.uint8_t kernel_mean(Py_ssize_t * histo, float pop, n += histo[i] mean += histo[i] * i if n > 0: - return (1.0 * mean / n) + 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, @@ -109,11 +109,11 @@ cdef inline np.uint8_t kernel_mean_substraction(Py_ssize_t * histo, n += histo[i] mean += histo[i] * i if n > 0: - return ((g - (mean / n)) * .5 + 127) + 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, @@ -137,15 +137,15 @@ cdef inline np.uint8_t kernel_morph_contr_enh(Py_ssize_t * histo, imax = i break if g > imax: - return imax + return < np.uint8_t > imax if g < imin: - return imin + return < np.uint8_t > imin if imax - g < g - imin: - return imax + 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, @@ -160,9 +160,9 @@ cdef inline np.uint8_t kernel_percentile(Py_ssize_t * histo, float 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, @@ -177,9 +177,9 @@ cdef inline np.uint8_t kernel_pop(Py_ssize_t * histo, float pop, sum += histo[i] if (sum >= p0 * pop) and (sum <= p1 * pop): n += histo[i] - return (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, @@ -194,9 +194,9 @@ cdef inline np.uint8_t kernel_threshold(Py_ssize_t * histo, float 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) # ----------------------------------------------------------------- @@ -212,7 +212,7 @@ def autolevel(np.ndarray[np.uint8_t, ndim=2] image, """autolevel """ _core8(kernel_autolevel, image, selem, mask, out, shift_x, shift_y, p0, p1, - 0, 0) + < Py_ssize_t > 0, < Py_ssize_t > 0) def gradient(np.ndarray[np.uint8_t, ndim=2] image, @@ -223,7 +223,7 @@ def gradient(np.ndarray[np.uint8_t, ndim=2] image, """return p0,p1 percentile gradient """ _core8(kernel_gradient, image, selem, mask, out, shift_x, shift_y, p0, p1, - 0, 0) + < Py_ssize_t > 0, < Py_ssize_t > 0) def mean(np.ndarray[np.uint8_t, ndim=2] image, @@ -234,7 +234,7 @@ def mean(np.ndarray[np.uint8_t, ndim=2] image, """return mean between [p0 and p1] percentiles """ _core8(kernel_mean, image, selem, mask, out, shift_x, shift_y, p0, p1, - 0, 0) + < Py_ssize_t > 0, < Py_ssize_t > 0) def mean_substraction(np.ndarray[np.uint8_t, ndim=2] image, @@ -245,7 +245,7 @@ def mean_substraction(np.ndarray[np.uint8_t, ndim=2] image, """return original - mean between [p0 and p1] percentiles *.5 +127 """ _core8(kernel_mean_substraction, image, selem, mask, out, shift_x, shift_y, - p0, p1, 0, 0) + p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 0) def morph_contr_enh(np.ndarray[np.uint8_t, ndim=2] image, @@ -256,7 +256,7 @@ def morph_contr_enh(np.ndarray[np.uint8_t, ndim=2] image, """reforce contrast using percentiles """ _core8(kernel_morph_contr_enh, image, selem, mask, out, shift_x, shift_y, - p0, p1, 0, 0) + p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 0) def percentile(np.ndarray[np.uint8_t, ndim=2] image, @@ -267,7 +267,7 @@ def percentile(np.ndarray[np.uint8_t, ndim=2] image, """return p0 percentile """ _core8(kernel_percentile, image, selem, mask, out, shift_x, shift_y, - p0, p1, 0, 0) + p0, p1, < Py_ssize_t > 0, < Py_ssize_t > 0) def pop(np.ndarray[np.uint8_t, ndim=2] image, @@ -278,7 +278,7 @@ def pop(np.ndarray[np.uint8_t, ndim=2] image, """return nb of pixels between [p0 and p1] """ _core8(kernel_pop, image, selem, mask, out, shift_x, shift_y, p0, p1, - 0, 0) + < Py_ssize_t > 0, < Py_ssize_t > 0) def threshold(np.ndarray[np.uint8_t, ndim=2] image, @@ -289,4 +289,4 @@ def threshold(np.ndarray[np.uint8_t, ndim=2] image, """return 255 if g > percentile p0 """ _core8(kernel_threshold, image, selem, mask, out, shift_x, shift_y, p0, p1, - 0, 0) + < Py_ssize_t > 0, < Py_ssize_t > 0) diff --git a/skimage/filter/rank/percentile_rank.pyx b/skimage/filter/rank/percentile_rank.pyx index 5ad94af4..eee83a74 100644 --- a/skimage/filter/rank/percentile_rank.pyx +++ b/skimage/filter/rank/percentile_rank.pyx @@ -102,9 +102,10 @@ def percentile_autolevel(image, selem, out=None, mask=None, shift_x=False, """ - 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) + 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, @@ -228,8 +229,9 @@ def percentile_mean_substraction(image, selem, out=None, mask=None, 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.): +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. morph_contr_enh is computed on the given structuring element. Only levels @@ -385,6 +387,7 @@ def percentile_threshold(image, selem, out=None, mask=None, shift_x=False, """ - 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) + 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) diff --git a/skimage/filter/rank/rank.pyx b/skimage/filter/rank/rank.pyx index da2f35bc..be078ddd 100644 --- a/skimage/filter/rank/rank.pyx +++ b/skimage/filter/rank/rank.pyx @@ -21,7 +21,7 @@ from skimage.filter.rank.generic import find_bitdepth __all__ = ['autolevel', 'bottomhat', 'equalize', 'gradient', 'maximum', 'mean', 'meansubstraction', 'median', 'minimum', 'modal', 'morph_contr_enh', - 'pop', 'threshold', 'tophat','noise_filter','entropy','otsu'] + 'pop', 'threshold', 'tophat', 'noise_filter', 'entropy', 'otsu'] def _apply(func8, func16, image, selem, out, mask, shift_x, shift_y): @@ -619,6 +619,7 @@ def tophat(image, selem, out=None, mask=None, shift_x=False, shift_y=False): return _apply(_crank8.tophat, _crank16.tophat, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y) + def noise_filter(image, selem, out=None, mask=None, shift_x=False, shift_y=False): """Returns the noise feature as described in [Hashimoto12]_ @@ -658,11 +659,12 @@ def noise_filter(image, selem, out=None, mask=None, shift_x=False, centre_c = int(selem.shape[1] / 2) + shift_x # make a local copy selem_cpy = selem.copy() - selem_cpy[centre_r,centre_c] = 0 + selem_cpy[centre_r, centre_c] = 0 return _apply(_crank8.noise_filter, None, image, selem_cpy, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y) + def entropy(image, selem, out=None, mask=None, shift_x=False, shift_y=False): """Returns the entropy [wiki_entropy]_ computed locally. Entropy is computed using base 2 logarithm i.e. the filter returns the minimum number of @@ -714,6 +716,7 @@ def entropy(image, selem, out=None, mask=None, shift_x=False, shift_y=False): return _apply(_crank8.entropy, _crank16.entropy, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y) + def otsu(image, selem, out=None, mask=None, shift_x=False, shift_y=False): """Returns the Otsu's threshold value for each pixel. diff --git a/skimage/filter/rank/tests/test_rank.py b/skimage/filter/rank/tests/test_rank.py index d8bcba52..c5d42f51 100644 --- a/skimage/filter/rank/tests/test_rank.py +++ b/skimage/filter/rank/tests/test_rank.py @@ -171,7 +171,7 @@ def test_compare_autolevels(): assert_array_equal(loc_autolevel, loc_perc_autolevel) -def test_compare_autolevels_16-bit(): +def test_compare_autolevels_16bit(): # compare autolevel(16-bit) and percentile autolevel(16-bit) with p0=0.0 and # p1=1.0 should returns the same arrays @@ -185,7 +185,7 @@ def test_compare_autolevels_16-bit(): assert_array_equal(loc_autolevel, loc_perc_autolevel) -def test_compare_8-bit_vs_16-bit(): +def test_compare_8bit_vs_16bit(): # filters applied on 8-bit image ore 16-bit image (having only real 8-bit of # dynamic) should be identical