Thu, 05 Dec 2019 17:12:55 +0200
update deps
0 | 1 | ###################################### |
2 | # Image subpixel accuracy translation | |
3 | ###################################### | |
4 | ||
7
ab7d59b47140
Add __precompile__() for whatever it is worth
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
5 | __precompile__() |
ab7d59b47140
Add __precompile__() for whatever it is worth
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
6 | |
0 | 7 | module Translate |
8 | ||
9 | ########## | |
10 | # Exports | |
11 | ########## | |
12 | ||
13 | export interpolate2d, | |
14 | interpolate2d_quadrants, | |
15 | extract_subimage!, | |
16 | translate_image!, | |
17 | DisplacementFull, | |
18 | DisplacementConstant, | |
19 | Displacement, | |
20 | Image | |
21 | ||
22 | ################## | |
23 | # Types | |
24 | ################## | |
25 | ||
26 | # Two different types of displacement data supported: | |
27 | # a) given in each pixel | |
28 | # b) constant in space | |
29 | Image = Array{Float64,2} | |
30 | DisplacementFull = Array{Float64,3} | |
31 | DisplacementConstant = Array{Float64,1} | |
32 | Displacement = Union{DisplacementFull,DisplacementConstant} | |
33 | ||
34 | ############################# | |
35 | # Base interpolation routine | |
36 | ############################# | |
37 | ||
38 | @inline function interpolate2d_quadrants(v, (x, y)) | |
39 | (m, n) = size(v) | |
40 | clipx = xʹ -> max(1, min(xʹ, m)) | |
41 | clipy = yʹ -> max(1, min(yʹ, n)) | |
42 | ||
43 | xfℤ = clipx(floor(Int, x)) | |
44 | xcℤ = clipx(ceil(Int, x)) | |
45 | yfℤ = clipy(floor(Int, y)) | |
46 | ycℤ = clipy(ceil(Int, y)) | |
47 | ||
48 | xf = convert(Float64, xfℤ) | |
49 | xc = convert(Float64, xcℤ) | |
50 | yf = convert(Float64, yfℤ) | |
51 | yc = convert(Float64, ycℤ) | |
52 | xm = (xf+xc)/2 | |
53 | ym = (yf+yc)/2 | |
54 | ||
55 | vff = @inbounds v[xfℤ, yfℤ] | |
56 | vfc = @inbounds v[xfℤ, ycℤ] | |
57 | vcf = @inbounds v[xcℤ, yfℤ] | |
58 | vcc = @inbounds v[xcℤ, ycℤ] | |
59 | vmm = (vff+vfc+vcf+vcc)/4 | |
60 | ||
61 | if xfℤ==xcℤ | |
62 | if yfℤ==ycℤ | |
63 | # Completely degenerate case | |
64 | v = vmm | |
65 | else | |
66 | # Degenerate x | |
67 | v = vff+(y-yf)/(yc-yf)*(vfc-vff) | |
68 | end | |
69 | elseif yfℤ==ycℤ | |
70 | # Degenerate y | |
71 | v = vff + (x-xf)/(xc-xf)*(vcf-vff) | |
72 | elseif y-ym ≥ x-xm | |
73 | # top-left half | |
74 | if (y-ym) + (x-xm) ≥ 0 | |
75 | # top quadrant | |
76 | v = vfc + (x-xf)/(xc-xf)*(vcc-vfc) + (y-yc)/(ym-yc)*(vmm-(vcc+vfc)/2) | |
77 | else | |
78 | # left quadrant | |
79 | v = vff + (y-yf)/(yc-yf)*(vfc-vff) + (x-xf)/(xm-xf)*(vmm-(vfc+vff)/2) | |
80 | end | |
81 | else | |
82 | # bottom-left half | |
83 | if (y-ym) + (x-xm) ≥ 0 | |
84 | # right quadrant | |
85 | v = vcf + (y-yf)/(yc-yf)*(vcc-vcf) + (x-xc)/(xm-xc)*(vmm-(vcc+vcf)/2) | |
86 | else | |
87 | # bottom quadrant | |
88 | v = vff + (x-xf)/(xc-xf)*(vcf-vff) + (y-yf)/(ym-yf)*(vmm-(vcf+vff)/2) | |
89 | end | |
90 | end | |
91 | ||
92 | return v | |
93 | end | |
94 | ||
95 | ############## | |
96 | # Translation | |
97 | ############## | |
98 | ||
99 | @polly function translate_image!(x, z, u::DisplacementFull) | |
100 | @assert(size(u, 1)==2 && size(x)==size(u)[2:end] && size(x)==size(z)) | |
101 | ||
102 | @inbounds @simd for i=1:size(x, 1) | |
103 | @simd for j=1:size(x, 2) | |
104 | pt = (i - u[1, i, j], j - u[2, i, j]) | |
6
cd3ca6286daa
For inlining to work we cannot do function assignments
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
105 | x[i, j] = interpolate2d_quadrants(z, pt) |
0 | 106 | end |
107 | end | |
108 | end | |
109 | ||
110 | @polly function translate_image!(x, z, u::DisplacementConstant) | |
111 | @assert(size(u)==(2,) && size(x)==size(z)) | |
112 | ||
113 | @inbounds @simd for i=1:size(x, 1) | |
114 | @simd for j=1:size(x, 2) | |
115 | pt = (i - u[1], j - u[2]) | |
6
cd3ca6286daa
For inlining to work we cannot do function assignments
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
116 | x[i, j] = interpolate2d_quadrants(z, pt) |
0 | 117 | end |
118 | end | |
119 | end | |
120 | ||
121 | ###################### | |
122 | # Subimage extraction | |
123 | ###################### | |
124 | ||
125 | @polly function extract_subimage!(b, im, v::DisplacementConstant) | |
126 | (imx, imy) = size(im) | |
127 | (bx, by) = size(b) | |
128 | ||
129 | # Translation from target to source coordinates | |
2 | 130 | vxʹ = (imx-bx)/2 - v[1] |
131 | vyʹ = (imy-by)/2 - v[2] | |
0 | 132 | |
133 | # Target image indices within source image | |
134 | px = ceil(Int, max(1, vxʹ + 1) - vxʹ) | |
135 | py = ceil(Int, max(1, vyʹ + 1) - vyʹ) | |
136 | qx = floor(Int, min(imx, vxʹ + bx) - vxʹ) | |
137 | qy = floor(Int, min(imy, vyʹ + by) - vyʹ) | |
138 | ||
139 | b .= 0 | |
140 | ||
141 | @inbounds @simd for i=px:qx | |
142 | for j=py:qy | |
6
cd3ca6286daa
For inlining to work we cannot do function assignments
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
143 | b[i, j] = interpolate2d_quadrants(im, (i+vxʹ, j+vyʹ)) |
0 | 144 | end |
145 | end | |
146 | end | |
147 | ||
5
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
148 | ##################################################### |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
149 | # Precompilation hints to speed up compilation time |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
150 | # for projects depending on this package (hopefully). |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
151 | ###################################################### |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
152 | |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
153 | precompile(translate_image!, (Array{Float64,2}, Array{Float64,2}, Array{Float64,1})) |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
154 | precompile(translate_image!, (Array{Float64,2}, Array{Float64,2}, Array{Float64,3})) |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
155 | precompile(extract_subimage!, (Array{Float64,2}, Array{Float64,2}, Array{Float64,1})) |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
156 | |
0 | 157 | end |