Add support for all data types.

All dtypes larger than 8-bits are converted to uint16 and then bit-shifted to
uint12.
This commit is contained in:
Tony S Yu
2013-06-29 14:16:22 -05:00
parent 58a4a43f10
commit f92f057cbd
2 changed files with 24 additions and 8 deletions
+5 -7
View File
@@ -17,7 +17,7 @@ References
"""
import numpy as np
from skimage import img_as_ubyte
from skimage import img_as_ubyte, img_as_uint
from skimage.filter.rank import _crank8, _crank16
from skimage.filter.rank.generic import find_bitdepth
@@ -44,18 +44,16 @@ def _apply(func8, func16, image, selem, out, mask, shift_x, shift_y):
if func8 is not None and (is_8bit or func16 is None):
out = _apply8(func8, image, selem, out, mask, shift_x, shift_y)
elif image.dtype == np.uint16:
if func16 is None:
raise TypeError("Not implemented for uint16 image.")
else:
image = img_as_uint(image)
if out is None:
out = np.zeros(image.shape, dtype=np.uint16)
bitdepth = find_bitdepth(image)
if bitdepth > 11:
raise ValueError("Only uint16 <4096 image (12bit) supported.")
image = image >> 4
bitdepth = find_bitdepth(image)
func16(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.")
return out
+19 -1
View File
@@ -1,7 +1,7 @@
import numpy as np
from numpy.testing import run_module_suite, assert_array_equal, assert_raises
from skimage import img_as_ubyte
from skimage import img_as_ubyte, img_as_uint, img_as_float
from skimage import data, util
from skimage.morphology import cmorph, disk
from skimage.filter import rank
@@ -187,6 +187,24 @@ def test_compare_autolevels_16bit():
assert_array_equal(loc_autolevel, loc_perc_autolevel)
def test_compare_uint_vs_float():
# filters applied on 8-bit image ore 16-bit image (having only real 8-bit of
# dynamic) should be identical
# Create signed int8 image that and convert it to uint8
image_uint = img_as_uint(data.camera())
image_float = img_as_float(image_uint)
methods = ['autolevel', 'bottomhat', 'equalize', 'gradient', 'threshold',
'meansubtraction', 'morph_contr_enh', 'pop', 'tophat']
for method in methods:
func = getattr(rank, method)
out_u = func(image_uint, disk(3))
out_f = func(image_float, disk(3))
assert_array_equal(out_u, out_f)
def test_compare_8bit_unsigned_vs_signed():
# filters applied on 8-bit image ore 16-bit image (having only real 8-bit of
# dynamic) should be identical