From 14d0923959e7509fd40f3f1b67e43989fa72a037 Mon Sep 17 00:00:00 2001 From: Andreas Wuerl Date: Mon, 27 Aug 2012 12:41:29 +0200 Subject: [PATCH] fixed data of tv_denoise result images with float datatype to be in default range [0.0:1.0] --- skimage/filter/_tv_denoise.py | 27 ++++++++++++++++++++++++- skimage/filter/tests/test_tv_denoise.py | 11 +++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/skimage/filter/_tv_denoise.py b/skimage/filter/_tv_denoise.py index e8736786..af8decfc 100644 --- a/skimage/filter/_tv_denoise.py +++ b/skimage/filter/_tv_denoise.py @@ -170,7 +170,30 @@ def _tv_denoise_2d(im, weight=50, eps=2.e-4, n_iter_max=200): i += 1 return out +def _renormalize_image(image, data_type): + """ + inplace renormalize image date to obey the float range [0.0:1.0] + Parameters + ---------- + immage: ndarray + input data to be renormalized + + data_type: type, + image data type before running tv_denoise + + Notes + ------- + the minimum and maximum values of the image data type define the range + which is mapped to the interval [0.0:1.0] + + """ + type_info = np.iinfo(data_type) + start = type_info.min + width = type_info.max - type_info.min + np.subtract(image, start, image) + np.divide(image, width, image) + def tv_denoise(im, weight=50, eps=2.e-4, keep_type=False, n_iter_max=200): """ Perform total-variation denoising on 2-d and 3-d images @@ -257,4 +280,6 @@ def tv_denoise(im, weight=50, eps=2.e-4, keep_type=False, n_iter_max=200): if keep_type: return out.astype(im_type) else: - return out + if not im_type.kind == 'f': + _renormalize_image(out, im_type) + return out diff --git a/skimage/filter/tests/test_tv_denoise.py b/skimage/filter/tests/test_tv_denoise.py index 4cc6adbb..47f53251 100644 --- a/skimage/filter/tests/test_tv_denoise.py +++ b/skimage/filter/tests/test_tv_denoise.py @@ -33,6 +33,16 @@ class TestTvDenoise(): weight=60.0, keep_type=True) assert denoised_lena_int.dtype is np.dtype('uint16') + def test_tv_denoise_float_result_range(self): + # lena image + lena = color.rgb2gray(data.lena())[:256, :256] + int_lena = np.multiply(lena, 255).astype(np.uint8) + assert np.max(int_lena) > 1 + denoised_int_lena = filter.tv_denoise(int_lena, weight=60.0) + # test if the value range of output float data is within [0.0:1.0] + assert np.max(denoised_int_lena) <= 1.0 + assert np.min(denoised_int_lena) >= 0.0 + def test_tv_denoise_3d(self): """ Apply the TV denoising algorithm on a 3D image representing @@ -59,6 +69,5 @@ class TestTvDenoise(): except ValueError: pass - if __name__ == "__main__": run_module_suite()