Merge Tony Yu's tv denoise fixes.

This commit is contained in:
Stefan van der Walt
2011-07-10 13:52:14 -07:00
2 changed files with 17 additions and 21 deletions
@@ -22,7 +22,7 @@ class TestTvDenoise():
grad = ndimage.morphological_gradient(lena, size=((3,3)))
grad_denoised = ndimage.morphological_gradient(denoised_lena, size=((3,3)))
# test if the total variation has decreased
assert np.sqrt((grad_denoised**2).sum()) < np.sqrt((grad**2).sum())
assert np.sqrt((grad_denoised**2).sum()) < np.sqrt((grad**2).sum()) / 2
denoised_lena_int = F.tv_denoise(lena.astype(np.int32), \
weight=60.0, keep_type=True)
assert denoised_lena_int.dtype is np.dtype('int32')
+16 -20
View File
@@ -1,6 +1,6 @@
import numpy as np
def _tv_denoise_3d(im, eps=2.e-4, weight=100, keep_type=False, n_iter_max=200):
def _tv_denoise_3d(im, weight=100, eps=2.e-4, keep_type=False, n_iter_max=200):
"""
Perform total-variation denoising on 3-D arrays
@@ -9,15 +9,15 @@ def _tv_denoise_3d(im, eps=2.e-4, weight=100, keep_type=False, n_iter_max=200):
im: ndarray
3-D input data to be denoised
weight: float, optional
denoising weight. The greater ``weight``, the more denoising (at
the expense of fidelity to ``input``)
eps: float, optional
relative difference of the value of the cost function that determines
the stop criterion. The algorithm stops when
(E_(n-1) - E_n) < eps * E_0
weight: float, optional
denoising weight. The greater ``weight``, the more denoising (at
the expense of fidelity to ``input``)
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
@@ -78,13 +78,11 @@ def _tv_denoise_3d(im, eps=2.e-4, weight=100, keep_type=False, n_iter_max=200):
pz -= 1/6.*gz
pz /= norm
E /= float(im.size)
print E
if i == 0:
E_init = E
E_previous = E
else:
if np.abs(E_previous - E) < eps * E_init:
print E_previous, E
break
else:
E_previous = E
@@ -103,15 +101,15 @@ def _tv_denoise_2d(im, weight=50, eps=2.e-4, keep_type=False, n_iter_max=200):
im: ndarray
input data to be denoised
weight: float, optional
denoising weight. The greater ``weight``, the more denoising (at
the expense of fidelity to ``input``)
eps: float, optional
relative difference of the value of the cost function that determines
the stop criterion. The algorithm stops when
(E_(n-1) - E_n) < eps * E_0
weight: float, optional
denoising weight. The greater ``weight``, the more denoising (at
the expense of fidelity to ``input``)
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
@@ -176,7 +174,6 @@ def _tv_denoise_2d(im, weight=50, eps=2.e-4, keep_type=False, n_iter_max=200):
py -= 0.25*gy
py /= norm
E /= float(im.size)
print E
if i == 0:
E_init = E
E_previous = E
@@ -186,13 +183,12 @@ def _tv_denoise_2d(im, weight=50, eps=2.e-4, keep_type=False, n_iter_max=200):
else:
E_previous = E
i += 1
print i
if keep_type:
return out.astype(im_type)
else:
return out
def tv_denoise(im, eps=2.e-4, weight=50, keep_type=False, n_iter_max=200):
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
@@ -203,15 +199,15 @@ def tv_denoise(im, eps=2.e-4, weight=50, keep_type=False, n_iter_max=200):
but it is cast into an ndarray of floats for the computation
of the denoised image.
weight: float, optional
denoising weight. The greater ``weight``, the more denoising (at
the expense of fidelity to ``input``)
eps: float, optional
relative difference of the value of the cost function that
determines the stop criterion. The algorithm stops when
(E_(n-1) - E_n) < eps * E_0
weight: float, optional
denoising weight. The greater ``weight``, the more denoising (at
the expense of fidelity to ``input``)
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
@@ -265,9 +261,9 @@ def tv_denoise(im, eps=2.e-4, weight=50, keep_type=False, n_iter_max=200):
"""
if im.ndim == 2:
return _tv_denoise_2d(im, eps, weight, keep_type, n_iter_max)
return _tv_denoise_2d(im, weight, eps, keep_type, n_iter_max)
elif im.ndim == 3:
return _tv_denoise_3d(im, eps, weight, keep_type, n_iter_max)
return _tv_denoise_3d(im, weight, eps, keep_type, n_iter_max)
else:
raise ValueError('only 2-d and 3-d images may be denoised with this function')