diff --git a/skimage/filter/rank/_crank8.pyx b/skimage/filter/rank/_crank8.pyx index da716bd9..a0c3cc35 100644 --- a/skimage/filter/rank/_crank8.pyx +++ b/skimage/filter/rank/_crank8.pyx @@ -223,6 +223,26 @@ cdef inline np.uint8_t kernel_tophat( return < np.uint8_t > (i - g) +cdef inline np.uint8_t kernel_noise_filter( + 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 + + for i in range(255, g, -1): + if histo[i]: + break + min_i = i-g + for i in range(0, g): + if histo[i]: + break + if g-i < min_i: + return < np.uint8_t > (g-i) + else: + return < np.uint8_t > min_i + + # ----------------------------------------------------------------- # python wrappers # ----------------------------------------------------------------- @@ -380,3 +400,12 @@ def tophat(np.ndarray[np.uint8_t, ndim=2] image, """top hat """ return _core8(kernel_tophat, image, selem, mask, out, shift_x, shift_y, .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): + """top hat + """ + return _core8(kernel_noise_filter, image, selem, mask, out, shift_x, shift_y, .0, .0, < Py_ssize_t > 0, < Py_ssize_t > 0) diff --git a/skimage/filter/rank/demo/demo_single.py b/skimage/filter/rank/demo/demo_single.py index 652f2cc8..3b7eaa95 100644 --- a/skimage/filter/rank/demo/demo_single.py +++ b/skimage/filter/rank/demo/demo_single.py @@ -19,6 +19,27 @@ if __name__ == '__main__': den = denoise_bilateral(a8,win_size=10,sigma_range=10,sigma_spatial=2)[:,:,0] f16b= rank.bilateral_mean(a8.astype(np.uint16),disk(10),s0=10,s1=10) + + selem = np.ones((3,3)) + selem[1,1] = 0 + radius = 3 + selem = disk(radius) + selem[radius,radius] = 0 + print selem + noise = rank.noise_filter(a8,selem) + plt.imsave('noise.png',noise,cmap=plt.cm.gray) + plt.imsave('cam.png',a8,cmap=plt.cm.gray) + print noise + + plt.figure() + plt.subplot(1,2,1) + plt.imshow(a8) + plt.subplot(1,2,2) + plt.imshow(noise) + plt.colorbar() + plt.show() + + plt.figure() plt.subplot(1,2,1) plt.imshow(den) diff --git a/skimage/filter/rank/rank.pyx b/skimage/filter/rank/rank.pyx index 2f144e98..40ceecad 100644 --- a/skimage/filter/rank/rank.pyx +++ b/skimage/filter/rank/rank.pyx @@ -19,7 +19,7 @@ from skimage.filter.rank import _crank8, _crank16 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'] + 'modal', 'morph_contr_enh', 'pop', 'threshold', 'tophat','noise_filter'] def _apply(func8, func16, image, selem, out, mask, shift_x, shift_y): @@ -580,3 +580,31 @@ 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): + """Return minimum absolute diffirence between a pixel and its neighborhood + + 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) + The image noise . + + """ + + return _apply(_crank8.noise_filter, None, image, selem, out=out, mask=mask, shift_x=shift_x, shift_y=shift_y) \ No newline at end of file