src/ImFilter.jl

Fri, 23 Oct 2020 17:57:50 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Fri, 23 Oct 2020 17:57:50 -0500
changeset 44
2f123527b499
parent 32
41d13bf7d637
child 48
1b9e90ca81e3
permissions
-rw-r--r--

update packages

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
32
41d13bf7d637 Use @threadsif from AlgTools
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
11 using AlgTools.Util: @threadsif
18
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 ##########
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 # Exports
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 ##########
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16
22
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
17 export simple_imfilter,
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
18 gaussian
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 ##############
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 # The routine
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
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 @inline function inside(i, aʹ, bʹ, a, b)
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 return (max(a, i - aʹ) - i):(min(b, i + bʹ) - i)
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 end
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 function simple_imfilter(b::Array{Float64,2},
32
41d13bf7d637 Use @threadsif from AlgTools
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
29 kernel::OffsetArray{Float64,2,Array{Float64,2}};
41d13bf7d637 Use @threadsif from AlgTools
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
30 threads::Bool=true)
18
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 n, m = size(b)
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 k, 𝓁 = size(kernel)
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 o₁, o₂ = kernel.offsets
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 a₁, a₂ = k + o₁, 𝓁 + o₂
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 b₁, b₂ = -1 - o₁, -1 - o₂
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 kp = kernel.parent
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 @assert(isodd(k) && isodd(𝓁))
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 res = similar(b)
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42
32
41d13bf7d637 Use @threadsif from AlgTools
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
43 @threadsif threads for i=1:n
30
0b25d9ef7af9 Better @inbounds placements etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 29
diff changeset
44 @inbounds for j=1:m
18
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 tmp = 0.0
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 it₁ = inside(i, a₁, b₁, 1, n)
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 it₂ = inside(j, a₂, b₂, 1, m)
30
0b25d9ef7af9 Better @inbounds placements etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 29
diff changeset
48 for p=it₁
18
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49 @simd for q=it₂
29
05b11c96ef45 safeguards
Tuomo Valkonen <tuomov@iki.fi>
parents: 22
diff changeset
50 tmp += kp[p-o₁, q-o₂]*b[i+p,j+q]
18
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51 end
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52 end
30
0b25d9ef7af9 Better @inbounds placements etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 29
diff changeset
53 res[i, j] = tmp
18
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54 end
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55 end
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 return res
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58 end
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59
22
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
60 ######################################################
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
61 # 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
62 # 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
63 ######################################################
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
64
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
65 function gaussian(σ, n)
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
66 @assert(all(isodd.(n)))
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
67 a=convert.(Integer, @. (n-1)/2)
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
68 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
69 for i in CartesianIndices(g)
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
70 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
71 end
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
72 g./=sum(g)
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
73 end
4403f0445814 Add gaussian distr to avoid hevy ImageFiltering dependencies.
Tuomo Valkonen <tuomov@iki.fi>
parents: 18
diff changeset
74
18
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
75 end # Module
0d99f8f7d261 Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
76

mercurial