mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-15 11:25:53 +08:00
fixed data of tv_denoise result images with float datatype to be in default range [0.0:1.0]
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user