diff --git a/skimage/rank/rank.py b/skimage/rank/rank.py index c26fd033..9045f04e 100644 --- a/skimage/rank/rank.py +++ b/skimage/rank/rank.py @@ -9,11 +9,365 @@ import warnings from skimage import img_as_ubyte import numpy as np -from .generic import find_bitdepth +from generic import find_bitdepth import _crank16,_crank8 -__all__ = ['mean'] +__all__ = ['autolevel','bottomhat','egalise','gradient','maximum','mean' + ,'meansubstraction','median','minimum','modal','morph_contr_enh','pop'] +def autolevel(image, selem, out=None, mask=None, shift_x=False, shift_y=False): + """Return greyscale local autolevel of an image. + + Autolevel is computed on the given structuring element. + + Parameters + ---------- + image : ndarray + 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 + 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 : 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 autolevel : uint8 array or uint16 array depending on input image + The result of the local autolevel. + + Examples + -------- + to be updated + >>> # Local mean + >>> from skimage.morphology import square + >>> ima8 = 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) + >>> autolevel(ima8, square(3)) + array([[ 0, 0, 0, 0, 0], + [ 0, 255, 255, 255, 0], + [ 0, 255, 0, 255, 0], + [ 0, 255, 255, 255, 0], + [ 0, 0, 0, 0, 0]], dtype=uint8) + + >>> ima16 = 4095*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) + >>> autolevel(ima16, square(3)) + array([[ 0, 0, 0, 0, 0], + [ 0, 4096, 4096, 4096, 0], + [ 0, 4096, 0, 4096, 0], + [ 0, 4096, 4096, 4096, 0], + [ 0, 0, 0, 0, 0]], dtype=uint16) + + """ + selem = img_as_ubyte(selem) + if mask is not None: + mask = img_as_ubyte(mask) + if image.dtype == np.uint8: + return _crank8.autolevel(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,out=out) + elif image.dtype == np.uint16: + bitdepth = find_bitdepth(image) + if bitdepth>11: + raise ValueError("only uint16 <4096 image (12bit) supported!") + return _crank16.autolevel(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,bitdepth=bitdepth+1,out=out) + else: + raise TypeError("only uint8 and uint16 image supported!") + +def bottomhat(image, selem, out=None, mask=None, shift_x=False, shift_y=False): + """Return greyscale local bottomhat of an image. + + Bottomhat is computed on the given structuring element. + + Parameters + ---------- + image : ndarray + 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 + 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 : 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 bottomhat : uint8 array or uint16 array depending on input image + The result of the local bottomhat. + + Examples + -------- + to be updated + >>> # Local mean + >>> from skimage.morphology import square + >>> ima8 = 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) + >>> bottomhat(ima8, square(3)) + array([[ 0, 0, 0, 0, 0], + [ 0, 255, 255, 255, 0], + [ 0, 255, 0, 255, 0], + [ 0, 255, 255, 255, 0], + [ 0, 0, 0, 0, 0]], dtype=uint8) + + >>> ima16 = 4095*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) + >>> bottomhat(ima16, square(3)) + array([[ 0, 0, 0, 0, 0], + [ 0, 4095, 4095, 4095, 0], + [ 0, 4095, 0, 4095, 0], + [ 0, 4095, 4095, 4095, 0], + [ 0, 0, 0, 0, 0]], dtype=uint16) + """ + selem = img_as_ubyte(selem) + if mask is not None: + mask = img_as_ubyte(mask) + if image.dtype == np.uint8: + return _crank8.bottomhat(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,out=out) + elif image.dtype == np.uint16: + bitdepth = find_bitdepth(image) + if bitdepth>11: + raise ValueError("only uint16 <4096 image (12bit) supported!") + return _crank16.bottomhat(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,bitdepth=bitdepth+1,out=out) + else: + raise TypeError("only uint8 and uint16 image supported!") + +def egalise(image, selem, out=None, mask=None, shift_x=False, shift_y=False): + """Return greyscale local egalise of an image. + + egalise is computed on the given structuring element. + + Parameters + ---------- + image : ndarray + 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 + 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 : 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 egalise : uint8 array or uint16 array depending on input image + The result of the local egalise. + + Examples + -------- + to be updated + >>> # Local mean + >>> from skimage.morphology import square + >>> ima8 = 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) + >>> egalise(ima8, square(3)) + array([[191, 170, 127, 170, 191], + [170, 255, 255, 255, 170], + [127, 255, 255, 255, 127], + [170, 255, 255, 255, 170], + [191, 170, 127, 170, 191]], dtype=uint8) + + >>> ima16 = 4095*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) + >>> egalise(ima16, square(3)) + array([[3072, 2730, 2048, 2730, 3072], + [2730, 4096, 4096, 4096, 2730], + [2048, 4096, 4096, 4096, 2048], + [2730, 4096, 4096, 4096, 2730], + [3072, 2730, 2048, 2730, 3072]], dtype=uint16) + """ + selem = img_as_ubyte(selem) + if mask is not None: + mask = img_as_ubyte(mask) + if image.dtype == np.uint8: + return _crank8.egalise(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,out=out) + elif image.dtype == np.uint16: + bitdepth = find_bitdepth(image) + if bitdepth>11: + raise ValueError("only uint16 <4096 image (12bit) supported!") + return _crank16.egalise(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,bitdepth=bitdepth+1,out=out) + else: + raise TypeError("only uint8 and uint16 image supported!") + +def gradient(image, selem, out=None, mask=None, shift_x=False, shift_y=False): + """Return greyscale local gradient of an image. + + gradient is computed on the given structuring element. + + Parameters + ---------- + image : ndarray + 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 + 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 : 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 gradient : uint8 array or uint16 array depending on input image + The result of the local gradient. + + Examples + -------- + to be updated + >>> # Local gradient + >>> from skimage.morphology import square + >>> ima8 = 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) + >>> gradient(ima8, square(3)) + array([[255, 255, 255, 255, 255], + [255, 255, 255, 255, 255], + [255, 255, 0, 255, 255], + [255, 255, 255, 255, 255], + [255, 255, 255, 255, 255]], dtype=uint8) + + >>> ima16 = 4095*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) + >>> gradient(ima16, square(3)) + array([[4095, 4095, 4095, 4095, 4095], + [4095, 4095, 4095, 4095, 4095], + [4095, 4095, 0, 4095, 4095], + [4095, 4095, 4095, 4095, 4095], + [4095, 4095, 4095, 4095, 4095]], dtype=uint16) + + """ + selem = img_as_ubyte(selem) + if mask is not None: + mask = img_as_ubyte(mask) + if image.dtype == np.uint8: + return _crank8.gradient(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,out=out) + elif image.dtype == np.uint16: + bitdepth = find_bitdepth(image) + if bitdepth>11: + raise ValueError("only uint16 <4096 image (12bit) supported!") + return _crank16.gradient(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,bitdepth=bitdepth+1,out=out) + else: + raise TypeError("only uint8 and uint16 image supported!") + + +def maximum(image, selem, out=None, mask=None, shift_x=False, shift_y=False): + """Return greyscale local maximum of an image. + + maximum is computed on the given structuring element. + + Parameters + ---------- + image : ndarray + 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 + 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 : 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 maximum : uint8 array or uint16 array depending on input image + The result of the local maximum. + + Examples + -------- + to be updated + >>> # Local maximum + >>> from skimage.morphology import square + >>> ima8 = 255*np.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=np.uint8) + >>> maximum(ima8, square(3)) + array([[ 0, 0, 0, 0, 0], + [ 0, 255, 255, 255, 0], + [ 0, 255, 255, 255, 0], + [ 0, 255, 255, 255, 0], + [ 0, 0, 0, 0, 0]], dtype=uint8) + + >>> ima16 = 4095*np.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=np.uint16) + >>> maximum(ima16, square(3)) + array([[ 0, 0, 0, 0, 0], + [ 0, 4095, 4095, 4095, 0], + [ 0, 4095, 4095, 4095, 0], + [ 0, 4095, 4095, 4095, 0], + [ 0, 0, 0, 0, 0]], dtype=uint16) + + """ + selem = img_as_ubyte(selem) + if mask is not None: + mask = img_as_ubyte(mask) + if image.dtype == np.uint8: + return _crank8.maximum(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,out=out) + elif image.dtype == np.uint16: + bitdepth = find_bitdepth(image) + if bitdepth>11: + raise ValueError("only uint16 <4096 image (12bit) supported!") + return _crank16.maximum(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,bitdepth=bitdepth+1,out=out) + else: + raise TypeError("only uint8 and uint16 image supported!") def mean(image, selem, out=None, mask=None, shift_x=False, shift_y=False): """Return greyscale local mean of an image. @@ -86,3 +440,572 @@ def mean(image, selem, out=None, mask=None, shift_x=False, shift_y=False): else: raise TypeError("only uint8 and uint16 image supported!") +def meansubstraction(image, selem, out=None, mask=None, shift_x=False, shift_y=False): + """Return greyscale local meansubstraction of an image. + + meansubstraction is computed on the given structuring element. + + Parameters + ---------- + image : ndarray + 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 + 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 : 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 meansubstraction : uint8 array or uint16 array depending on input image + The result of the local meansubstraction. + + Examples + -------- + to be updated + >>> # Local meansubstraction + >>> from skimage.morphology import square + >>> ima8 = 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) + >>> meansubstraction(ima8, square(3)) + array([[ 95, 84, 63, 84, 95], + [ 84, 197, 169, 197, 84], + [ 63, 169, 127, 169, 63], + [ 84, 197, 169, 197, 84], + [ 95, 84, 63, 84, 95]], dtype=uint8) + + >>> ima16 = 4095*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) + >>> meansubstraction(ima16, square(3)) + array([[1536, 1365, 1024, 1365, 1536], + [1365, 3185, 2730, 3185, 1365], + [1024, 2730, 2048, 2730, 1024], + [1365, 3185, 2730, 3185, 1365], + [1536, 1365, 1024, 1365, 1536]], dtype=uint16) + + """ + selem = img_as_ubyte(selem) + if mask is not None: + mask = img_as_ubyte(mask) + if image.dtype == np.uint8: + return _crank8.meansubstraction(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,out=out) + elif image.dtype == np.uint16: + bitdepth = find_bitdepth(image) + if bitdepth>11: + raise ValueError("only uint16 <4096 image (12bit) supported!") + return _crank16.meansubstraction(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,bitdepth=bitdepth+1,out=out) + else: + raise TypeError("only uint8 and uint16 image supported!") + +def median(image, selem, out=None, mask=None, shift_x=False, shift_y=False): + """Return greyscale local median of an image. + + median is computed on the given structuring element. + + Parameters + ---------- + image : ndarray + 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 + 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 : 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 median : uint8 array or uint16 array depending on input image + The result of the local median. + + Examples + -------- + to be updated + >>> # Local median + >>> from skimage.morphology import square + >>> ima8 = 255*np.array([[0, 0, 0, 0, 0], + ... [0, 1, 1, 1, 0], + ... [0, 1, 0, 1, 0], + ... [0, 1, 1, 1, 0], + ... [0, 0, 0, 0, 0]], dtype=np.uint8) + >>> median(ima8, square(3)) + array([[ 0, 0, 255, 0, 0], + [ 0, 0, 255, 0, 0], + [255, 255, 255, 255, 255], + [ 0, 0, 255, 0, 0], + [ 0, 0, 255, 0, 0]], dtype=uint8) + + >>> ima16 = 4095*np.array([[0, 0, 0, 0, 0], + ... [0, 1, 1, 1, 0], + ... [0, 1, 0, 1, 0], + ... [0, 1, 1, 1, 0], + ... [0, 0, 0, 0, 0]], dtype=np.uint16) + >>> median(ima16, square(3)) + array([[ 0, 0, 4095, 0, 0], + [ 0, 0, 4095, 0, 0], + [4095, 4095, 4095, 4095, 4095], + [ 0, 0, 4095, 0, 0], + [ 0, 0, 4095, 0, 0]], dtype=uint16) + + """ + selem = img_as_ubyte(selem) + if mask is not None: + mask = img_as_ubyte(mask) + if image.dtype == np.uint8: + return _crank8.median(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,out=out) + elif image.dtype == np.uint16: + bitdepth = find_bitdepth(image) + if bitdepth>11: + raise ValueError("only uint16 <4096 image (12bit) supported!") + return _crank16.median(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,bitdepth=bitdepth+1,out=out) + else: + raise TypeError("only uint8 and uint16 image supported!") + +def minimum(image, selem, out=None, mask=None, shift_x=False, shift_y=False): + """Return greyscale local minimum of an image. + + minimum is computed on the given structuring element. + + Parameters + ---------- + image : ndarray + 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 + 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 : 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 minimum : uint8 array or uint16 array depending on input image + The result of the local minimum. + + Examples + -------- + to be updated + >>> # Local minimum + >>> from skimage.morphology import square + >>> ima8 = 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) + >>> minimum(ima8, square(3)) + array([[ 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0], + [ 0, 0, 255, 0, 0], + [ 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0]], dtype=uint8) + + + >>> ima16 = 4095*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) + >>> minimum(ima16, square(3)) + array([[ 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0], + [ 0, 0, 4095, 0, 0], + [ 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0]], dtype=uint16) + + """ + selem = img_as_ubyte(selem) + if mask is not None: + mask = img_as_ubyte(mask) + if image.dtype == np.uint8: + return _crank8.minimum(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,out=out) + elif image.dtype == np.uint16: + bitdepth = find_bitdepth(image) + if bitdepth>11: + raise ValueError("only uint16 <4096 image (12bit) supported!") + return _crank16.minimum(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,bitdepth=bitdepth+1,out=out) + else: + raise TypeError("only uint8 and uint16 image supported!") + +def modal(image, selem, out=None, mask=None, shift_x=False, shift_y=False): + """Return greyscale local modal of an image. + + modal is computed on the given structuring element. + + Parameters + ---------- + image : ndarray + 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 + 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 : 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 modal : uint8 array or uint16 array depending on input image + The result of the local modal. + + Examples + -------- + to be updated + >>> # Local modal + >>> from skimage.morphology import square + >>> ima8 = np.array([[0, 0, 0, 0, 0], + ... [0, 1, 1, 1, 0], + ... [0, 1, 5, 6, 0], + ... [0, 1, 5, 5, 0], + ... [0, 0, 0, 5, 0]], dtype=np.uint8) + >>> modal(ima8, square(3)) + array([[0, 0, 0, 0, 0], + [0, 0, 1, 0, 0], + [0, 1, 1, 0, 0], + [0, 0, 5, 0, 0], + [0, 0, 5, 0, 0]], dtype=uint8) + + + >>> ima16 = 100*np.array([[0, 0, 0, 0, 0], + ... [0, 1, 1, 1, 0], + ... [0, 1, 5, 6, 0], + ... [0, 1, 5, 5, 0], + ... [0, 0, 0, 5, 0]], dtype=np.uint16) + >>> modal(ima16, square(3)) + array([[ 0, 0, 0, 0, 0], + [ 0, 0, 100, 0, 0], + [ 0, 100, 100, 0, 0], + [ 0, 0, 500, 0, 0], + [ 0, 0, 500, 0, 0]], dtype=uint16) + + """ + selem = img_as_ubyte(selem) + if mask is not None: + mask = img_as_ubyte(mask) + if image.dtype == np.uint8: + return _crank8.modal(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,out=out) + elif image.dtype == np.uint16: + bitdepth = find_bitdepth(image) + if bitdepth>11: + raise ValueError("only uint16 <4096 image (12bit) supported!") + return _crank16.modal(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,bitdepth=bitdepth+1,out=out) + else: + raise TypeError("only uint8 and uint16 image supported!") + +def morph_contr_enh(image, selem, out=None, mask=None, shift_x=False, shift_y=False): + """Return greyscale local morph_contr_enh of an image. + + morph_contr_enh is computed on the given structuring element. + + Parameters + ---------- + image : ndarray + 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 + 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 : 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 morph_contr_enh : uint8 array or uint16 array depending on input image + The result of the local morph_contr_enh. + + Examples + -------- + to be updated + >>> # Local mean + >>> from skimage.morphology import square + >>> ima8 = 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) + >>> morph_contr_enh(ima8, square(3)) + 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=uint8) + + >>> ima16 = 4095*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) + >>> morph_contr_enh(ima16, square(3)) + array([[ 0, 0, 0, 0, 0], + [ 0, 4095, 4095, 4095, 0], + [ 0, 4095, 4095, 4095, 0], + [ 0, 4095, 4095, 4095, 0], + [ 0, 0, 0, 0, 0]], dtype=uint16) + + """ + selem = img_as_ubyte(selem) + if mask is not None: + mask = img_as_ubyte(mask) + if image.dtype == np.uint8: + return _crank8.morph_contr_enh(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,out=out) + elif image.dtype == np.uint16: + bitdepth = find_bitdepth(image) + if bitdepth>11: + raise ValueError("only uint16 <4096 image (12bit) supported!") + return _crank16.morph_contr_enh(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,bitdepth=bitdepth+1,out=out) + else: + raise TypeError("only uint8 and uint16 image supported!") + +def pop(image, selem, out=None, mask=None, shift_x=False, shift_y=False): + """Return greyscale local pop of an image. + + pop is computed on the given structuring element. + + Parameters + ---------- + image : ndarray + 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 + 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 : 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 pop : uint8 array or uint16 array depending on input image + The result of the local pop. + + Examples + -------- + to be updated + >>> # Local mean + >>> from skimage.morphology import square + >>> ima8 = 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) + >>> pop(ima8, square(3)) + array([[4, 6, 6, 6, 4], + [6, 9, 9, 9, 6], + [6, 9, 9, 9, 6], + [6, 9, 9, 9, 6], + [4, 6, 6, 6, 4]], dtype=uint8) + + >>> ima16 = 4095*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) + >>> pop(ima16, square(3)) + array([[4, 6, 6, 6, 4], + [6, 9, 9, 9, 6], + [6, 9, 9, 9, 6], + [6, 9, 9, 9, 6], + [4, 6, 6, 6, 4]], dtype=uint16) + + """ + selem = img_as_ubyte(selem) + if mask is not None: + mask = img_as_ubyte(mask) + if image.dtype == np.uint8: + return _crank8.pop(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,out=out) + elif image.dtype == np.uint16: + bitdepth = find_bitdepth(image) + if bitdepth>11: + raise ValueError("only uint16 <4096 image (12bit) supported!") + return _crank16.pop(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,bitdepth=bitdepth+1,out=out) + else: + raise TypeError("only uint8 and uint16 image supported!") + +def threshold(image, selem, out=None, mask=None, shift_x=False, shift_y=False): + """Return greyscale local threshold of an image. + + threshold is computed on the given structuring element. + + Parameters + ---------- + image : ndarray + 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 + 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 : 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 threshold : uint8 array or uint16 array depending on input image + The result of the local threshold. + + Examples + -------- + to be updated + >>> # Local mean + >>> from skimage.morphology import square + >>> ima8 = 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) + >>> threshold(ima8, square(3)) + array([[0, 0, 0, 0, 0], + [0, 1, 1, 1, 0], + [0, 1, 0, 1, 0], + [0, 1, 1, 1, 0], + [0, 0, 0, 0, 0]], dtype=uint8) + + >>> ima16 = 4095*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) + >>> threshold(ima16, square(3)) + array([[0, 0, 0, 0, 0], + [0, 1, 1, 1, 0], + [0, 1, 0, 1, 0], + [0, 1, 1, 1, 0], + [0, 0, 0, 0, 0]], dtype=uint16) + + + """ + selem = img_as_ubyte(selem) + if mask is not None: + mask = img_as_ubyte(mask) + if image.dtype == np.uint8: + return _crank8.threshold(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,out=out) + elif image.dtype == np.uint16: + bitdepth = find_bitdepth(image) + if bitdepth>11: + raise ValueError("only uint16 <4096 image (12bit) supported!") + return _crank16.threshold(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,bitdepth=bitdepth+1,out=out) + else: + raise TypeError("only uint8 and uint16 image supported!") + +def tophat(image, selem, out=None, mask=None, shift_x=False, shift_y=False): + """Return greyscale local tophat of an image. + + tophat is computed on the given structuring element. + + Parameters + ---------- + image : ndarray + 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 + 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 : 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 tophat : uint8 array or uint16 array depending on input image + The result of the local tophat. + + Examples + -------- + to be updated + >>> # Local mean + >>> from skimage.morphology import square + >>> ima8 = 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) + >>> tophat(ima8, square(3)) + array([[255, 255, 255, 255, 255], + [255, 0, 0, 0, 255], + [255, 0, 0, 0, 255], + [255, 0, 0, 0, 255], + [255, 255, 255, 255, 255]], dtype=uint8) + + >>> ima16 = 4095*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) + >>> tophat(ima16, square(3)) + array([[4095, 4095, 4095, 4095, 4095], + [4095, 0, 0, 0, 4095], + [4095, 0, 0, 0, 4095], + [4095, 0, 0, 0, 4095], + [4095, 4095, 4095, 4095, 4095]], dtype=uint16) + """ + selem = img_as_ubyte(selem) + if mask is not None: + mask = img_as_ubyte(mask) + if image.dtype == np.uint8: + return _crank8.tophat(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,out=out) + elif image.dtype == np.uint16: + bitdepth = find_bitdepth(image) + if bitdepth>11: + raise ValueError("only uint16 <4096 image (12bit) supported!") + return _crank16.tophat(image,selem,shift_x=shift_x,shift_y=shift_y,mask=mask,bitdepth=bitdepth+1,out=out) + else: + raise TypeError("only uint8 and uint16 image supported!") \ No newline at end of file diff --git a/skimage/rank/tests/demo_all.py b/skimage/rank/tests/demo_all.py index 8e09048e..da592c3d 100644 --- a/skimage/rank/tests/demo_all.py +++ b/skimage/rank/tests/demo_all.py @@ -1,21 +1,23 @@ import numpy as np import matplotlib.pyplot as plt +from pprint import pprint from skimage import data from skimage.morphology.selem import disk -from skimage.rank import crank8,crank8_percentiles -from skimage.rank import crank16,crank16_percentiles,crank16_bilateral +from skimage.rank import _crank8,_crank8_percentiles +from skimage.rank import _crank16,_crank16_percentiles,_crank16_bilateral -if __name__ == '__main__': +def plot_all(): a8 = data.camera() a16 = a8.astype('uint16')*16 -# selem = np.ones((30,30),dtype='uint8') + # selem = np.ones((30,30),dtype='uint8') selem = disk(5) - for n in dir(crank16): - method = eval('crank16.%s'%n) + + for n in dir(_crank16): + method = eval('_crank16.%s'%n) t = type(method) - if t == type(crank8.maximum): + if t == type(_crank8.maximum): print n,t f = method(a16,selem = selem,bitdepth=12) @@ -28,10 +30,10 @@ if __name__ == '__main__': plt.colorbar() plt.title(method) - for n in dir(crank8): - method = eval('crank8.%s'%n) + for n in dir(_crank8): + method = eval('_crank8.%s'%n) t = type(method) - if t == type(crank8.maximum): + if t == type(_crank8.maximum): print n,t f = method(a8,selem = selem) @@ -44,10 +46,10 @@ if __name__ == '__main__': plt.colorbar() plt.title(method) - for n in dir(crank8_percentiles): - method = eval('crank8_percentiles.%s'%n) + for n in dir(_crank8_percentiles): + method = eval('_crank8_percentiles.%s'%n) t = type(method) - if t == type(crank8.maximum): + if t == type(_crank8.maximum): print n,t f = method(a8,selem = selem,p0=.1,p1=.9) @@ -60,10 +62,10 @@ if __name__ == '__main__': plt.colorbar() plt.title(method) - for n in dir(crank16_percentiles): - method = eval('crank16_percentiles.%s'%n) + for n in dir(_crank16_percentiles): + method = eval('_crank16_percentiles.%s'%n) t = type(method) - if t == type(crank8.maximum): + if t == type(_crank8.maximum): print n,t f = method(a16,selem = selem,bitdepth=12,p0=.1,p1=.9) @@ -77,10 +79,10 @@ if __name__ == '__main__': plt.title(method) selem = disk(50) - for n in dir(crank16_bilateral): - method = eval('crank16_bilateral.%s'%n) + for n in dir(_crank16_bilateral): + method = eval('_crank16_bilateral.%s'%n) t = type(method) - if t == type(crank8.maximum): + if t == type(_crank8.maximum): print n,t f = method(a16,selem = selem,bitdepth=12,s0=300,s1=300) @@ -95,3 +97,7 @@ if __name__ == '__main__': # plt.show() + +if __name__ == '__main__': +# plot_all() + pprint(dir(_crank8)) \ No newline at end of file