Skip to content

Wavelet denoising in YCbCr color space - #2240

Merged
sciunto merged 7 commits into
scikit-image:masterfrom
stsievert:wavelet-denoise-color-channels
Jan 18, 2017
Merged

sciunto merged 7 commits into
scikit-image:masterfrom
stsievert:wavelet-denoise-color-channels

Conversation

@stsievert

Copy link
Copy Markdown
Contributor

Description

This documents the process for doing denoising a color image in the YCbCr color space (aka only denoising the Y color channel).

This also shows the comparison between denoising in RGB and denoising in YCbCr. The example shown in the docs is shown below.

Checklist

[For detailed information on these and other aspects see scikit-image contribution guidelines]

Possible enhancements

  • @JDWarner denoising the Y channel in YCbCr doesn't appear to be a better denoising, at least when denoising by default.
  • Should we add an optional parameter to denoise_wavelet to denoise a color image in the YCbCr color space?

References

This is documentation was suggested by #2190 (comment)

Example added to docs

...but formatted differently

Imgur

@codecov-io

codecov-io commented Aug 10, 2016 •

Copy link
Copy Markdown

Current coverage is 90.69% (diff: 100%)

Merging #2240 into master will increase coverage by 0.01%

@@             master      #2240   diff @@
==========================================
  Files           304        304          
  Lines         21452      21476    +24   
  Methods           0          0          
  Messages          0          0          
  Branches       1842       1850     +8   
==========================================
+ Hits          19454      19478    +24   
  Misses         1658       1658          
  Partials        340        340          

Powered by Codecov. Last update c2198ad...5a36da2

@soupault soupault added 📄 type: Documentation Updates, fixes and additions to documentation ⏩ type: Enhancement Improve existing features labels Aug 10, 2016
@JDWarner

Copy link
Copy Markdown
Contributor

Hmm, I honestly thought this would look better. I've seen wavelet denoising do an amazing job at eliminating both luminance and color noise, so it's possible. None of these are quite the right approach, so I was wrong about YCbCr. @scikit-image/core any other suggestions?

@stsievert

Copy link
Copy Markdown
Contributor Author

In my previous implementation, I was only denoising the Y color plane. On my local machine (nothing pushed yet), I'm denoising separately for all 3 color planes, Cb and Cr included.

I'm unsure how to integrate this with specifying the noise variance; how does the noise variance change in each new color plane? That's definitely not global across different color planes.

In doing this, I can obtain the following result:

Imgur

@JDWarner is what you were expecting? The luminance is reduced (in the image above there's less red, in astronaut there's less orange, etc) but there is more detail.

@stsievert

Copy link
Copy Markdown
Contributor Author

I've added a keyword argument to denoise_wavelet that allows for denoising in different colorspaces. For color images by default this denoises in the YCbCr colorspace.

In doing this, I also allow the noise st.devs to be either a scalar or a list of noise levels in each color plane.

It is still possible to keep the same behavior of plain wavelet thresholding without any conversion to any colorspaces with colorspace=None.

Do we want to keep going down this path? It seems like this should be in every denoise function, but it also took me a while to figure how properly denoise an image in YCbCr and have it look good. Maybe a good halfway point is having an example in the documentation of YCbCr, but I'd like for that to be default.

@soupault

soupault commented Sep 8, 2016

Copy link
Copy Markdown
Member

@stsievert Hi!
Finally, we've get #2241 merged. Could you rebase on master, please, so we can see where we are with this PR? Thanks in advance!

@stsievert
stsievert force-pushed the wavelet-denoise-color-channels branch from e20ed08 to d839987 Compare September 24, 2016 18:21
@stsievert

Copy link
Copy Markdown
Contributor Author

@soupault I've rebased and have allowed edits from maintainers. I had to make some modifications to the if-statement logic to allow sensible denoising when the RGB colorspace is specified.

This commit is still a WIP; the code is not clean. If we decide we want to integrate, I can clean up.

I have verified that incorporating the changes still yields a correct result. The docs should reflect comparison between RGB and YCbCr denoising.

img = data.chelsea()[:450, :450]
img = img_as_float(img)

