From ed0ae50f6292aa12f3bbf7463661675ac9c701ce Mon Sep 17 00:00:00 2001 From: Olivier Debeir Date: Fri, 6 Dec 2013 09:05:55 +0100 Subject: [PATCH 01/12] add rank filter sum --- skimage/filter/rank/__init__.py | 3 +- skimage/filter/rank/generic.py | 46 ++++++++++++++++++++++++++ skimage/filter/rank/generic_cy.pyx | 24 ++++++++++++++ skimage/filter/rank/tests/test_rank.py | 35 ++++++++++++++++++++ 4 files changed, 107 insertions(+), 1 deletion(-) diff --git a/skimage/filter/rank/__init__.py b/skimage/filter/rank/__init__.py index 04a4b854..361e400e 100644 --- a/skimage/filter/rank/__init__.py +++ b/skimage/filter/rank/__init__.py @@ -1,6 +1,6 @@ from .generic import (autolevel, bottomhat, equalize, gradient, maximum, mean, subtract_mean, median, minimum, modal, enhance_contrast, - pop, threshold, tophat, noise_filter, entropy, otsu) + pop, threshold, tophat, noise_filter, entropy, otsu, sum) from ._percentile import (autolevel_percentile, gradient_percentile, mean_percentile, subtract_mean_percentile, enhance_contrast_percentile, percentile, @@ -51,6 +51,7 @@ __all__ = ['autolevel', 'pop', 'pop_percentile', 'pop_bilateral', + 'sum', 'threshold', 'threshold_percentile', 'tophat', diff --git a/skimage/filter/rank/generic.py b/skimage/filter/rank/generic.py index 34c0787b..29f243d1 100644 --- a/skimage/filter/rank/generic.py +++ b/skimage/filter/rank/generic.py @@ -528,6 +528,52 @@ def pop(image, selem, out=None, mask=None, shift_x=False, shift_y=False): return _apply(generic_cy._pop, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y) +def sum(image, selem, out=None, mask=None, shift_x=False, shift_y=False): + """Return the sum of pixels inside the neighborhood. If sum does not fit the data type,folding is possible. + + Parameters + ---------- + image : ndarray (uint8, uint16) + Image array. + selem : ndarray + The neighborhood expressed as a 2-D array of 1's and 0's. + out : ndarray (same dtype as input) + If None, a new array will be allocated. + mask : ndarray + Mask array that defines (>0) area of the image included in the local + neighborhood. If None, the complete image is used (default). + shift_x, shift_y : int + Offset added to the structuring element center point. Shift is bounded + to the structuring element sizes (center must be inside the given + structuring element). + + Returns + ------- + out : ndarray (same dtype as input image) + Output image. + + Examples + -------- + >>> # Local mean + >>> from skimage.morphology import square + >>> import skimage.filter.rank as rank + >>> ima = 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) + >>> rank.pop(ima, square(3)) + array([[1, 2, 3, 2, 1], + [2, 4, 6, 4, 2], + [3, 6, 9, 6, 3], + [2, 4, 6, 4, 2], + [1, 2, 3, 2, 1]], dtype=uint8) + + """ + + return _apply(generic_cy._sum, 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. diff --git a/skimage/filter/rank/generic_cy.pyx b/skimage/filter/rank/generic_cy.pyx index dcf6e361..2953b2f0 100644 --- a/skimage/filter/rank/generic_cy.pyx +++ b/skimage/filter/rank/generic_cy.pyx @@ -221,6 +221,21 @@ cdef inline double _kernel_pop(Py_ssize_t* histo, double pop, dtype_t g, return pop +cdef inline double _kernel_sum(Py_ssize_t* histo, double pop,dtype_t g, + Py_ssize_t max_bin, Py_ssize_t mid_bin, + double p0, double p1, + Py_ssize_t s0, Py_ssize_t s1): + + cdef Py_ssize_t i + cdef Py_ssize_t sum = 0 + + if pop: + for i in range(max_bin): + sum += histo[i] * i + return sum + else: + return 0 + cdef inline double _kernel_threshold(Py_ssize_t* histo, double pop, dtype_t g, Py_ssize_t max_bin, Py_ssize_t mid_bin, @@ -455,6 +470,15 @@ def _pop(dtype_t[:, ::1] image, _core(_kernel_pop[dtype_t], image, selem, mask, out, shift_x, shift_y, 0, 0, 0, 0, max_bin) +def _sum(dtype_t[:, ::1] image, + char[:, ::1] selem, + char[:, ::1] mask, + dtype_t_out[:, ::1] out, + char shift_x, char shift_y, Py_ssize_t max_bin): + + _core(_kernel_sum[dtype_t], image, selem, mask, + out, shift_x, shift_y, 0, 0, 0, 0, max_bin) + def _threshold(dtype_t[:, ::1] image, char[:, ::1] selem, diff --git a/skimage/filter/rank/tests/test_rank.py b/skimage/filter/rank/tests/test_rank.py index cedeb92d..26aa43ed 100644 --- a/skimage/filter/rank/tests/test_rank.py +++ b/skimage/filter/rank/tests/test_rank.py @@ -498,6 +498,41 @@ def test_percentile_median(): img_max = rank.median(img16, selem=selem) assert_array_equal(img_p0, img_max) +def test_sum(): + # check the number of valid pixels in the neighborhood + + image8 = 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) + image16 = 400*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.uint16) + elem = np.ones((3, 3), dtype=np.uint8) + out8 = np.empty_like(image8) + out16 = np.empty_like(image16) + mask = np.ones(image8.shape, dtype=np.uint8) + + rank.sum(image=image8, selem=elem, out=out8, mask=mask) + r = np.array([[1, 2, 3, 2, 1], + [2, 4, 6, 4, 2], + [3, 6, 9, 6, 3], + [2, 4, 6, 4, 2], + [1, 2, 3, 2, 1]], dtype=np.uint8) + assert_array_equal(r, out8) + + rank.sum(image=image16, selem=elem, out=out16, mask=mask) + r = 400* np.array([[1, 2, 3, 2, 1], + [2, 4, 6, 4, 2], + [3, 6, 9, 6, 3], + [2, 4, 6, 4, 2], + [1, 2, 3, 2, 1]], dtype=np.uint16) + print image16 + assert_array_equal(r, out16) + if __name__ == "__main__": run_module_suite() From fe0e252db67406ba495e844bab038b71e6e1c683 Mon Sep 17 00:00:00 2001 From: Olivier Debeir Date: Fri, 6 Dec 2013 09:08:34 +0100 Subject: [PATCH 02/12] shorten line --- skimage/filter/rank/generic.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/skimage/filter/rank/generic.py b/skimage/filter/rank/generic.py index 29f243d1..6341b822 100644 --- a/skimage/filter/rank/generic.py +++ b/skimage/filter/rank/generic.py @@ -529,7 +529,8 @@ def pop(image, selem, out=None, mask=None, shift_x=False, shift_y=False): mask=mask, shift_x=shift_x, shift_y=shift_y) def sum(image, selem, out=None, mask=None, shift_x=False, shift_y=False): - """Return the sum of pixels inside the neighborhood. If sum does not fit the data type,folding is possible. + """Return the sum of pixels inside the neighborhood. + If sum does not fit the data type,folding is possible. Parameters ---------- From 39b256a7ede77eb5376e01ad7fddad29c31916f2 Mon Sep 17 00:00:00 2001 From: Olivier Debeir Date: Fri, 6 Dec 2013 10:05:19 +0100 Subject: [PATCH 03/12] fix:rank.sum docstring --- skimage/filter/rank/generic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skimage/filter/rank/generic.py b/skimage/filter/rank/generic.py index 6341b822..ba45a898 100644 --- a/skimage/filter/rank/generic.py +++ b/skimage/filter/rank/generic.py @@ -558,12 +558,12 @@ def sum(image, selem, out=None, mask=None, shift_x=False, shift_y=False): >>> # Local mean >>> from skimage.morphology import square >>> import skimage.filter.rank as rank - >>> ima = 255 * np.array([[0, 0, 0, 0, 0], + >>> ima = 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) - >>> rank.pop(ima, square(3)) + >>> rank.sum(ima, square(3)) array([[1, 2, 3, 2, 1], [2, 4, 6, 4, 2], [3, 6, 9, 6, 3], From 0fc0c9f042cedf1f93876dd066fb3f40013b5dfa Mon Sep 17 00:00:00 2001 From: Olivier Debeir Date: Fri, 6 Dec 2013 10:06:32 +0100 Subject: [PATCH 04/12] fix:rank.sum docstring --- skimage/filter/rank/generic.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/skimage/filter/rank/generic.py b/skimage/filter/rank/generic.py index ba45a898..2c3557b4 100644 --- a/skimage/filter/rank/generic.py +++ b/skimage/filter/rank/generic.py @@ -508,7 +508,6 @@ def pop(image, selem, out=None, mask=None, shift_x=False, shift_y=False): Examples -------- - >>> # Local mean >>> from skimage.morphology import square >>> import skimage.filter.rank as rank >>> ima = 255 * np.array([[0, 0, 0, 0, 0], @@ -555,7 +554,6 @@ def sum(image, selem, out=None, mask=None, shift_x=False, shift_y=False): Examples -------- - >>> # Local mean >>> from skimage.morphology import square >>> import skimage.filter.rank as rank >>> ima = np.array([[0, 0, 0, 0, 0], From ef5de4165b15821b3f1d1feb46fd6fe188f0a31a Mon Sep 17 00:00:00 2001 From: Olivier Debeir Date: Fri, 6 Dec 2013 10:08:27 +0100 Subject: [PATCH 05/12] fix:rank.sum docstring --- skimage/filter/rank/generic.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/skimage/filter/rank/generic.py b/skimage/filter/rank/generic.py index 2c3557b4..b7293862 100644 --- a/skimage/filter/rank/generic.py +++ b/skimage/filter/rank/generic.py @@ -528,8 +528,7 @@ def pop(image, selem, out=None, mask=None, shift_x=False, shift_y=False): mask=mask, shift_x=shift_x, shift_y=shift_y) def sum(image, selem, out=None, mask=None, shift_x=False, shift_y=False): - """Return the sum of pixels inside the neighborhood. - If sum does not fit the data type,folding is possible. + """Return the sum of pixels inside the neighborhood (truncated to uint8 or uint16). Parameters ---------- From 1f8adcc7555ea2555b2f7a011d31f406157090a7 Mon Sep 17 00:00:00 2001 From: Olivier Debeir Date: Fri, 6 Dec 2013 10:09:10 +0100 Subject: [PATCH 06/12] fix:indent --- skimage/filter/rank/generic_cy.pyx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/skimage/filter/rank/generic_cy.pyx b/skimage/filter/rank/generic_cy.pyx index 2953b2f0..dd461fc7 100644 --- a/skimage/filter/rank/generic_cy.pyx +++ b/skimage/filter/rank/generic_cy.pyx @@ -222,9 +222,9 @@ cdef inline double _kernel_pop(Py_ssize_t* histo, double pop, dtype_t g, return pop cdef inline double _kernel_sum(Py_ssize_t* histo, double pop,dtype_t g, - Py_ssize_t max_bin, Py_ssize_t mid_bin, - double p0, double p1, - Py_ssize_t s0, Py_ssize_t s1): + Py_ssize_t max_bin, Py_ssize_t mid_bin, + double p0, double p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i cdef Py_ssize_t sum = 0 From d98ed722d986fd92e25115d112c737aaaa70ab39 Mon Sep 17 00:00:00 2001 From: Olivier Debeir Date: Fri, 6 Dec 2013 10:37:39 +0100 Subject: [PATCH 07/12] add:rank.sum_percentile --- skimage/filter/rank/__init__.py | 2 +- skimage/filter/rank/_percentile.py | 36 ++++++++++++++++++++++++++ skimage/filter/rank/percentile_cy.pyx | 32 +++++++++++++++++++++++ skimage/filter/rank/tests/test_rank.py | 34 +++++++++++++++++++++++- 4 files changed, 102 insertions(+), 2 deletions(-) diff --git a/skimage/filter/rank/__init__.py b/skimage/filter/rank/__init__.py index 361e400e..9a31a565 100644 --- a/skimage/filter/rank/__init__.py +++ b/skimage/filter/rank/__init__.py @@ -4,7 +4,7 @@ from .generic import (autolevel, bottomhat, equalize, gradient, maximum, mean, from ._percentile import (autolevel_percentile, gradient_percentile, mean_percentile, subtract_mean_percentile, enhance_contrast_percentile, percentile, - pop_percentile, threshold_percentile) + pop_percentile,sum_percentile, threshold_percentile) from .bilateral import mean_bilateral, pop_bilateral from skimage._shared.utils import deprecated diff --git a/skimage/filter/rank/_percentile.py b/skimage/filter/rank/_percentile.py index ff3b1559..7f24e3e0 100644 --- a/skimage/filter/rank/_percentile.py +++ b/skimage/filter/rank/_percentile.py @@ -310,6 +310,42 @@ def pop_percentile(image, selem, out=None, mask=None, shift_x=False, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y, p0=p0, p1=p1) +def sum_percentile(image, selem, out=None, mask=None, shift_x=False, + shift_y=False, p0=0, p1=1): + """Return greyscale local sum of an image. + + sum is computed on the given structuring element. Only levels between + percentiles [p0, p1] are used. result is truncated (8bit or 16bit). + + Parameters + ---------- + image : ndarray (uint8, uint16) + Image array. + selem : ndarray + The neighborhood expressed as a 2-D array of 1's and 0's. + out : ndarray (same dtype as input) + If None, a new array will be allocated. + mask : ndarray + Mask array that defines (>0) area of the image included in the local + neighborhood. If None, the complete image is used (default). + shift_x, shift_y : int + Offset added to the structuring element center point. Shift is bounded + to the structuring element sizes (center must be inside the given + structuring element). + p0, p1 : float in [0, ..., 1] + Define the [p0, p1] percentile interval to be considered for computing + the value. + + Returns + ------- + out : ndarray (same dtype as input image) + Output image. + + """ + + return _apply(percentile_cy._sum, + image, selem, out=out, mask=mask, shift_x=shift_x, + shift_y=shift_y, p0=p0, p1=p1) def threshold_percentile(image, selem, out=None, mask=None, shift_x=False, shift_y=False, p0=0): diff --git a/skimage/filter/rank/percentile_cy.pyx b/skimage/filter/rank/percentile_cy.pyx index e951a76e..bd0cbe53 100644 --- a/skimage/filter/rank/percentile_cy.pyx +++ b/skimage/filter/rank/percentile_cy.pyx @@ -90,6 +90,29 @@ cdef inline double _kernel_mean(Py_ssize_t* histo, double pop, dtype_t g, else: return 0 +cdef inline double _kernel_sum(Py_ssize_t* histo, double pop, dtype_t g, + Py_ssize_t max_bin, Py_ssize_t mid_bin, + double p0, double p1, + Py_ssize_t s0, Py_ssize_t s1): + + cdef Py_ssize_t i, sum, sum_g, n + + if pop: + sum = 0 + sum_g = 0 + n = 0 + for i in range(max_bin): + sum += histo[i] + if (sum >= p0 * pop) and (sum <= p1 * pop): + n += histo[i] + sum_g += histo[i] * i + + if n > 0: + return sum_g + else: + return 0 + else: + return 0 cdef inline double _kernel_subtract_mean(Py_ssize_t* histo, double pop, dtype_t g, @@ -245,6 +268,15 @@ def _mean(dtype_t[:, ::1] image, _core(_kernel_mean[dtype_t], image, selem, mask, out, shift_x, shift_y, p0, p1, 0, 0, max_bin) +def _sum(dtype_t[:, ::1] image, + char[:, ::1] selem, + char[:, ::1] mask, + dtype_t_out[:, ::1] out, + char shift_x, char shift_y, double p0, double p1, + Py_ssize_t max_bin): + + _core(_kernel_sum[dtype_t], image, selem, mask, out, + shift_x, shift_y, p0, p1, 0, 0, max_bin) def _subtract_mean(dtype_t[:, ::1] image, char[:, ::1] selem, diff --git a/skimage/filter/rank/tests/test_rank.py b/skimage/filter/rank/tests/test_rank.py index 26aa43ed..b3de866a 100644 --- a/skimage/filter/rank/tests/test_rank.py +++ b/skimage/filter/rank/tests/test_rank.py @@ -530,9 +530,41 @@ def test_sum(): [3, 6, 9, 6, 3], [2, 4, 6, 4, 2], [1, 2, 3, 2, 1]], dtype=np.uint16) - print image16 assert_array_equal(r, out16) +def test_sum_percentile(): + # check the number of valid pixels in the neighborhood + + image8 = 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) + image16 = 400*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.uint16) + elem = np.ones((3, 3), dtype=np.uint8) + out8 = np.empty_like(image8) + out16 = np.empty_like(image16) + mask = np.ones(image16.shape, dtype=np.uint8) + + rank.sum_percentile(image=image8, selem=elem, out=out8, mask=mask,p0=.0,p1=1.) + r = np.array([[1, 2, 3, 2, 1], + [2, 4, 6, 4, 2], + [3, 6, 9, 6, 3], + [2, 4, 6, 4, 2], + [1, 2, 3, 2, 1]], dtype=np.uint8) + assert_array_equal(r, out8) + + rank.sum_percentile(image=image16, selem=elem, out=out16, mask=mask,p0=.0,p1=1.) + r = 400* np.array([[1, 2, 3, 2, 1], + [2, 4, 6, 4, 2], + [3, 6, 9, 6, 3], + [2, 4, 6, 4, 2], + [1, 2, 3, 2, 1]], dtype=np.uint16) + assert_array_equal(r, out16) if __name__ == "__main__": run_module_suite() From 3f0877981025ee139342eb88e8e5f6640abf06fb Mon Sep 17 00:00:00 2001 From: Olivier Debeir Date: Fri, 6 Dec 2013 10:47:52 +0100 Subject: [PATCH 08/12] add:rank.sum_bilateral --- skimage/filter/rank/__init__.py | 2 +- skimage/filter/rank/bilateral.py | 60 +++++++++++++++++++++++++- skimage/filter/rank/bilateral_cy.pyx | 31 +++++++++++++ skimage/filter/rank/tests/test_rank.py | 51 ++++++---------------- 4 files changed, 104 insertions(+), 40 deletions(-) diff --git a/skimage/filter/rank/__init__.py b/skimage/filter/rank/__init__.py index 9a31a565..aad8036b 100644 --- a/skimage/filter/rank/__init__.py +++ b/skimage/filter/rank/__init__.py @@ -5,7 +5,7 @@ from ._percentile import (autolevel_percentile, gradient_percentile, mean_percentile, subtract_mean_percentile, enhance_contrast_percentile, percentile, pop_percentile,sum_percentile, threshold_percentile) -from .bilateral import mean_bilateral, pop_bilateral +from .bilateral import mean_bilateral, pop_bilateral, sum_bilateral from skimage._shared.utils import deprecated diff --git a/skimage/filter/rank/bilateral.py b/skimage/filter/rank/bilateral.py index f1b10fec..f6e7d228 100644 --- a/skimage/filter/rank/bilateral.py +++ b/skimage/filter/rank/bilateral.py @@ -30,7 +30,7 @@ from . import bilateral_cy from .generic import _handle_input -__all__ = ['mean_bilateral', 'pop_bilateral'] +__all__ = ['mean_bilateral', 'pop_bilateral', 'sum_bilateral'] def _apply(func, image, selem, out, mask, shift_x, shift_y, s0, s1, @@ -155,3 +155,61 @@ def pop_bilateral(image, selem, out=None, mask=None, shift_x=False, return _apply(bilateral_cy._pop, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y, s0=s0, s1=s1) + +def sum_bilateral(image, selem, out=None, mask=None, shift_x=False, + shift_y=False, s0=10, s1=10): + """Apply a flat kernel bilateral filter. + + This is an edge-preserving and noise reducing denoising filter. It averages + pixels based on their spatial closeness and radiometric similarity. + + Spatial closeness is measured by considering only the local pixel + neighborhood given by a structuring element (selem). + + Radiometric similarity is defined by the greylevel interval [g-s0, g+s1] + where g is the current pixel greylevel. Only pixels belonging to the + structuring element AND having a greylevel inside this interval are + averaged. Return greyscale local bilateral_sum of an image. + + Parameters + ---------- + image : ndarray (uint8, uint16) + Image array. + selem : ndarray + The neighborhood expressed as a 2-D array of 1's and 0's. + out : ndarray (same dtype as input) + If None, a new array will be allocated. + mask : ndarray + Mask array that defines (>0) area of the image included in the local + neighborhood. If None, the complete image is used (default). + shift_x, shift_y : int + Offset added to the structuring element center point. Shift is bounded + to the structuring element sizes (center must be inside the given + structuring element). + s0, s1 : int + Define the [s0, s1] interval around the greyvalue of the center pixel + to be considered for computing the value. + + Returns + ------- + out : ndarray (same dtype as input image) + Output image. + + See also + -------- + skimage.filter.denoise_bilateral for a gaussian bilateral filter. + + Examples + -------- + >>> from skimage import data + >>> from skimage.morphology import disk + >>> from skimage.filter.rank import bilateral_sum + >>> # Load test image / cast to uint16 + >>> ima = data.camera().astype(np.uint16) + >>> # bilateral filtering of cameraman image using a flat kernel + >>> bilat_ima = bilateral_sum(ima, disk(20), s0=10,s1=10) + + """ + + return _apply(bilateral_cy._sum, image, selem, out=out, + mask=mask, shift_x=shift_x, shift_y=shift_y, s0=s0, s1=s1) diff --git a/skimage/filter/rank/bilateral_cy.pyx b/skimage/filter/rank/bilateral_cy.pyx index de2b3e53..25a81766 100644 --- a/skimage/filter/rank/bilateral_cy.pyx +++ b/skimage/filter/rank/bilateral_cy.pyx @@ -47,6 +47,27 @@ cdef inline double _kernel_pop(Py_ssize_t* histo, double pop, dtype_t g, else: return 0 +cdef inline double _kernel_sum(Py_ssize_t* histo, double pop, dtype_t g, + Py_ssize_t max_bin, Py_ssize_t mid_bin, + double p0, double p1, + Py_ssize_t s0, Py_ssize_t s1): + + cdef Py_ssize_t i + cdef Py_ssize_t bilat_pop = 0 + cdef Py_ssize_t sum = 0 + + if pop: + for i in range(max_bin): + if (g > (i - s0)) and (g < (i + s1)): + bilat_pop += histo[i] + sum += histo[i] * i + if bilat_pop: + return sum + else: + return 0 + else: + return 0 + def _mean(dtype_t[:, ::1] image, char[:, ::1] selem, @@ -68,3 +89,13 @@ def _pop(dtype_t[:, ::1] image, _core(_kernel_pop[dtype_t], image, selem, mask, out, shift_x, shift_y, 0, 0, s0, s1, max_bin) + +def _sum(dtype_t[:, ::1] image, + char[:, ::1] selem, + char[:, ::1] mask, + dtype_t_out[:, ::1] out, + char shift_x, char shift_y, Py_ssize_t s0, Py_ssize_t s1, + Py_ssize_t max_bin): + + _core(_kernel_sum[dtype_t], image, selem, mask, out, + shift_x, shift_y, 0, 0, s0, s1, max_bin) diff --git a/skimage/filter/rank/tests/test_rank.py b/skimage/filter/rank/tests/test_rank.py index b3de866a..72f9afe6 100644 --- a/skimage/filter/rank/tests/test_rank.py +++ b/skimage/filter/rank/tests/test_rank.py @@ -516,55 +516,30 @@ def test_sum(): out16 = np.empty_like(image16) mask = np.ones(image8.shape, dtype=np.uint8) + r = np.array([[1, 2, 3, 2, 1], + [2, 4, 6, 4, 2], + [3, 6, 9, 6, 3], + [2, 4, 6, 4, 2], + [1, 2, 3, 2, 1]], dtype=np.uint8) rank.sum(image=image8, selem=elem, out=out8, mask=mask) - r = np.array([[1, 2, 3, 2, 1], - [2, 4, 6, 4, 2], - [3, 6, 9, 6, 3], - [2, 4, 6, 4, 2], - [1, 2, 3, 2, 1]], dtype=np.uint8) assert_array_equal(r, out8) - - rank.sum(image=image16, selem=elem, out=out16, mask=mask) - r = 400* np.array([[1, 2, 3, 2, 1], - [2, 4, 6, 4, 2], - [3, 6, 9, 6, 3], - [2, 4, 6, 4, 2], - [1, 2, 3, 2, 1]], dtype=np.uint16) - assert_array_equal(r, out16) - -def test_sum_percentile(): - # check the number of valid pixels in the neighborhood - - image8 = 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) - image16 = 400*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.uint16) - elem = np.ones((3, 3), dtype=np.uint8) - out8 = np.empty_like(image8) - out16 = np.empty_like(image16) - mask = np.ones(image16.shape, dtype=np.uint8) - rank.sum_percentile(image=image8, selem=elem, out=out8, mask=mask,p0=.0,p1=1.) - r = np.array([[1, 2, 3, 2, 1], - [2, 4, 6, 4, 2], - [3, 6, 9, 6, 3], - [2, 4, 6, 4, 2], - [1, 2, 3, 2, 1]], dtype=np.uint8) + assert_array_equal(r, out8) + rank.sum_bilateral(image=image8, selem=elem, out=out8, mask=mask,s0=255,s1=255) assert_array_equal(r, out8) - rank.sum_percentile(image=image16, selem=elem, out=out16, mask=mask,p0=.0,p1=1.) r = 400* np.array([[1, 2, 3, 2, 1], [2, 4, 6, 4, 2], [3, 6, 9, 6, 3], [2, 4, 6, 4, 2], [1, 2, 3, 2, 1]], dtype=np.uint16) + rank.sum(image=image16, selem=elem, out=out16, mask=mask) assert_array_equal(r, out16) + rank.sum_percentile(image=image16, selem=elem, out=out16, mask=mask,p0=.0,p1=1.) + assert_array_equal(r, out16) + rank.sum_bilateral(image=image16, selem=elem, out=out16, mask=mask,s0=1000,s1=1000) + assert_array_equal(r, out16) + if __name__ == "__main__": run_module_suite() From 0b8d83c24c61994c488f69ed699e307f7c5da390 Mon Sep 17 00:00:00 2001 From: Olivier Debeir Date: Fri, 6 Dec 2013 10:50:27 +0100 Subject: [PATCH 09/12] fix:rank.sum docstrings --- skimage/filter/rank/_percentile.py | 2 +- skimage/filter/rank/bilateral.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/skimage/filter/rank/_percentile.py b/skimage/filter/rank/_percentile.py index 7f24e3e0..f2b5e7f4 100644 --- a/skimage/filter/rank/_percentile.py +++ b/skimage/filter/rank/_percentile.py @@ -315,7 +315,7 @@ def sum_percentile(image, selem, out=None, mask=None, shift_x=False, """Return greyscale local sum of an image. sum is computed on the given structuring element. Only levels between - percentiles [p0, p1] are used. result is truncated (8bit or 16bit). + percentiles [p0, p1] are used. Result is truncated (8bit or 16bit). Parameters ---------- diff --git a/skimage/filter/rank/bilateral.py b/skimage/filter/rank/bilateral.py index f6e7d228..6abba0ec 100644 --- a/skimage/filter/rank/bilateral.py +++ b/skimage/filter/rank/bilateral.py @@ -169,7 +169,8 @@ def sum_bilateral(image, selem, out=None, mask=None, shift_x=False, Radiometric similarity is defined by the greylevel interval [g-s0, g+s1] where g is the current pixel greylevel. Only pixels belonging to the structuring element AND having a greylevel inside this interval are - averaged. Return greyscale local bilateral_sum of an image. + summed. Return greyscale local bilateral_sum of an image. + Result is truncated (8bit or 16bit). Parameters ---------- From 9d734ad4f2ac629121d5ab510d1bf2d5c6a0ef3f Mon Sep 17 00:00:00 2001 From: Olivier Debeir Date: Fri, 6 Dec 2013 12:05:36 +0100 Subject: [PATCH 10/12] fix:docstring in sum_bilateral --- skimage/filter/rank/bilateral.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/skimage/filter/rank/bilateral.py b/skimage/filter/rank/bilateral.py index 6abba0ec..4509e06b 100644 --- a/skimage/filter/rank/bilateral.py +++ b/skimage/filter/rank/bilateral.py @@ -169,7 +169,7 @@ def sum_bilateral(image, selem, out=None, mask=None, shift_x=False, Radiometric similarity is defined by the greylevel interval [g-s0, g+s1] where g is the current pixel greylevel. Only pixels belonging to the structuring element AND having a greylevel inside this interval are - summed. Return greyscale local bilateral_sum of an image. + summed. Return greyscale local bilateral sum of an image. Result is truncated (8bit or 16bit). Parameters @@ -204,11 +204,11 @@ def sum_bilateral(image, selem, out=None, mask=None, shift_x=False, -------- >>> from skimage import data >>> from skimage.morphology import disk - >>> from skimage.filter.rank import bilateral_sum + >>> from skimage.filter.rank import sum_bilateral >>> # Load test image / cast to uint16 >>> ima = data.camera().astype(np.uint16) >>> # bilateral filtering of cameraman image using a flat kernel - >>> bilat_ima = bilateral_sum(ima, disk(20), s0=10,s1=10) + >>> bilat_ima = sum_bilateral(ima, disk(20), s0=10,s1=10) """ From cb310feab111e2dafc95360eca4c34cf3e9c7388 Mon Sep 17 00:00:00 2001 From: Olivier Debeir Date: Fri, 6 Dec 2013 13:42:06 +0100 Subject: [PATCH 11/12] fix:rtank.__init__ --- skimage/filter/rank/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/skimage/filter/rank/__init__.py b/skimage/filter/rank/__init__.py index aad8036b..9fc2c18e 100644 --- a/skimage/filter/rank/__init__.py +++ b/skimage/filter/rank/__init__.py @@ -4,7 +4,7 @@ from .generic import (autolevel, bottomhat, equalize, gradient, maximum, mean, from ._percentile import (autolevel_percentile, gradient_percentile, mean_percentile, subtract_mean_percentile, enhance_contrast_percentile, percentile, - pop_percentile,sum_percentile, threshold_percentile) + pop_percentile, sum_percentile, threshold_percentile) from .bilateral import mean_bilateral, pop_bilateral, sum_bilateral from skimage._shared.utils import deprecated @@ -52,6 +52,8 @@ __all__ = ['autolevel', 'pop_percentile', 'pop_bilateral', 'sum', + 'sum_bilateral', + 'sum_percentile', 'threshold', 'threshold_percentile', 'tophat', From 2e84dc639935f89a05ece74b340659b311737412 Mon Sep 17 00:00:00 2001 From: odebeir Date: Fri, 6 Dec 2013 20:13:00 +0100 Subject: [PATCH 12/12] fix:indentation --- skimage/filter/rank/generic.py | 8 ++++---- skimage/filter/rank/generic_cy.pyx | 8 ++++---- skimage/filter/rank/percentile_cy.pyx | 16 ++++++++-------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/skimage/filter/rank/generic.py b/skimage/filter/rank/generic.py index b7293862..703b66b3 100644 --- a/skimage/filter/rank/generic.py +++ b/skimage/filter/rank/generic.py @@ -556,10 +556,10 @@ def sum(image, selem, out=None, mask=None, shift_x=False, shift_y=False): >>> from skimage.morphology import square >>> import skimage.filter.rank as rank >>> ima = 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) + ... [0, 1, 1, 1, 0], + ... [0, 1, 1, 1, 0], + ... [0, 1, 1, 1, 0], + ... [0, 0, 0, 0, 0]], dtype=np.uint8) >>> rank.sum(ima, square(3)) array([[1, 2, 3, 2, 1], [2, 4, 6, 4, 2], diff --git a/skimage/filter/rank/generic_cy.pyx b/skimage/filter/rank/generic_cy.pyx index dd461fc7..2a1a8455 100644 --- a/skimage/filter/rank/generic_cy.pyx +++ b/skimage/filter/rank/generic_cy.pyx @@ -471,10 +471,10 @@ def _pop(dtype_t[:, ::1] image, shift_x, shift_y, 0, 0, 0, 0, max_bin) def _sum(dtype_t[:, ::1] image, - char[:, ::1] selem, - char[:, ::1] mask, - dtype_t_out[:, ::1] out, - char shift_x, char shift_y, Py_ssize_t max_bin): + char[:, ::1] selem, + char[:, ::1] mask, + dtype_t_out[:, ::1] out, + char shift_x, char shift_y, Py_ssize_t max_bin): _core(_kernel_sum[dtype_t], image, selem, mask, out, shift_x, shift_y, 0, 0, 0, 0, max_bin) diff --git a/skimage/filter/rank/percentile_cy.pyx b/skimage/filter/rank/percentile_cy.pyx index bd0cbe53..38d04b33 100644 --- a/skimage/filter/rank/percentile_cy.pyx +++ b/skimage/filter/rank/percentile_cy.pyx @@ -91,9 +91,9 @@ cdef inline double _kernel_mean(Py_ssize_t* histo, double pop, dtype_t g, return 0 cdef inline double _kernel_sum(Py_ssize_t* histo, double pop, dtype_t g, - Py_ssize_t max_bin, Py_ssize_t mid_bin, - double p0, double p1, - Py_ssize_t s0, Py_ssize_t s1): + Py_ssize_t max_bin, Py_ssize_t mid_bin, + double p0, double p1, + Py_ssize_t s0, Py_ssize_t s1): cdef Py_ssize_t i, sum, sum_g, n @@ -269,11 +269,11 @@ def _mean(dtype_t[:, ::1] image, shift_x, shift_y, p0, p1, 0, 0, max_bin) def _sum(dtype_t[:, ::1] image, - char[:, ::1] selem, - char[:, ::1] mask, - dtype_t_out[:, ::1] out, - char shift_x, char shift_y, double p0, double p1, - Py_ssize_t max_bin): + char[:, ::1] selem, + char[:, ::1] mask, + dtype_t_out[:, ::1] out, + char shift_x, char shift_y, double p0, double p1, + Py_ssize_t max_bin): _core(_kernel_sum[dtype_t], image, selem, mask, out, shift_x, shift_y, p0, p1, 0, 0, max_bin)