Wavelet thresholding¶
| Fact | Value |
|---|---|
| Also called | Wavelet shrinkage denoising, Haar wavelet thresholding, Daubechies wavelet filtering, Symlet wavelet filtering, Coiflet wavelet filtering, Wavelet hard thresholding, Wavelet soft thresholding, VisuShrink, BayesShrink, SureShrink |
| Status | Implemented and tested |
| Family | Wavelet and multiscale filters |
| Engine | Science engine |
| Type | Nonlinear, transform, wavelet coefficient shrinkage |
| Output | Image |
| Operator shown as | transform (exact) |
| Support radius | global |
| Code | engine/src/pixelnexus_engine/filters/wavelets.py, wavelet_threshold |
| Operator code | engine/src/pixelnexus_engine/filters/wavelets.py, wavelet_threshold_representation |
| Packages | PyWavelets |
| Channels | RGB, Brightness only, Color only |
Overview¶
Wavelet thresholding removes Gaussian noise. It describes the image with wavelets, small waves at several sizes and in three directions. Noise spreads over many small wavelet coefficients, while edges and shapes make a few large ones. So the filter sets the small coefficients to zero, keeps the large ones, and rebuilds the image. One entry covers the classic variants: the Haar, Daubechies, Symlet and Coiflet wavelets, hard and soft thresholding, and the VisuShrink, BayesShrink and SureShrink rules for the threshold. The filter runs in the PixelNexus engine on your computer.
How it works¶
- Split each channel into a small low-pass copy and, at each of J levels, three detail images: horizontal, vertical and diagonal.
- Measure the noise from the finest diagonal details, unless you set it.
- Pick a threshold for each detail image with the chosen rule.
- Set the small detail coefficients to zero. With soft thresholding, also move the others toward zero by the threshold.
- Rebuild the channel from the low-pass copy and the changed details.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(I\): The input channel
- \(a_J\): The approximation after J levels
- \(h_j, v_j, d_j\): The horizontal, vertical and diagonal details of level j. Level 1 is the finest.
- \(w\): One detail coefficient
- \(\eta_T\): The shrink rule with threshold T
- \(T\): The threshold, in pixel values
- \(\sigma\): The standard deviation of the noise
- \(N\): The number of pixels
- \(n_b, \sigma_{x,b}\): The number of coefficients in subband b, and the estimated deviation of the signal in it
- \(t^{S}_b\): The SURE threshold of subband b in units of sigma
The discrete wavelet transform splits the image into a coarse approximation a_J and, at each level j, horizontal, vertical and diagonal details. An orthogonal wavelet transform turns white Gaussian noise of standard deviation sigma into white noise of the same sigma in every subband, away from the image borders. Edges and textures make a few large coefficients. So small coefficients are mostly noise. The shrink rule eta_T sets them to 0. When sigma is 0, it is estimated from the diagonal details d_1 of the finest level. The estimate is their median absolute value divided by 0.6745, the 75th percentile of the standard normal distribution. Coefficients that are exactly 0 are left out. VisuShrink uses the universal threshold of Donoho and Johnstone, where N is the number of pixels. BayesShrink, after Chang, Yu and Vetterli, divides sigma^2 by the signal deviation sigma_x,b of each subband b. That is the square root of the subband variance minus sigma^2. If the difference is not positive, the subband is set to 0. SureShrink, after Donoho and Johnstone, picks a threshold t^S_b for each subband, in units of sigma. It minimizes Stein's unbiased risk estimate of soft thresholding, from 0 to sqrt(2 ln n_b). A sparse subband gets the universal threshold of its size instead. The approximation a_J is kept.
Mallat showed how a wavelet transform splits a signal level by level with a pair of filters, a low-pass and a high-pass, each followed by keeping every second sample. In 2-D, rows and columns are filtered in turn, so each level gives three detail subbands. Daubechies built orthogonal wavelets with short filters. For an orthogonal transform, the energy of the image equals the energy of its coefficients, and noise keeps its strength in every subband.
Donoho and Johnstone proved that the universal threshold sigma sqrt(2 ln N) removes pure noise with a probability that tends to 1 as N grows. That makes VisuShrink smooth but often too smooth for images. Donoho showed that soft thresholding at this level gives a result that is, with high probability, at least as smooth as the true signal. SureShrink chooses the threshold of each subband by Stein's unbiased risk estimate, an estimate of the mean squared error that needs no clean image. BayesShrink models the coefficients of each subband with a generalized Gaussian distribution. Chang, Yu and Vetterli found that sigma^2 / sigma_x is close to the threshold that minimizes the Bayesian risk of soft thresholding for that model.
Kernel or operator¶
The Inspector asks the engine for the stages of the filter with the current settings. They are the analysis with the filter taps of the chosen wavelet, the noise estimate, the threshold rule, the shrink rule and the synthesis. The engine builds them with the code that runs the filter.
| Wavelet | Taps per filter | Vanishing moments |
|---|---|---|
| haar | 2 | 1 |
| db2 | 4 | 2 |
| db4 | 8 | 4 |
| db8 | 16 | 8 |
| sym4 | 8 | 4 |
| sym8 | 16 | 8 |
| coif1 | 6 | 2 |
| coif3 | 18 | 6 |
The taps and vanishing moments as PyWavelets reports them. The analysis filters of the default sym4 wavelet, rounded to 4 decimals:
Algorithm¶
- Split the channel with the 2-D discrete wavelet transform into an approximation and J levels of horizontal, vertical and diagonal details.
- Estimate the noise sigma from the diagonal details of the finest level, unless it is set.
- Choose a threshold for each detail subband with the threshold rule.
- Shrink every detail coefficient with the soft or hard rule. Keep the approximation.
- Rebuild the channel with the inverse transform and cut it to the image size.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Wavelet wavelet |
Symlet 4 (sym4) | Haar, Daubechies 2 (db2), Daubechies 4 (db4), Daubechies 8 (db8), Symlet 4 (sym4), Symlet 8 (sym8), Coiflet 1 (coif1), Coiflet 3 (coif3) | The orthogonal wavelet. Its filter taps come from PyWavelets. |
Levels levels |
3 | 1 to 8 | How many times the transform splits the low-pass part. It is capped at the deepest level the image size allows. |
Threshold rule rule |
BayesShrink | VisuShrink (universal), BayesShrink, SureShrink, Fixed threshold | How the threshold T is chosen. VisuShrink uses one threshold for all subbands. BayesShrink and SureShrink choose one for each subband. |
Shrink rule mode |
Soft | Soft, Hard | Soft thresholding sets small coefficients to 0 and moves the others toward 0 by T. Hard thresholding sets small coefficients to 0 and keeps the others. |
Noise sigma sigma |
0 | 0 to 100 | The standard deviation of the noise, in pixel values. 0 estimates it from the finest diagonal details. |
Threshold threshold |
20 | 0 to 255 | The threshold T in pixel values, for the fixed threshold rule only. |
Border mode border |
Reflect | Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap | How pixels outside the image are filled when the window reaches past the edge. |
Channels channelScope |
RGB | RGB, Brightness only, Color only | Which part of the color image the filter changes. |
Implementation¶
wavelet_threshold in engine/src/pixelnexus_engine/filters/wavelets.py runs wavelet_threshold_plane on each channel the scope names, through filter_by_scope in engine/src/pixelnexus_engine/planes.py, which follows the same color rules as the browser filters. PyWavelets computes the transform with wavedec2 and waverec2. The noise estimate (estimate_sigma), the thresholds (universal_threshold, bayes_threshold and sure_threshold) and the shrink rules (shrink) are PixelNexus code. The levels are capped at the deepest level that pywt.dwtn_max_level allows for the image size. The engine checks every value against the catalog before it runs the job. There is no live preview. The result arrives when you click Apply.
Worked example¶
| Threshold rule | Soft | Hard |
|---|---|---|
| VisuShrink | 26.31 | 28.88 |
| BayesShrink | 29.80 | 27.89 |
| SureShrink | 29.10 | 28.58 |
The PSNR in dB against the clean disks, 160 by 160 pixels, for the disks with Gaussian noise of sigma 20, 22.66 dB before the filter. The noise comes from NumPy's default_rng with seed 7 and is rounded to 8 bits. The other settings are the defaults: sym4, 3 levels and the estimated noise sigma.
| Threshold rule | T for h_1 | T for v_1 | T for d_1 |
|---|---|---|---|
| VisuShrink | 86.73 | 86.73 | 86.73 |
| BayesShrink | 51.16 | 50.32 | 171.41 |
| SureShrink | 80.93 | 80.93 | 80.93 |
The thresholds of the finest level. The estimated noise sigma is 19.25, close to the 20 that was added. VisuShrink uses one threshold for every subband, sigma sqrt(2 ln N) with N = 25600 pixels. BayesShrink gives each subband its own. Its threshold for d_1 is high, because the disks leave little signal in the finest diagonal details. SureShrink found all three finest subbands sparse, so its hybrid rule chose the universal threshold of their size, sigma sqrt(2 ln 6889).
With soft thresholding, BayesShrink gives the highest PSNR. VisuShrink keeps its large threshold on the coarsest level too, where the disks put most of their signal and the other rules use much lower thresholds. Soft thresholding also shrinks the edges it keeps, so VisuShrink blurs the most. With hard thresholding, VisuShrink gives the highest PSNR, since hard thresholding keeps the edges at full size.
Complexity¶
- Time: \(O(N L)\)
- Memory: \(O(N)\)
N is the number of pixels and L the number of filter taps. Each level works on a quarter of the pixels of the level before. SureShrink also sorts each subband, which adds O(N log N).
Artifacts and limitations¶
Limitations
- The rules assume white Gaussian noise of the same strength everywhere. Other noise, such as salt and pepper, is not removed well.
- The transform is not shift invariant, so the result changes when the image moves by one pixel. Stationary wavelet filtering removes this.
- Fine textures with small coefficients are removed along with the noise.
Artifacts
- Ringing and small wavelet-shaped spots near edges, where some noise coefficients pass the threshold.
- Blocky patches with the Haar wavelet.
- With hard thresholding, isolated spots where single noise coefficients survive.
Tests¶
The reference for VisuShrink and BayesShrink is denoise_wavelet of scikit-image, which uses the same thresholds. The tests compare it with the filter for 3 wavelets, 1 and 3 levels, and both shrink rules. They use the estimated noise and a given one. They match to within 10^-9. SureShrink has no reference in scikit-image, so a brute-force search over a fine grid of thresholds checks that the SURE threshold is the minimum. More tests check the noise estimate against estimate_sigma of scikit-image and the shrink rules against pywt.threshold. They check that a zero threshold gives the image back for every border mode, and they check the level cap, the color scopes and alpha. One test runs a full job through the engine API, with its parameter checks and its representation.
References¶
- Mallat, S. A theory for multiresolution signal decomposition. The wavelet representation. IEEE Transactions on Pattern Analysis and Machine Intelligence 11(7), 674 to 693, 1989. DOI 10.1109/34.192463.
- Daubechies, I. Orthonormal bases of compactly supported wavelets. Communications on Pure and Applied Mathematics 41(7), 909 to 996, 1988. DOI 10.1002/cpa.3160410705.
- Donoho, D. L., and Johnstone, I. M. Ideal spatial adaptation by wavelet shrinkage. Biometrika 81(3), 425 to 455, 1994. DOI 10.1093/biomet/81.3.425.
- Donoho, D. L. De-noising by soft-thresholding. IEEE Transactions on Information Theory 41(3), 613 to 627, 1995. DOI 10.1109/18.382009.
- Donoho, D. L., and Johnstone, I. M. Adapting to unknown smoothness via wavelet shrinkage. Journal of the American Statistical Association 90(432), 1200 to 1224, 1995. DOI 10.1080/01621459.1995.10476626.
- Chang, S. G., Yu, B., and Vetterli, M. Adaptive wavelet thresholding for image denoising and compression. IEEE Transactions on Image Processing 9(9), 1532 to 1546, 2000. DOI 10.1109/83.862633.
- Lee, G. R., Gommers, R., Waselewski, F., Wohlfahrt, K., and O'Leary, A. PyWavelets. A Python package for wavelet analysis. Journal of Open Source Software 4(36), 1237, 2019. DOI 10.21105/joss.01237.