Skip to content

Implements wavelet denoising - #1833

Closed
stsievert wants to merge 17 commits into
scikit-image:masterfrom
stsievert:wavelet-denoise
Closed

stsievert wants to merge 17 commits into
scikit-image:masterfrom
stsievert:wavelet-denoise

Conversation

@stsievert

Copy link
Copy Markdown
Contributor

This pull request implements wavelet denoising (as mentioned in issue #602). This pull request uses PyWavelets to implement this.

  • add tests to cover 100% of code (tested each optional argument)
  • add example to gallery (added to plot_denoise.py, currently untested b/c couldn't compile)
  • add reference to CONTRIBUTORS.txt (final step to avoid merge conflicts)
  • Only allow specifying noise variance. There's a paper by Donoho et al that specifies more detail.
  • Only compute wavelet transform once on image. I believe this requires renaming the helper function to something like _soft_threshold with an optional parameter for the threshold. If this parameter is None, we can estimate the threshold from the noise variance, similar to ENH: routine to estimate the noise standard deviation from an image #1837
  • Test! Also, reformat the tests for removal of optional argument

@grlee77 also had useful points

Running the example in docstring yields the following results:

figure_1

Code changes

  • added denoise_wavelet in _denoise.py
  • added PyWavelets to optional_requirements.txt.

Unsure about

  • clipping. Wavelet denoising may result in values that are beyond the max/min of normal images. A keyword argument for this has been added that clips between 0/1 for the skimage workflow -- is that the right approach?
  • sizing. There are cases where pywt.threshold returns an array of different size than the input (e.g., in my testing, for odd sized arrays when using db2). Currently, the code enforces that the output should be the same size as the image -- is that the right approach?
  • documentation formatting. I tried building the docs (typing make in docs/) but couldn't find any function definitions.
  • tests. How should tests be implemented for this?

@stefanv

stefanv commented Dec 20, 2015

Copy link
Copy Markdown
Member

Thanks for this contribution, Scott. We are in the middle of releasing 0.12, so please be patient for feedback, and ping us again if you haven't heard back within a week or so after the release. Thanks!

Comment thread skimage/restoration/_denoise.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need extra variable here? Remove im_type

@soupault soupault added the ⏩ type: Enhancement Improve existing features label Dec 20, 2015
Comment thread skimage/restoration/_denoise.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clip is never used in the code. Remove

@soupault

Copy link
Copy Markdown
Member

Hi @scottsievert !
Thanks for your work!
I've just added some recommendations on code improvement.

Regarding your questions:
clipping : it is mandatory to always return to correct image, so unless you have a better idea how to handle negative values, function has to always return image with all values within [0;1] / [-1:1] .

sizing : I guess you should investigate better into DWT. As far as I know, the most common implementation is based on /2 downsampling, so it is neccesary (unless another way is proposed) to pad the input image.

tests : you should add both basic tests (check if function returns error for >2D arrays, for example) and functional ones (test if result on some image from skimage returns the expected result). This is minimal set of tests we would like to see.

Regarding other points: you should also add example to gallery (check doc/examples/ directory).

@stsievert

Copy link
Copy Markdown
Contributor Author

@soupault Thanks for answering my questions! Can we assume our input images scaled from 0 to 1?

8997800 fixes many of these issues.

@soupault

Copy link
Copy Markdown
Member

@scottsievert yes, in accordance to scikit-image conventions, once you call im = img_as_float(im), you should consider you image as one in float numbers with values from interval [0;1] after convertion of unsigned dtype or [-1;1] for signed.

@alexdesiqueira

Copy link
Copy Markdown
Member

Hi guys, this interests me a lot! Thanks @scottsievert for that!
About one of @soupault hints:

  • yes, the most used discrete wavelet algorithm uses downsampling by 2. You could use np.pad for padding. Here is how I solved that issue on my input images:
    # mirroring parameter: lower pixel number
    if (m < n):
        par = m
    else:
        par = n

    # mirroring
    aux_aprx = np.pad(img, (par, par), 'symmetric')

Thanks again! Have a nice one!

@JDWarner

Copy link
Copy Markdown
Contributor

I'm traveling but want to clarify a little here.

There is absolutely a place for optionally allowing values outside [0, 1] in this package. See for example the implementation of skimage.util.random_noise. This is similar; I recommend retaining the kwarg following those conventions.

Clipping should probably be the default, but it should be possible to disable for advanced users who want to do further work without throwing away data.

@grlee77

grlee77 commented Dec 20, 2015

Copy link
Copy Markdown
Contributor

A couple of comments on a few implementation details:

  • If you are intending to apply a hard threshold, you need to pass mode='hard' to pywt.threshold. The default is soft thresholding.
  • It would be nice if rather than being forced to manually specify a threshold, some simple common threshold estimates from the literature (e.g. VisuShrink, BayesShrink, or SUREShrink) could be implemented.

Also, there are substantial improvements in the upcoming release of PyWavelets that could be of use here (the current plan is to release v0.4.0 on Dec 28).

  • 2D and nD wavelet transforms are MUCH (up to two orders of magnitude) faster than in v0.3.0.
  • I don't think you will have to worry about the sizing issues anymore. These should be automatically handled by the wavedec/waverec functions now.
  • There are now n-dimensional versions of the multilevel transforms: wavedecn/waverecn, so this wouldn't have to be restricted to 2D/2D+color images.

@soupault

Copy link
Copy Markdown
Member

@grlee77 thank you very much for the comment! Version 0.4.0 sounds like a quite major update.
I guess it is better for us to target 0.4.0 so as the PR, in any case, requires some work to be done.

@stsievert

Copy link
Copy Markdown
Contributor Author

@grlee77 BayesShrink implemented as explained in (1) -- thanks for the reference. I have also removed the keyword argument enforce_size (and clip too).

Comment thread skimage/restoration/_denoise.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you should move function definition outside to avoid overhead during a batch processing.

@emmanuelle

Copy link
Copy Markdown
Member

Hi @scottsievert thanks for this useful addition! Could you please write an example for the gallery using wavelet denoising?

Also, there is some "staircasing" in the example that you showed, which is a bit surprising. I could have expected staircasing with Haar wavelet but I reckon that have used Daubechies wavelets. Is there a way to make it look better in the example?

@alexdesiqueira

Copy link
Copy Markdown
Member

@scottsievert @emmanuelle I think you could show two examples on that,
maybe with a lower and a higher order Daubechies wavelet. What do you think
about it?
On Dec 21, 2015 8:14 AM, "Emmanuelle Gouillart" [email protected]
wrote:

Hi @scottsievert https://github.com/scottsievert thanks for this useful
addition! Could you please write an example for the gallery using wavelet
denoising?

Also, there is some "staircasing" in the example that you showed, which is
a bit surprising. I could have expected staircasing with Haar wavelet but I
reckon that have used Daubechies wavelets. Is there a way to make it look
better in the example?

—
Reply to this email directly or view it on GitHub
#1833 (comment)
.

@stsievert

Copy link
Copy Markdown
Contributor Author

@emmanuelle The BayesShrink helps with the "stair stepping." In the new implementation, you can (optionally) specify a noise standard deviation. Here's the output (after slightly tuning the noise standard deviation, not the threshold)

figure_1

Comment thread skimage/restoration/_denoise.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo here: img here should be im

Comment thread skimage/restoration/_denoise.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please enumerate these references.

@soupault

Copy link
Copy Markdown
Member

@scottsievert hi!
Regarding the point in the header:

generalize to n dimensional wavelet transforms (not doing it on each color plane)

I believe n-dimensionality is considered as generalization in the sense of 1D (timeseries), 2D (images) and 3D (videos) cases. Color channels form a "special" dimension and usually should be processed separately.

@JDWarner

Copy link
Copy Markdown
Contributor

@soupault Not exactly. You're correct that color channels are a special case, but let's keep time out of this. For reference, the progression for n-dimensional data is best understood from 0-3 spatial dimensions where

0: Single point
1: Line
2: Plane
3: Volume

Note that n-dimensional analysis almost never mixes units. As we increased the dimensionality, every added dimension was orthogonal to the prior dimensions and and also carried the same spatial units. When you throw time into the mix things get treated very differently (time series analysis, etc.).

@stsievert

Copy link
Copy Markdown
Contributor Author

@JDWarner @soupault So would this be just doing this for each dimension separately (using wavedec2? I envisioned taking an n-dimensional wavelet transform with wavedecn.

tests every optional argument, in the only manner I could think of. All
tests rely on there being less energy in the signal after soft
thresholding.
@stsievert

Copy link
Copy Markdown
Contributor Author

Okay, tests and examples added.

@soupault soupault added this to the 0.13 milestone Mar 8, 2016
@soupault

soupault commented Mar 8, 2016

Copy link
Copy Markdown
Member

@stsievert thanks a lot! I think we should plan this and other wavelets-related issues/PRs for skimage==0.13.
BTW, I've found this old PR on the same topic: #760 . Scott, could you, please, take a look and hopefully transfer several enhancements from it?

@stsievert

Copy link
Copy Markdown
Contributor Author

That PR for soft or hard thresholding, and has an array of thresholds for each wavelet level. I've implemented soft/hard thresholding but am ignoring a list of different coefficients. In the problem formulations I've seen, when it's known that the wavelet coefficients have at most k non-zeros, it's optimal to choose a constant threshold.

Scott Sievert added 2 commits March 19, 2016 09:27
implemented _wavelet_threshold. denoise_wavelet is just a thin wrapper
around wavelet_threshold
db3, db4, haar}``.
noise_std : float, optional
The noise is estimated when noise_std is None (the default). It does
this as mentioned in pull request #1837.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to keep a reference to a PR in a docstring (even for a hiden function)? I think the information can be summarized in a Notes section.

JDWarner added a commit that referenced this pull request Aug 5, 2016
ENH: Implements wavelet denoising (from #1833)
@soupault

soupault commented Aug 5, 2016

Copy link
Copy Markdown
Member

Closed in advance of #2190.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants