-
Notifications
You must be signed in to change notification settings - Fork 2
Add BM3D(sparse 3D transform-domain collaborative filtering) denoising algorithm. #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ae26de9
Add BM3D(sparse 3D transform-domain collaborative filtering) denoisin…
Longhao-Chen 322c545
Fix(BM3D): Data type.
Longhao-Chen 201c4eb
add example page(BM3D)
Longhao-Chen df649a2
Fix(BM3D): Data type
Longhao-Chen b9d7f59
Add regression test (BM3D)
Longhao-Chen 96d67a3
Merge remote-tracking branch 'upstream/master'
Longhao-Chen 4fd1864
Update docs/examples/reduce_noise/BM3D.jl
Longhao-Chen 66393aa
Make BM3DDenoise as a direct dependency.
Longhao-Chen c7f9cae
Update Project.toml
johnnychen94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # title: The BM3D(sparse 3D transform-domain collaborative filtering) denoising algorithm. | ||
| # id: bm3d-demo | ||
| # cover: assets/bm3d_cover.png | ||
| # date: 2021-08-04 | ||
| # author: Longhao Chen <Longhao.Chen@outlook.com> | ||
| # description: This demo shows how to use the BM3D denoising algorithm to reduce gaussian noise | ||
| # --- | ||
|
|
||
| using ImageNoise | ||
| using TestImages, ImageShow, ImageCore, ImageQualityIndexes, ImageTransformations | ||
| using FileIO, Random #src | ||
|
|
||
| # First, load an image and and add gaussian noise to it | ||
|
|
||
| gray_img = float.(imresize(testimage("cameraman"), ratio=0.5)) | ||
| n = AdditiveWhiteGaussianNoise(0.1) | ||
| noisy_img = apply_noise(gray_img, n) | ||
|
|
||
| # Then calling the standard `reduce_noise` API | ||
|
|
||
| f_bm3d = BM3D(0.1) | ||
| denoised_img = reduce_noise(noisy_img, f_bm3d) | ||
|
|
||
| mosaicview(gray_img, noisy_img, denoised_img; nrow=1) | ||
|
|
||
| # Get the PSNR using ImageQualityIndexes package: | ||
|
|
||
| assess_psnr(gray_img, denoised_img) | ||
|
|
||
| mkpath("assets") #src | ||
| save("assets/bm3d_cover.png", denoised_img) #src | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| using UUIDs | ||
| const BM3DDenoise = Base.PkgId(UUID("95fb3b36-088a-43fb-bb1b-b1f34fadbd7d"), "BM3DDenoise") | ||
|
|
||
| # Rewrite from ImageIO.jl | ||
| function checked_import(pkgid) | ||
| Base.root_module_exists(pkgid) && return Base.root_module(pkgid) | ||
| # If not available, load the library or throw an error. | ||
| Base.require(pkgid) | ||
| end | ||
|
|
||
|
|
||
| @doc raw""" | ||
| BM3D(σ [, config=bm3d_config()]) | ||
|
|
||
| The BM3D(sparse 3D transform-domain collaborative filtering) denoising algorithm. | ||
|
|
||
| !!! info | ||
|
|
||
| This feature depends on the package: `BM3DDenoise` | ||
|
|
||
| You can install it by `Pkg`: | ||
| ``` | ||
| using Pkg | ||
| Pkg.add("BM3DDenoise") | ||
| ``` | ||
|
johnnychen94 marked this conversation as resolved.
Outdated
|
||
|
|
||
| # Arguments | ||
|
|
||
| * `σ::Float64` is the variance of the noise. | ||
|
|
||
| * `config::bm3d_config` is `BM3DDenoise.bm3d_config`. | ||
|
|
||
| # Examples | ||
|
|
||
| ```julia | ||
| img = testimage("lena_color_256") | ||
|
|
||
| n = AdditiveWhiteGaussianNoise(0.1) | ||
| noisy_img = apply_noise(img, n) | ||
|
|
||
| # use default arguments | ||
| f_denoise = BM3D(0.1) | ||
| denoised_img = reduce_noise(noisy_img, f_denoise) | ||
| ``` | ||
|
|
||
| See also: [`reduce_noise`](@ref), [`reduce_noise!`](@ref) | ||
| """ | ||
| struct BM3D <: AbstractImageDenoiseAlgorithm | ||
| """degree of filtering""" | ||
| σ::Float64 | ||
| """bm3d_config""" | ||
| config | ||
| function BM3D(σ, config) | ||
| σ > 0 || @warn "σ is supposed to be positive" | ||
| new(σ, config) | ||
| end | ||
| end | ||
| BM3D(σ) = BM3D(σ, Base.invokelatest(checked_import(BM3DDenoise).bm3d_config)) | ||
|
|
||
| function (f::BM3D)(out::AbstractArray{T}, | ||
| img::AbstractArray) where T | ||
| axes(out) == axes(img) || ArgumentError("Images should have the same axes.") | ||
| out .= T.(Base.invokelatest(checked_import(BM3DDenoise).bm3d, img, f.σ, f.config)) | ||
| return out | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| @testset "BM3DDenoise" begin | ||
| @info "Test: BM3DDenoise" | ||
| @testset "Numeric" begin | ||
| img_gray = n0f8.(imresize(testimage("lena_gray_256"); ratio=0.25)) | ||
| n = AdditiveWhiteGaussianNoise(0.05) | ||
| noisy_img = apply_noise(img_gray, n; rng=MersenneTwister(0)) | ||
|
|
||
| f = BM3D(0.05) | ||
| denoised_img = reduce_noise(noisy_img, f) | ||
| # further modification shall not decrease psnr and ssim | ||
| @test assess(PSNR(), denoised_img, img_gray) >= 28. | ||
| @test assess(SSIM(), denoised_img, img_gray) >= 0.9 | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.