From e804f96f02a53864400dd40a8796c371b0e15647 Mon Sep 17 00:00:00 2001 From: Olivier Debeir Date: Thu, 8 Nov 2012 10:45:20 +0100 Subject: [PATCH] fix noise_filter --- skimage/filter/rank/_crank8.pyx | 23 +++++++++++++++++++++ skimage/filter/rank/rank.pyx | 36 ++++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/skimage/filter/rank/_crank8.pyx b/skimage/filter/rank/_crank8.pyx index 9ddcb12d..5cb0b5ed 100644 --- a/skimage/filter/rank/_crank8.pyx +++ b/skimage/filter/rank/_crank8.pyx @@ -246,6 +246,22 @@ cdef inline np.uint8_t kernel_noise_filter( else: 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 Py_ssize_t min_i + cdef float e,p + + e = 0 + + for i in range(256): + p = histo[i]/pop + if p>0: + e -= p*np.log2(p) + + return < np.uint8_t > e # ----------------------------------------------------------------- # python wrappers @@ -385,3 +401,10 @@ def noise_filter(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): return _core8(kernel_noise_filter, image, selem, mask, out, shift_x, shift_y, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 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): + return _core8(kernel_entropy, image, selem, mask, out, shift_x, shift_y, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 0) diff --git a/skimage/filter/rank/rank.pyx b/skimage/filter/rank/rank.pyx index 74875d1c..12517cfa 100644 --- a/skimage/filter/rank/rank.pyx +++ b/skimage/filter/rank/rank.pyx @@ -28,11 +28,11 @@ def _apply(func8, func16, image, selem, out, mask, shift_x, shift_y): mask = img_as_ubyte(mask) if image.dtype == np.uint8: if func8 is None: - raise TypeError("uint8 image not supported for this filter") + raise TypeError("not implemented for uint8 image") return func8(image, selem, shift_x=shift_x, shift_y=shift_y, mask=mask, out=out) elif image.dtype == np.uint16: if func16 is None: - raise TypeError("uint16 image not supported for this filter") + raise TypeError("not implemented for uint16 image") bitdepth = find_bitdepth(image) if bitdepth > 11: raise ValueError("only uint16 <4096 image (12bit) supported!") @@ -623,4 +623,34 @@ def noise_filter(image, selem, out=None, mask=None, shift_x=False, shift_y=False selem_cpy = selem.copy() 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) \ No newline at end of file + 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 (in bit) computed locally (precision is limited due to image type used 8- or 16-bit) + + Parameters + ---------- + image : ndarray + Image array (uint8 array or uint16). If image is uint16, the algorithm uses max. 12bit histogram, + an exception will be raised if image has a value > 4095 + selem : ndarray + The neighborhood expressed as a 2-D array of 1's and 0's. + out : ndarray + The array to store the result of the morphology. If None is + passed, a new array will be allocated. + mask : ndarray (uint8) + 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 : uint8 array or uint16 array (same as input image) + local entropy (in bit) + + """ + + return _apply(_crank8.entropy, None, image, selem_cpy, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y) \ No newline at end of file