Skip to content

Commit

Permalink
MAINT: reuse code from _sigma_est_dwt within _wavelet_threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
grlee77 committed Sep 7, 2016
1 parent 251fe2f commit deb96a2
Showing 1 changed file with 36 additions and 38 deletions.
74 changes: 36 additions & 38 deletions skimage/restoration/_denoise.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,41 @@ def _bayes_thresh(details, var):
return thresh


def _sigma_est_dwt(detail_coeffs, distribution='Gaussian'):
"""Calculate the robust median estimator of the noise standard deviation.
Parameters
----------
detail_coeffs : ndarray
The detail coefficients corresponding to the discrete wavelet
transform of an image.
distribution : str
The underlying noise distribution.
Returns
-------
sigma : float
The estimated noise standard deviation (see section 4.2 of [1]_).
References
----------
.. [1] D. L. Donoho and I. M. Johnstone. "Ideal spatial adaptation
by wavelet shrinkage." Biometrika 81.3 (1994): 425-455.
DOI:10.1093/biomet/81.3.425
"""
# Consider regions with detail coefficients exactly zero to be masked out
detail_coeffs = detail_coeffs[np.nonzero(detail_coeffs)]

if distribution.lower() == 'gaussian':
# 75th quantile of the underlying, symmetric noise distribution
denom = scipy.stats.norm.ppf(0.75)
sigma = np.median(np.abs(detail_coeffs)) / denom
else:
raise ValueError("Only Gaussian noise estimation is currently "
"supported")
return sigma


def _wavelet_threshold(img, wavelet, threshold=None, sigma=None, mode='soft',
wavelet_levels=None):
"""Perform wavelet denoising.
Expand Down Expand Up @@ -420,7 +455,7 @@ def _wavelet_threshold(img, wavelet, threshold=None, sigma=None, mode='soft',
if sigma is None:
# Estimate the noise via the method in [2]_
detail_coeffs = dcoeffs[-1]['d' * img.ndim]
sigma = np.median(np.abs(detail_coeffs)) / 0.67448975019608171
sigma = _sigma_est_dwt(detail_coeffs, distribution='Gaussian')

if threshold is None:
# The BayesShrink thresholds from [1]_ in docstring
Expand Down Expand Up @@ -527,43 +562,6 @@ def denoise_wavelet(img, sigma=None, wavelet='db1', mode='soft',
return np.clip(out, *clip_range)


def _sigma_est_dwt(detail_coeffs, distribution='Gaussian'):
"""
Calculation of the robust median estimator of the noise standard
deviation.
Parameters
----------
detail_coeffs : ndarray
The detail coefficients corresponding to the discrete wavelet
transform of an image.
distribution : str
The underlying noise distribution.
Returns
-------
sigma : float
The estimated noise standard deviation (see section 4.2 of [1]_).
References
----------
.. [1] D. L. Donoho and I. M. Johnstone. "Ideal spatial adaptation
by wavelet shrinkage." Biometrika 81.3 (1994): 425-455.
DOI:10.1093/biomet/81.3.425
"""
# consider regions with detail coefficients exactly zero to be masked out
detail_coeffs = detail_coeffs[np.nonzero(detail_coeffs)]

if distribution.lower() == 'gaussian':
# 75th quantile of the underlying, symmetric noise distribution:
denom = scipy.stats.norm.ppf(0.75)
sigma = np.median(np.abs(detail_coeffs)) / denom
else:
raise ValueError("Only Gaussian noise estimation is currently "
"supported")
return sigma


def estimate_sigma(im, multichannel=False, average_sigmas=False):
"""
Robust wavelet-based estimator of the (Gaussian) noise standard deviation.
Expand Down

0 comments on commit deb96a2

Please sign in to comment.