sigma = 0.8*img.std()
noisy = util.random_noise(img, var=sigma**2)

denoised = [('original', img), ('noisy' , noisy),
            ('ycbcr' , denoise_wavelet(noisy)),
            ('rgb' , denoise_wavelet(noisy, colorspace='rgb'))]

comparison

@soupault

Copy link
Copy Markdown
Member

@stsievert very nice results!
Regarding the PR, although, I have some concerns.

  1. From my quick thinking, for temporary colorspace conversion some kind of context manager could be a better solution (i.e. with convert_to_cs(img, cs_src, cs_dst) as img_tmp: ...).
  2. If we choose to follow your approach, we need to expose two parameters: cspace_img (input image colorspace, defaults to RGB) and cspace_filt (colorspace we will apply filtering in, defaults to YCbCr) to be able to return filtered image in the original colorspace.
  3. The simplest approach will be to perform filtering for multichannel images with rgb2ycbcr by default, giving a user the possibility only to disable the conversion via kwarg. For any specific cases user will be obliged to perform the conversion by themselfves. However, I've heard that people apply wavelet filtering also in CIELAB, so, I suppose, we should be more flexible here and use one of the alternatives above instead.

Whichever way we choose to go in, any general purpose color conversion functions/context managers/whatever have to be put under skimage.color.

@scikit-image/core more opinions on this?

@jni

jni commented Sep 27, 2016

Copy link
Copy Markdown
Member

To my eyes, the RGB denoising looks better than the YCbCr! Is it just my inexperienced visual system playing tricks on me? I would improve this PR by changing the docstring part that says "denoising is often done in YCbCr" to an actual justification + reference as to why that would be the case.

As to @soupault's suggestions/questions:

  • I think a colorspace context manager is a little too heavyweight for this purpose.
  • I'm not sure this is the right answer, but we have precedent for default processing colorspace in slic's convert2lab=None. ref I think we should go this way (convert2ycbcr=True) because setting the precedent that skimage functions could expect images in any colorspace is quite a big can of worms. So a mild -1 from me on cspace_img or variants.

@JDWarner

JDWarner commented Sep 28, 2016 •

Copy link
Copy Markdown
Contributor

Look again, @jni - this is an excellent and compelling example. The goal isn't maximizing the amount of noise removed, it's addressing the color accuracy problems introduced by denoising. YCbCr does this beautifully; RGB keeps a significant amount of incorrect chroma as well as preserves much less texture/detail.

When you denoise, you need to tailor your methods to your noise. The way this example is structured, we add a lot of chroma noise. In other words, the noise is random and uncorrelated between channels, That gives you the color mishmash shown in the raw noisy image. Another example might deal exclusively with luminance noise, but that's out of the scope of this PR. YCbCr is much better than RGB for chroma noise.

The result for YCbCr is true to the original image's color, and that's the point of the example. RGB denoising, when turned up high enough, can sort of smear this over at the expense of details - but it never quite works, color accuracy always suffers, and you lose both texture and detail. RGB never gets as good as YCbCr. Look at them both at 100-200%, comparing the out-of-focus bokeh areas, and you'll see this.

Because it's better suited to this noise, YCbCr is also preserving more detail at the same denoising strength. If you thought the best result would be the smoothest, RGB might at first glance appear ahead - but it isn't.

@soupault - Assumptions are dangerous. Multichannel images are not necessarily RGB[A], consider NASA cameras at arbitrary wavelengths as well as medical images. Users should be informed how this would best be used, examples go a long way, but ultimately they are responsible to make sure their data is preprocessd appropriately. Context managers for this kind of thing quickly lead toward pipeline workflows. VTK and similar use these, and they have benefits, but that philosophy is not at all easily integrated into scikit-image given the choices we've made to date. That doesn't mean it's wrong; we could discuss how that would look, but it's out of scope for this PR. Feel free to raise that as a separate Issue (or Project) for consideration, but let's keep the discussion narrowly focused on the example here.

Finally, as far as my review is concerned: There are a few minor test suite complaints which should be simple to address.

@jni

