Sat, 14 Dec 2019 23:56:21 +0200
Small possible optimisations
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 | |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
16 | export simple_imfilter |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
17 | |
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 | # The routine |
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 | |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
22 | @inline function inside(i, aʹ, bʹ, a, b) |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
23 | return (max(a, i - aʹ) - i):(min(b, i + bʹ) - i) |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
24 | end |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
25 | |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
26 | function simple_imfilter(b::Array{Float64,2}, |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
27 | kernel::OffsetArray{Float64,2,Array{Float64,2}}) |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
28 | |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
29 | n, m = size(b) |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
30 | k, 𝓁 = size(kernel) |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
31 | o₁, o₂ = kernel.offsets |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
32 | a₁, a₂ = k + o₁, 𝓁 + o₂ |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
33 | b₁, b₂ = -1 - o₁, -1 - o₂ |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
34 | kp = kernel.parent |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
35 | |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
36 | @assert(isodd(k) && isodd(𝓁)) |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
37 | |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
38 | res = similar(b) |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
39 | |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
40 | Threads.@threads for i=1:n |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
41 | for j=1:m |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
42 | tmp = 0.0 |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
43 | it₁ = inside(i, a₁, b₁, 1, n) |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
44 | it₂ = inside(j, a₂, b₂, 1, m) |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
45 | for p=it₁ |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
46 | @simd for q=it₂ |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
47 | 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
|
48 | end |
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 | res[i, j] = tmp |
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 |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
53 | |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
54 | return res |
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 | end # Module |
0d99f8f7d261
Added ImFilter & simple_imfilter.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
58 |