diff --git a/skimage/filter/_tv_denoise.py b/skimage/filter/_tv_denoise.py index 545f9e09..76affb98 100644 --- a/skimage/filter/_tv_denoise.py +++ b/skimage/filter/_tv_denoise.py @@ -1,5 +1,5 @@ import numpy as np -from skimage.util import dtype +from skimage import img_as_float def _tv_denoise_3d(im, weight=100, eps=2.e-4, n_iter_max=200): @@ -171,7 +171,7 @@ def _tv_denoise_2d(im, weight=50, eps=2.e-4, n_iter_max=200): i += 1 return out -def tv_denoise(im, weight=50, eps=2.e-4, keep_type=False, n_iter_max=200): +def tv_denoise(im, weight=50, eps=2.e-4, n_iter_max=200): """ Perform total-variation denoising on 2-d and 3-d images @@ -192,11 +192,6 @@ def tv_denoise(im, weight=50, eps=2.e-4, keep_type=False, n_iter_max=200): (E_(n-1) - E_n) < eps * E_0 - keep_type: bool, optional (False) - whether the output has the same dtype as the input array. - keep_type is False by default, and the dtype of the output - is np.float - n_iter_max: int, optional maximal number of iterations used for the optimization. @@ -245,7 +240,7 @@ def tv_denoise(im, weight=50, eps=2.e-4, keep_type=False, n_iter_max=200): """ im_type = im.dtype if not im_type.kind == 'f': - im = im.astype(np.float) + im = img_as_float(im) if im.ndim == 2: out = _tv_denoise_2d(im, weight, eps, n_iter_max) @@ -254,9 +249,4 @@ def tv_denoise(im, weight=50, eps=2.e-4, keep_type=False, n_iter_max=200): else: raise ValueError('only 2-d and 3-d images may be denoised with this ' 'function') - if keep_type: - return out.astype(im_type) - else: - if not im_type.kind == 'f': - out = dtype.convert(out.astype(im_type), np.float) - return out + return out diff --git a/skimage/filter/tests/test_tv_denoise.py b/skimage/filter/tests/test_tv_denoise.py index 47f53251..afee6b02 100644 --- a/skimage/filter/tests/test_tv_denoise.py +++ b/skimage/filter/tests/test_tv_denoise.py @@ -2,7 +2,7 @@ import numpy as np from numpy.testing import run_module_suite from skimage import filter, data, color -from skimage import img_as_uint +from skimage import img_as_uint, img_as_ubyte class TestTvDenoise(): @@ -29,8 +29,8 @@ class TestTvDenoise(): # test if the total variation has decreased assert (np.sqrt((grad_denoised**2).sum()) < np.sqrt((grad**2).sum()) / 2) - denoised_lena_int = filter.tv_denoise(img_as_uint(lena), - weight=60.0, keep_type=True) + denoised_lena_int = img_as_uint(filter.tv_denoise(img_as_ubyte(lena), + weight=60.0)) assert denoised_lena_int.dtype is np.dtype('uint16') def test_tv_denoise_float_result_range(self): @@ -55,13 +55,13 @@ class TestTvDenoise(): mask += 20 * np.random.randn(*mask.shape) mask[mask < 0] = 0 mask[mask > 255] = 255 - res = filter.tv_denoise(mask.astype(np.uint8), - weight=100, keep_type=True) + res = img_as_ubyte(filter.tv_denoise(mask.astype(np.uint8), + weight=100)) assert res.std() < mask.std() assert res.dtype is np.dtype('uint8') - res = filter.tv_denoise(mask.astype(np.uint8), weight=100) + res = img_as_ubyte(filter.tv_denoise(mask.astype(np.uint8), weight=100)) assert res.std() < mask.std() - assert res.dtype is not np.dtype('uint8') + # test wrong number of dimensions a = np.random.random((8, 8, 8, 8)) try: