diff --git a/skimage/rank/rank.py b/skimage/rank/rank.py index d569a12b..7e4d16a2 100644 --- a/skimage/rank/rank.py +++ b/skimage/rank/rank.py @@ -7,59 +7,23 @@ __docformat__ = 'restructuredtext en' import warnings from skimage import img_as_ubyte +import numpy as np -__all__ = ['mean','percentile_mean','bilateral_mean'] +import _crank16,_crank8 +__all__ = ['mean'] -def percentile_mean(image, selem, out=None, shift_x=False, shift_y=False, p0=.0, p1=1.): - """Return greyscale local mean of an image. - - Mean is computed on the given structuring element. Only pixel values contained inside the - percentile interval [p0,p1] are taken into account. - - Parameters - ---------- - image : ndarray - Image array (uint8 array or uint16). - 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. - shift_x, shift_y : bool - shift structuring element about center point. This only affects - eccentric structuring elements (i.e. selem with even numbered sides). - Shift is bounded to the structuring element sizes. - - Returns - ------- - local mean : uint8 array or uint16 array depending on input image - The result of the local mean. - - Examples - -------- - to be updated - >>> # Erosion shrinks bright regions - >>> from skimage.morphology import square - >>> bright_square = 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) - >>> erosion(bright_square, square(3)) - array([[0, 0, 0, 0, 0], - [0, 0, 0, 0, 0], - [0, 0, 1, 0, 0], - [0, 0, 0, 0, 0], - [0, 0, 0, 0, 0]], dtype=uint8) - +def find_bitdepth(image): + """returns the max bith depth of a uint16 image """ - pass + umax = np.max(image) + if umax>2: + return int(np.log2(umax)) + else: + return 1 -def bilateral_mean(image, selem, out=None, shift_x=False, shift_y=False, s0=10, s1=10): - pass -def mean(image, selem, out=None, shift_x=False, shift_y=False): +def mean(image, selem, mask=None, out=None, shift_x=False, shift_y=False): """Return greyscale local mean of an image. Mean is computed on the given structuring element. @@ -67,7 +31,11 @@ def mean(image, selem, out=None, shift_x=False, shift_y=False): Parameters ---------- image : ndarray - Image array (uint8 array or uint16). + Image array (uint8 array or uint16). If image is uint16, as the algorithm uses max. 12bit histogram, + an exception will be raised if image has a value > 4095 + 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). selem : ndarray The neighborhood expressed as a 2-D array of 1's and 0's. out : ndarray @@ -101,12 +69,18 @@ def mean(image, selem, out=None, shift_x=False, shift_y=False): [0, 0, 0, 0, 0]], dtype=uint8) """ - pass -# if image is out: -# raise NotImplementedError("In-place erosion not supported!") -# image = img_as_ubyte(image) -# selem = img_as_ubyte(selem) -# return cmorph.erode(image, selem, out=out, -# shift_x=shift_x, shift_y=shift_y) - + if image is out: + raise NotImplementedError("In-place erosion not supported!") + selem = img_as_ubyte(selem) + if mask is not None: + mask = img_as_ubyte(mask) + if image.dtype == np.uint8: + return _crank8.mean(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask) + elif image.dtype == np.uint16: + bitdepth = find_bitdepth(image) + if bitdepth>11: + raise ValueError("only uint16 <4096 image are supported!") + return _crank16.mean(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,bitdepth=bitdepth+1) + else: + raise TypeError("only uint8 and uint16 image supported!") diff --git a/skimage/rank/tests/test_rank.py b/skimage/rank/tests/test_rank.py index 2a1e253d..77dae32b 100644 --- a/skimage/rank/tests/test_rank.py +++ b/skimage/rank/tests/test_rank.py @@ -2,9 +2,30 @@ import numpy as np import matplotlib.pyplot as plt from skimage import data +from skimage.morphology.selem import disk import skimage.rank as rank print dir(rank) +print rank.mean +print rank.percentile_mean +print rank.bilateral_mean + +a8 = data.camera() +a16 = a8.astype('uint16')*16 +selem = disk(10) + +f8 = rank.mean(a8,selem) +f16 = rank.mean(a16,selem) + +plt.figure() +plt.imshow(np.hstack((a8,f8))) +plt.colorbar() +plt.figure() +plt.imshow(np.hstack((a16,f16))) +plt.colorbar() +plt.show() + +