jni commented Sep 29, 2016

Copy link
Copy Markdown
Member

@JDWarner thanks for the write-up. After a second pass I agree with you, and notice the aberrations introduced by the RGB denoising. I fell for the old "crank up the TV saturation in the store" trick. =) Nevertheless, I think this would be valuable information to put in the docstring, perhaps in slightly more condensed form. =) And with a reference! I actually have no idea what the YCbCr colorspace is.

@stsievert

stsievert commented Oct 14, 2016 •

Copy link
Copy Markdown
Contributor Author

I'm also hesitant to include my current approach: this method is better suited for a context manager and I don't want to implement more than I need to. At first, I was only planning on mentioning this in the doc examples but made more changes because I ran into issues with converting and 0/1 scaling in the skimage workflow.

Sounds like we've settled on convert2ycbcr. I'll make it default to False, as I'd rather know exactly what's happening to my data and to avoid black boxes.

But looking at skimage.segmentation.slic, they have convert2lab be True by default for appropriate sized inputs. The docstring of this says "clustering in Color-(x,y,z) space". Wavelet denoising makes no assumptions about any colorspace, and I'm still inclined to have convert2ycbcr default to False.

@soupault soupault changed the title DOC: shows wavelet denoising in YCbCr color space (WIP) Wavelet denoising in YCbCr color space Oct 25, 2016
@soupault

soupault commented Nov 2, 2016

Copy link
Copy Markdown
Member

I think we should go this way (convert2ycbcr=True) because setting the precedent that skimage functions could expect images in any colorspace is quite a big can of worms.

@jni I actually think that it is better to have these things explicit. Keeping our images in pure numpy-arrays doesn't mean that we have to avoid the idea of metadata handling. With the current state of affairs, either we should have all functions taking img, metadata (where metadata is some kind of dict or list of previous operations applied on img) as a first pair of arguments, or we have to expose those keys of metadata as a separate parameters.
This could lead us close to the "hell" similar to the signature of http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html, but is actually a way to write clear, explicit, highly-configurable image processing functions without opening a "can of warnings and unhandled cases." 😉

@stsievert I've given this one more round, and I'd agree with your proposal to have convert2ycbcr parameter with False by default. Well, unless the roundtrip conversion is non-destructive (i.e. img == ycbcr2rgb(rgb2ycbcr(img))), which is not the case, I suppose.
And, I guess, we drop _rgb2colorspace(img, space) and the inverse function for now.

@jni

jni commented Nov 5, 2016

Copy link
Copy Markdown
Member

@soupault Yes, I am actually (mostly) a fan of (img, md) tuples as a standard thing in scikit-image. But imho that needs to be implemented in a giant sprint so that the interface is consistent throughout scikit-image, rather than in a PR about one specific feature. I think there's an issue open about metadata handling.

Comment thread skimage/restoration/_denoise.py Outdated
multichannel : bool, optional
Apply wavelet denoising separately for each channel (where channels
correspond to the final axis of the array).
convert2ycbcr : bool, options

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.

optional

Comment thread skimage/restoration/_denoise.py Outdated
convert2ycbcr : bool, options
If True do the wavelet denoising in the YCbCr colorspace (typically)
instead of the RGB color space. This typically results in better
performance for RGB images (default: False).

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, remove (default: False)

Comment thread skimage/restoration/_denoise.py Outdated
[0, 1] depending on the input image range.

Wavelet denoising is often done in the YCbCr colorspace. It is assumed the
input image is in RGB. If ``colorspace in {None, 'rgb'}``, no colorspace

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.

There is no such parameter anymore.

Comment thread skimage/restoration/_denoise.py Outdated
out = _wavelet_threshold(img, wavelet=wavelet, mode=mode,
sigma=sigma, wavelet_levels=wavelet_levels)
if convert2ycbcr:
if type(sigma) in {float, int, complex}:

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.

Could we just do this once somewhere?

out = color.rgb2ycbcr(img)
for i in range(3):
# renormalizing this color channel to live in [0, 1]
min, max = out[..., i].min(), out[..., i].max()

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.

