src/ImFilter.jl

Wed, 25 Dec 2019 17:42:32 +0200

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 25 Dec 2019 17:42:32 +0200
changeset 28
d1f40f6654cb
parent 22
4403f0445814
child 29
05b11c96ef45
permissions
-rw-r--r--

denoise_fista

18
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 ##################################################
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2 # Simple (and fast for small filters compared to
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 # ImageFiltering) image filtering
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4 ##################################################
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6 __precompile__()
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8 module ImFilter
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10 using OffsetArrays
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 ##########
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 # Exports
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 ##########
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15
22
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
16 export simple_imfilter,
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
17 gaussian
18
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19 ##############
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 # The routine
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 ##############
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 @inline function inside(i, aʹ, bʹ, a, b)
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 return (max(a, i - aʹ) - i):(min(b, i + bʹ) - i)
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 end
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 function simple_imfilter(b::Array{Float64,2},
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 kernel::OffsetArray{Float64,2,Array{Float64,2}})
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 n, m = size(b)
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 k, 𝓁 = size(kernel)
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 o₁, o₂ = kernel.offsets
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 a₁, a₂ = k + o₁, 𝓁 + o₂
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 b₁, b₂ = -1 - o₁, -1 - o₂
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 kp = kernel.parent
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 @assert(isodd(k) && isodd(𝓁))
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39 res = similar(b)
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 Threads.@threads for i=1:n
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 for j=1:m
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43 tmp = 0.0
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 it₁ = inside(i, a₁, b₁, 1, n)
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 it₂ = inside(j, a₂, b₂, 1, m)
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 for p=it₁
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 @simd for q=it₂
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48 tmp += @inbounds(kp[p-o₁, q-o₂]*b[i+p,j+q])
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49 end
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 end
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51 res[i, j] = tmp
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52 end
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 end
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55 return res
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 end
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57
22
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
58 ######################################################
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
59 # Distributions. Just to avoid the long load times of
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
60 # ImageFiltering and heavy dependencies on FFTW etc.
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
61 ######################################################
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
62
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
63 function gaussian(σ, n)
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
64 @assert(all(isodd.(n)))
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
65 a=convert.(Integer, @. (n-1)/2)
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
66 g=OffsetArray{Float64}(undef, [-m:m for m in a]...);
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
67 for i in CartesianIndices(g)
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
68 g[i]=exp(-sum(Tuple(i).^2 ./ (2 .* σ.^2)))
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
69 end
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
70 g./=sum(g)
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
71 end
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
72
18
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
73 end # Module
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
74

mercurial