min, max are reserved built-ins. Please, use another variable names.

else:
out = _wavelet_threshold(img, wavelet=wavelet, mode=mode,
sigma=sigma, wavelet_levels=wavelet_levels)
if convert2ycbcr:

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.

Hmmm... Under else? Not a multichannel?

@soupault soupault left a comment

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.

More comments on the implementation.

@soupault soupault left a comment

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.

Let's reloop :)

Comment thread skimage/restoration/_denoise.py Outdated
mode=mode, sigma=sigma,
mode=mode, sigma=sigma[c],
wavelet_levels=wavelet_levels)
elif convert2ycbcr:

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 mean aren't we expecting multichannel image here?

if multichannel:
    if convert2ycbcr:
        ...
    else:
        ...
else:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If we're expecting multichannel, why enforce that it's specified? In the docs we could add that convert2ycbcr implies multichannel.

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.

"explicit is better than implicit". This way it is clearer for the user, and is easier for the developer to be refactored/deprecated in case of any architectural changes.

Comment thread doc/examples/filters/plot_denoise.py Outdated
The noise is represented by small values in the wavelet domain which are set to
0.

In color images, wavelet denoising is typically done in the YCbCr color space

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.

Comment thread skimage/restoration/_denoise.py Outdated
but it is cast into an ndarray of floats for the computation
of the denoised image.
sigma : float, optional
sigma : float, list, optional

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.

According to numpy docstring format is should be float or list.

Comment thread skimage/restoration/_denoise.py Outdated
correspond to the final axis of the array).
convert2ycbcr : bool, optional
If True and multichannel True, do the wavelet denoising in the YCbCr
colorspace (typically) instead of the RGB color space. This typically

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, remove (typically).

Comment thread skimage/restoration/_denoise.py Outdated
out[..., c] = _wavelet_threshold(img[..., c], wavelet=wavelet,
mode=mode, sigma=sigma,
wavelet_levels=wavelet_levels)
if multichannel or convert2ycbcr:

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.

if multichannel: should be enough.

@stsievert
stsievert force-pushed the wavelet-denoise-color-channels branch from ac02be9 to e5a510c Compare January 16, 2017 22:07
@stsievert

stsievert commented Jan 16, 2017 •

Copy link
Copy Markdown
Contributor Author

Here's the output of the function as is right now:

figure_1

I am getting a weird repeating errors with doc/examples/features_detection/plot_local_binary_pattern.py and doc/examples/transform/plot_pyramid.py.

@soupault

Copy link
Copy Markdown
Member

@stsievert I kindly ask you to rebase onto master.

@stsievert
stsievert force-pushed the wavelet-denoise-color-channels branch from 9432434 to 290dc58 Compare January 18, 2017 03:29
@soupault

Copy link
Copy Markdown
Member

@stsievert Perfect, thanks! The tests are passing.
If you could squash the commits now, it will be even better :).

Scott Sievert added 2 commits January 18, 2017 11:08
BUG: typo

BUG: fixes the test to test right sigma

BUG: removes long

BUG: test typo
logic change; nest convert2ycbcr if inside multichannel if

TST: remove test args -- no test perform

BUG: sigma list formation in correct place

also properly checks for type(sigma) == number

BUG: typo (number => numbers)

BUG with multichannel=True and sigma=None

BUG: forgot to pass multichannel
@stsievert
stsievert force-pushed the wavelet-denoise-color-channels branch 2 times, most recently from 12983d5 to 5a36da2 Compare January 18, 2017 17:25
BUG: removes `colorspace` kwarg from denoise_wavelet in examples

MAINT: either convert2ycbcr OR multichannel

BUG: syntax

BUG: syntax

MAINT: explicit is better than implicit

DOC: reformats docstrings
@soupault

Copy link
Copy Markdown
Member

@scikit-image/core one more person to review?

@sciunto

sciunto commented Jan 18, 2017

Copy link
Copy Markdown
Member

awesome. I merge. Thanks @stsievert and to the other reviewers.

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

Labels

⏩ type: Enhancement Improve existing features 📄 type: Documentation Updates, fixes and additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants