Fri, 05 Jul 2024 14:48:02 -0500
Added tag v2.0.2 for changeset afeaa6f6d1ae
0 | 1 | ################################ |
2 | # Code relevant to optical flow | |
3 | ################################ | |
4 | ||
5 | __precompile__() | |
6 | ||
7 | module OpticalFlow | |
8 | ||
9 | using AlgTools.Util | |
10 | using ImageTools.Gradient | |
11 | import ImageTools.Translate | |
12 | using ImageTools.ImFilter | |
13 | ||
8
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
14 | # using ImageTransformations |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
15 | # using Images, CoordinateTransformations, Rotations, OffsetArrays |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
16 | # using Interpolations |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
17 | |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
18 | import Images: center, warp |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
19 | import CoordinateTransformations: recenter |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
20 | import Rotations: RotMatrix |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
21 | import Interpolations: Flat |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
22 | |
0 | 23 | ########## |
24 | # Exports | |
25 | ########## | |
26 | ||
27 | export flow!, | |
28 | pdflow!, | |
29 | flow_grad!, | |
30 | flow_interp!, | |
31 | estimate_Λ², | |
32 | estimate_linear_Λ², | |
33 | pointwise_gradiprod_2d!, | |
34 | pointwise_gradiprod_2dᵀ!, | |
35 | horn_schunck_reg_prox!, | |
36 | horn_schunck_reg_prox_op!, | |
37 | mldivide_step_plus_sym2x2!, | |
38 | linearised_optical_flow_error, | |
39 | Image, AbstractImage, ImageSize, | |
40 | Gradient, Displacement, | |
41 | DisplacementFull, DisplacementConstant, | |
42 | HornSchunckData, | |
8
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
43 | filter_hs, |
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
44 | petpdflow!, |
66
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
45 | DualScaling, Greedy, Rotation, ZeroDual, PrimalOnly, ActivatedDual, StrictGreedy, |
36 | 46 | identifier |
0 | 47 | |
48 | ############################################### | |
49 | # Types (several imported from ImageTools.Translate) | |
50 | ############################################### | |
51 | ||
52 | Image = Translate.Image | |
53 | AbstractImage = AbstractArray{Float64,2} | |
54 | Displacement = Translate.Displacement | |
55 | DisplacementFull = Translate.DisplacementFull | |
56 | DisplacementConstant = Translate.DisplacementConstant | |
57 | Gradient = Array{Float64,3} | |
58 | ImageSize = Tuple{Int64,Int64} | |
59 | ||
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
60 | |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
61 | ################################# |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
62 | # Struct for flow |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
63 | ################################# |
35 | 64 | struct DualScaling |
52
cb029cdb141a
activation function for dual scscaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
50
diff
changeset
|
65 | activation :: Function |
cb029cdb141a
activation function for dual scscaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
50
diff
changeset
|
66 | factor :: Float64 |
35 | 67 | threshold :: Real |
52
cb029cdb141a
activation function for dual scscaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
50
diff
changeset
|
68 | DualScaling(a = x -> x, f = 1.0, t = 1e-12) = new(a, f, t) |
35 | 69 | end |
70 | ||
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
71 | struct Greedy end |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
72 | struct Rotation end |
36 | 73 | struct ZeroDual end |
74 | struct PrimalOnly end | |
47
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
75 | struct ActivatedDual end |
66
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
76 | struct StrictGreedy end |
36 | 77 | |
78 | function identifier(::DualScaling) | |
79 | "pdps_known_dualscaling" | |
80 | end | |
81 | ||
82 | function identifier(::Rotation) | |
83 | "pdps_known_rotation" | |
84 | end | |
85 | ||
86 | function identifier(::Greedy) | |
87 | "pdps_known_greedy" | |
88 | end | |
89 | ||
90 | function identifier(::ZeroDual) | |
91 | "pdps_known_zerodual" | |
92 | end | |
93 | ||
94 | function identifier(::PrimalOnly) | |
95 | "pdps_known_primalonly" | |
96 | end | |
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
97 | |
47
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
98 | function identifier(::ActivatedDual) |
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
99 | "pdps_known_activateddual" |
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
100 | end |
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
101 | |
66
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
102 | function identifier(::StrictGreedy) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
103 | "pdps_known_strictgreedy" |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
104 | end |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
105 | |
0 | 106 | ################################# |
107 | # Displacement field based flow | |
108 | ################################# | |
109 | ||
110 | function flow_interp!(x::AbstractImage, u::Displacement, tmp::AbstractImage; | |
111 | threads = false) | |
112 | tmp .= x | |
113 | Translate.translate_image!(x, tmp, u; threads=threads) | |
114 | end | |
115 | ||
116 | function flow_interp!(x::AbstractImage, u::Displacement; | |
117 | threads = false) | |
118 | tmp = copy(x) | |
119 | Translate.translate_image!(x, tmp, u; threads=threads) | |
120 | end | |
121 | ||
122 | flow! = flow_interp! | |
123 | ||
124 | function pdflow!(x, Δx, y, Δy, u, dual_flow; threads=:none) | |
125 | if dual_flow | |
126 | #flow!((x, @view(y[1, :, :]), @view(y[2, :, :])), diffu, | |
127 | # (Δx, @view(Δy[1, :, :]), @view(Δy[2, :, :]))) | |
128 | @backgroundif (threads==:outer) begin | |
129 | flow!(x, u, Δx; threads=(threads==:inner)) | |
130 | end begin | |
131 | flow!(@view(y[1, :, :]), u, @view(Δy[1, :, :]); threads=(threads==:inner)) | |
132 | flow!(@view(y[2, :, :]), u, @view(Δy[2, :, :]); threads=(threads==:inner)) | |
133 | end | |
134 | else | |
135 | flow!(x, u, Δx) | |
136 | end | |
137 | end | |
138 | ||
36 | 139 | function pdflow!(x, Δx, y, Δy, z, Δz, u, dual_flow :: Bool; threads=:none) |
0 | 140 | if dual_flow |
141 | @backgroundif (threads==:outer) begin | |
142 | flow!(x, u, Δx; threads=(threads==:inner)) | |
143 | flow!(z, u, Δz; threads=(threads==:inner)) | |
144 | end begin | |
145 | flow!(@view(y[1, :, :]), u, @view(Δy[1, :, :]); threads=(threads==:inner)) | |
146 | flow!(@view(y[2, :, :]), u, @view(Δy[2, :, :]); threads=(threads==:inner)) | |
147 | end | |
148 | else | |
149 | flow!(x, u, Δx; threads=(threads==:inner)) | |
150 | flow!(z, u, Δz; threads=(threads==:inner)) | |
151 | end | |
152 | end | |
153 | ||
5 | 154 | # Additional method for Greedy |
66
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
155 | function pdflow!(x, Δx, y, Δy, u, flow :: Greedy, α; threads=:none) |
36 | 156 | @assert(size(u)==(2,)) |
5 | 157 | Δx .= x |
158 | Δy .= y | |
159 | flow!(x, u; threads=(threads==:inner)) | |
160 | Dxx = similar(Δy) | |
161 | DΔx = similar(Δy) | |
162 | ∇₂!(Dxx, x) | |
163 | ∇₂!(DΔx, Δx) | |
164 | inds = abs.(Dxx) .≤ 1e-1 | |
165 | Dxx[inds] .= 1 | |
166 | DΔx[inds] .= 1 | |
36 | 167 | y .= y.* DΔx ./ Dxx |
5 | 168 | end |
169 | ||
170 | # Additional method for Rotation | |
66
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
171 | function pdflow!(x, Δx, y, Δy, u, flow :: Rotation, α; threads=:none) |
5 | 172 | @assert(size(u)==(2,)) |
173 | Δx .= x | |
174 | flow!(x, u; threads=(threads==:inner)) | |
175 | (m,n) = size(x) | |
176 | dx = similar(y) | |
177 | dx_banana = similar(y) | |
178 | ∇₂!(dx, Δx) | |
179 | ∇₂!(dx_banana, x) | |
180 | for i=1:m | |
181 | for j=1:n | |
182 | ndx = @views sum(dx[:, i, j].^2) | |
183 | ndx_banana = @views sum(dx_banana[:, i, j].^2) | |
66
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
184 | if ndx > eps(1.0) |
5 | 185 | A = dx[:, i, j] |
186 | B = dx_banana[:, i, j] | |
187 | theta = atan(B[1] * A[2] - B[2] * A[1], B[1] * A[1] + B[2] * A[2]) # Oriented angle from A to B | |
188 | cos_theta = cos(theta) | |
189 | sin_theta = sin(theta) | |
190 | a = cos_theta * y[1, i, j] - sin_theta * y[2, i, j] | |
191 | b = sin_theta * y[1, i, j] + cos_theta * y[2, i, j] | |
192 | y[1, i, j] = a | |
193 | y[2, i, j] = b | |
66
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
194 | elseif ndx ≤ eps(1.0) && ndx_banana > eps(1.0) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
195 | y[:, i, j] .= α .* dx_banana[:, i, j] ./ sqrt(ndx_banana) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
196 | else |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
197 | y[:, i, j] .= 0 |
5 | 198 | end |
199 | end | |
200 | end | |
201 | end | |
202 | ||
203 | # Additional method for Dual Scaling | |
66
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
204 | function pdflow!(x, Δx, y, Δy, u, flow :: DualScaling, α; threads=:none) |
5 | 205 | @assert(size(u)==(2,)) |
206 | oldx = copy(x) | |
207 | flow!(x, u; threads=(threads==:inner)) | |
208 | C = similar(y) | |
209 | cc = abs.(x-oldx) | |
35 | 210 | cm = max(flow.threshold,maximum(cc)) |
52
cb029cdb141a
activation function for dual scscaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
50
diff
changeset
|
211 | c = 1.0 .- flow.factor.*flow.activation.(cc./cm) |
5 | 212 | C[1,:,:] .= c |
213 | C[2,:,:] .= c | |
36 | 214 | y .= C.*y |
5 | 215 | end |
216 | ||
66
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
217 | # Additional method for new strict TV preserving predictor a.k.a. Strict Greedy |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
218 | function pdflow!(x, Δx, y, Δy, u, flow :: StrictGreedy, α; threads=:none) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
219 | @assert(size(u)==(2,)) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
220 | Δx .= x |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
221 | flow!(x, u; threads=(threads==:inner)) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
222 | (m,n) = size(x) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
223 | dx = similar(y) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
224 | dx_banana = similar(y) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
225 | ∇₂!(dx, Δx) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
226 | ∇₂!(dx_banana, x) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
227 | Δy .= y |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
228 | for i=1:m |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
229 | for j=1:n |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
230 | ndx = @views sum(dx[:, i, j].^2) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
231 | ndx_banana = @views sum(dx_banana[:, i, j].^2) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
232 | if ndx > eps(1.0) && ndx_banana > eps(1.0) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
233 | A = dx[:, i, j] |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
234 | B = Δy[:, i, j] |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
235 | y[:, i, j] .= (dot(A,B)/(sqrt(ndx)*sqrt(ndx_banana))).* dx_banana[:, i, j] |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
236 | elseif ndx ≤ eps(1.0) && ndx_banana > eps(1.0) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
237 | A = ones(size(dx[:, i, j])...) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
238 | ndx = sum(A.^2) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
239 | B = Δy[:, i, j] |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
240 | y[:, i, j] .= (dot(A,B)/(sqrt(ndx)*sqrt(ndx_banana))).* dx_banana[:, i, j] |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
241 | else |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
242 | y[:, i, j] .= 0 |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
243 | end |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
244 | end |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
245 | end |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
246 | end |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
247 | |
50 | 248 | function activation(x :: Real) |
249 | return (1/(1 + exp(-1000(x - 0.05)))) # best for shepp logan | |
52
cb029cdb141a
activation function for dual scscaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
50
diff
changeset
|
250 | #return -abs(x-1)^1/5 + 1 # best for lighthouse and brainphantom |
48
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
251 | # return x^(5) |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
252 | # return (1/(1 + exp(-1000(x - 0.075)))) |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
253 | # return 4*(x-0.5)^3 + 0.5 |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
254 | # return (1/(1 + exp(-1000(x - 0.05)))) |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
255 | # return -3(x-0.5)^2 + 0.75 |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
256 | # return (x-1)^51 + 1 |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
257 | # return -abs(x-1)^1/3 + 1 |
50 | 258 | # return 16*(x-0.5)^5 + 0.5 |
47
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
259 | end |
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
260 | |
66
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
261 | function pdflow!(x, Δx, y, Δy, u, :: ZeroDual, α; threads=:none) |
36 | 262 | @assert(size(u)==(2,)) |
263 | flow!(x, u; threads=(threads==:inner)) | |
264 | y .= 0.0 | |
265 | end | |
266 | ||
66
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
267 | function pdflow!(x, Δx, y, Δy, u, :: PrimalOnly, α; threads=:none) |
36 | 268 | @assert(size(u)==(2,)) |
269 | flow!(x, u; threads=(threads==:inner)) | |
270 | end | |
8
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
271 | |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
272 | ########################## |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
273 | # PET |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
274 | ########################## |
36 | 275 | |
8
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
276 | function petflow_interp!(x::AbstractImage, tmp::AbstractImage, u::DisplacementConstant, theta_known::DisplacementConstant; |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
277 | threads = false) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
278 | tmp .= x |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
279 | center_point = center(x) .+ u |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
280 | tform = recenter(RotMatrix(theta_known[1]), center_point) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
281 | tmp = warp(x, tform, axes(x), fillvalue=Flat()) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
282 | x .= tmp |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
283 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
284 | |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
285 | petflow! = petflow_interp! |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
286 | |
36 | 287 | function petpdflow!(x, Δx, y, Δy, u, theta_known, dual_flow :: Bool; threads=:none) |
8
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
288 | if dual_flow |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
289 | @backgroundif (threads==:outer) begin |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
290 | petflow!(x, Δx, u, theta_known; threads=(threads==:inner)) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
291 | end begin |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
292 | petflow!(@view(y[1, :, :]), @view(Δy[1, :, :]), u, theta_known; threads=(threads==:inner)) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
293 | petflow!(@view(y[2, :, :]), @view(Δy[2, :, :]), u, theta_known; threads=(threads==:inner)) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
294 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
295 | else |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
296 | petflow!(x, Δx, u, theta_known) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
297 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
298 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
299 | |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
300 | # Method for greedy predictor |
66
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
301 | function petpdflow!(x, Δx, y, Δy, u, theta_known, flow :: Greedy, α; threads=:none) |
8
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
302 | oldx = copy(x) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
303 | center_point = center(x) .+ u |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
304 | tform = recenter(RotMatrix(theta_known[1]), center_point) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
305 | Δx = warp(x, tform, axes(x), fillvalue=Flat()) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
306 | @. x = Δx |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
307 | @. Δy = y |
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
308 | Dxx = copy(Δy) |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
309 | DΔx = copy(Δy) |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
310 | ∇₂!(Dxx, x) |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
311 | ∇₂!(DΔx, oldx) |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
312 | inds = abs.(Dxx) .≤ 1e-2 |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
313 | Dxx[inds] .= 1 |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
314 | DΔx[inds] .= 1 |
36 | 315 | y .= y.* DΔx ./ Dxx |
8
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
316 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
317 | |
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
318 | # Method for dual scaling predictor |
66
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
319 | function petpdflow!(x, Δx, y, Δy, u, theta_known, flow :: DualScaling, α; threads=:none) |
8
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
320 | oldx = copy(x) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
321 | center_point = center(x) .+ u |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
322 | tform = recenter(RotMatrix(theta_known[1]), center_point) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
323 | Δx = warp(x, tform, axes(x), fillvalue=Flat()) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
324 | @. x = Δx |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
325 | C = similar(y) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
326 | cc = abs.(x-oldx) |
47
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
327 | cm = max(flow.threshold,maximum(cc)) |
52
cb029cdb141a
activation function for dual scscaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
50
diff
changeset
|
328 | c = 1.0 .- flow.factor.*flow.activation.(cc./cm) |
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
329 | C[1,:,:] .= c |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
330 | C[2,:,:] .= c |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
331 | y .= C.*y |
8
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
332 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
333 | |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
334 | # Method for rotation prediction (exploiting property of inverse rotation) |
66
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
335 | function petpdflow!(x, Δx, y, Δy, u, theta_known, flow :: Rotation, α; threads=:none) |
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
336 | @backgroundif (threads==:outer) begin |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
337 | petflow!(x, Δx, u, theta_known; threads=(threads==:inner)) |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
338 | end begin |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
339 | petflow!(@view(y[1, :, :]), @view(Δy[1, :, :]), u, -theta_known; threads=(threads==:inner)) |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
340 | petflow!(@view(y[2, :, :]), @view(Δy[2, :, :]), u, -theta_known; threads=(threads==:inner)) |
8
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
341 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
342 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
343 | |
66
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
344 | # Alternative method for rotation prediction |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
345 | # function petpdflow!(x, Δx, y, Δy, u, theta_known, flow :: Rotation, α; threads=:none) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
346 | # Δx .= x |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
347 | # petflow!(x, Δx, u, theta_known) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
348 | # (m,n) = size(x) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
349 | # dx = similar(y) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
350 | # dx_banana = similar(y) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
351 | # ∇₂!(dx, Δx) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
352 | # ∇₂!(dx_banana, x) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
353 | # for i=1:m |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
354 | # for j=1:n |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
355 | # ndx = @views sum(dx[:, i, j].^2) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
356 | # ndx_banana = @views sum(dx_banana[:, i, j].^2) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
357 | # if ndx > eps(1.0) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
358 | # A = dx[:, i, j] |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
359 | # B = dx_banana[:, i, j] |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
360 | # theta = atan(B[1] * A[2] - B[2] * A[1], B[1] * A[1] + B[2] * A[2]) # Oriented angle from A to B |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
361 | # cos_theta = cos(theta) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
362 | # sin_theta = sin(theta) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
363 | # a = cos_theta * y[1, i, j] - sin_theta * y[2, i, j] |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
364 | # b = sin_theta * y[1, i, j] + cos_theta * y[2, i, j] |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
365 | # y[1, i, j] = a |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
366 | # y[2, i, j] = b |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
367 | # elseif ndx ≤ eps(1.0)&& ndx_banana > eps(1.0) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
368 | # y[:, i, j] = α .* dx_banana[:, i, j] ./ sqrt(ndx_banana) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
369 | # else |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
370 | # y[:, i, j] .= 0 |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
371 | # end |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
372 | # end |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
373 | # end |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
374 | # end |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
375 | |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
376 | # Additional method for new strict TV preserving predictor a.k.a. Strict Greedy |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
377 | function petpdflow!(x, Δx, y, Δy, u, theta_known, flow :: StrictGreedy, α; threads=:none) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
378 | Δx .= x |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
379 | petflow!(x, Δx, u, theta_known) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
380 | (m,n) = size(x) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
381 | dx = similar(y) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
382 | dx_banana = similar(y) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
383 | ∇₂!(dx, Δx) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
384 | ∇₂!(dx_banana, x) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
385 | Δy .= y |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
386 | for i=1:m |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
387 | for j=1:n |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
388 | ndx = @views sum(dx[:, i, j].^2) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
389 | ndx_banana = @views sum(dx_banana[:, i, j].^2) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
390 | if ndx > eps(1.0) && ndx_banana > eps(1.0) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
391 | A = dx[:, i, j] |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
392 | B = Δy[:, i, j] |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
393 | y[:, i, j] .= (dot(A,B)/(sqrt(ndx)*sqrt(ndx_banana))).* dx_banana[:, i, j] |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
394 | elseif ndx ≤ eps(1.0) && ndx_banana > eps(1.0) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
395 | A = ones(size(dx[:, i, j])...) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
396 | ndx = sum(A.^2) |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
397 | B = Δy[:, i, j] |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
398 | y[:, i, j] .= (dot(A,B)/(sqrt(ndx)*sqrt(ndx_banana))).* dx_banana[:, i, j] |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
399 | else |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
400 | y[:, i, j] .= 0 |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
401 | end |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
402 | end |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
403 | end |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
404 | end |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
405 | |
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
406 | function petpdflow!(x, Δx, y, Δy, u, theta_known, :: ZeroDual, α; threads=:none) |
36 | 407 | petflow!(x, Δx, u, theta_known, threads=(threads==:inner)) |
408 | y .= 0.0 | |
409 | end | |
410 | ||
66
dc69a0d234ae
modified rotation and added new strict tv preserving predictor
Neil Dizon <neil.dizon@helsinki.fi>
parents:
52
diff
changeset
|
411 | function petpdflow!(x, Δx, y, Δy, u, theta_known, :: PrimalOnly, α; threads=:none) |
36 | 412 | petflow!(x, Δx, u, theta_known, threads=(threads==:inner)) |
413 | end | |
414 | ||
0 | 415 | ########################## |
416 | # Linearised optical flow | |
417 | ########################## | |
418 | ||
419 | # ⟨⟨u, ∇b⟩⟩ | |
420 | function pointwise_gradiprod_2d!(y::Image, vtmp::Gradient, | |
421 | u::DisplacementFull, b::Image; | |
422 | add = false) | |
423 | ∇₂c!(vtmp, b) | |
424 | ||
425 | u′=reshape(u, (size(u, 1), prod(size(u)[2:end]))) | |
426 | vtmp′=reshape(vtmp, (size(vtmp, 1), prod(size(vtmp)[2:end]))) | |
427 | y′=reshape(y, prod(size(y))) | |
428 | ||
429 | if add | |
430 | @simd for i = 1:length(y′) | |
431 | @inbounds y′[i] += dot(@view(u′[:, i]), @view(vtmp′[:, i])) | |
432 | end | |
433 | else | |
434 | @simd for i = 1:length(y′) | |
435 | @inbounds y′[i] = dot(@view(u′[:, i]), @view(vtmp′[:, i])) | |
436 | end | |
437 | end | |
438 | end | |
439 | ||
440 | function pointwise_gradiprod_2d!(y::Image, vtmp::Gradient, | |
441 | u::DisplacementConstant, b::Image; | |
442 | add = false) | |
443 | ∇₂c!(vtmp, b) | |
444 | ||
445 | vtmp′=reshape(vtmp, (size(vtmp, 1), prod(size(vtmp)[2:end]))) | |
446 | y′=reshape(y, prod(size(y))) | |
447 | ||
448 | if add | |
449 | @simd for i = 1:length(y′) | |
450 | @inbounds y′[i] += dot(u, @view(vtmp′[:, i])) | |
451 | end | |
452 | else | |
453 | @simd for i = 1:length(y′) | |
454 | @inbounds y′[i] = dot(u, @view(vtmp′[:, i])) | |
455 | end | |
456 | end | |
457 | end | |
458 | ||
459 | # ∇b ⋅ y | |
460 | function pointwise_gradiprod_2dᵀ!(u::DisplacementFull, y::Image, b::Image) | |
461 | ∇₂c!(u, b) | |
462 | ||
463 | u′=reshape(u, (size(u, 1), prod(size(u)[2:end]))) | |
464 | y′=reshape(y, prod(size(y))) | |
465 | ||
466 | @simd for i=1:length(y′) | |
467 | @inbounds @. u′[:, i] *= y′[i] | |
468 | end | |
469 | end | |
470 | ||
471 | function pointwise_gradiprod_2dᵀ!(u::DisplacementConstant, y::Image, b::Image) | |
472 | @assert(size(y)==size(b) && size(u)==(2,)) | |
473 | u .= 0 | |
474 | ∇₂cfold!(b, nothing) do g, st, (i, j) | |
475 | @inbounds u .+= g.*y[i, j] | |
476 | return st | |
477 | end | |
478 | # Reweight to be with respect to 𝟙^*𝟙 inner product. | |
479 | u ./= prod(size(b)) | |
480 | end | |
481 | ||
482 | mutable struct ConstantDisplacementHornSchunckData | |
483 | M₀::Array{Float64,2} | |
484 | z::Array{Float64,1} | |
485 | Mv::Array{Float64,2} | |
486 | av::Array{Float64,1} | |
487 | cv::Float64 | |
488 | ||
489 | function ConstantDisplacementHornSchunckData() | |
490 | return new(zeros(2, 2), zeros(2), zeros(2,2), zeros(2), 0) | |
491 | end | |
492 | end | |
493 | ||
494 | # For DisplacementConstant, for the simple prox step | |
495 | # | |
496 | # (1) argmin_u 1/(2τ)|u-ũ|^2 + (θ/2)|b⁺-b+<<u-ŭ,∇b>>|^2 + (λ/2)|u-ŭ|^2, | |
497 | # | |
498 | # construct matrix M₀ and vector z such that we can solve u from | |
499 | # | |
500 | # (2) (I/τ+M₀)u = M₀ŭ + ũ/τ - z | |
501 | # | |
502 | # Note that the problem | |
503 | # | |
504 | # argmin_u 1/(2τ)|u-ũ|^2 + (θ/2)|b⁺-b+<<u-ŭ,∇b>>|^2 + (λ/2)|u-ŭ|^2 | |
505 | # + (θ/2)|b⁺⁺-b⁺+<<uʹ-u,∇b⁺>>|^2 + (λ/2)|u-uʹ|^2 | |
506 | # | |
507 | # has with respect to u the system | |
508 | # | |
509 | # (I/τ+M₀+M₀ʹ)u = M₀ŭ + M₀ʹuʹ + ũ/τ - z + zʹ, | |
510 | # | |
511 | # where the primed variables correspond to (2) for (1) for uʹ in place of u: | |
512 | # | |
513 | # argmin_uʹ 1/(2τ)|uʹ-ũʹ|^2 + (θ/2)|b⁺⁺-b⁺+<<uʹ-u,∇b⁺>>|^2 + (λ/2)|uʹ-u|^2 | |
514 | # | |
515 | function horn_schunck_reg_prox_op!(hs::ConstantDisplacementHornSchunckData, | |
516 | bnext::Image, b::Image, θ, λ, T) | |
517 | @assert(size(b)==size(bnext)) | |
518 | w = prod(size(b)) | |
519 | z = hs.z | |
520 | cv = 0 | |
521 | # Factors of symmetric matrix [a c; c d] | |
522 | a, c, d = 0.0, 0.0, 0.0 | |
523 | # This used to use ∇₂cfold but it is faster to allocate temporary | |
524 | # storage for the full gradient due to probably better memory and SIMD | |
36 | 525 | # instruction usage. |
0 | 526 | g = zeros(2, size(b)...) |
527 | ∇₂c!(g, b) | |
528 | @inbounds for i=1:size(b, 1) | |
529 | for j=1:size(b, 2) | |
530 | δ = bnext[i,j]-b[i,j] | |
531 | @. z += g[:,i,j]*δ | |
532 | cv += δ*δ | |
533 | a += g[1,i,j]*g[1,i,j] | |
534 | c += g[1,i,j]*g[2,i,j] | |
535 | d += g[2,i,j]*g[2,i,j] | |
536 | end | |
537 | end | |
538 | w₀ = λ | |
539 | w₂ = θ/w | |
540 | aʹ = w₀ + w₂*a | |
541 | cʹ = w₂*c | |
542 | dʹ = w₀ + w₂*d | |
543 | hs.M₀ .= [aʹ cʹ; cʹ dʹ] | |
544 | hs.Mv .= [w*λ+θ*a θ*c; θ*c w*λ+θ*d] | |
545 | hs.cv = cv*θ | |
546 | hs.av .= hs.z.*θ | |
547 | hs.z .*= w₂/T | |
548 | end | |
549 | ||
550 | # Solve the 2D system (I/τ+M₀)u = z | |
551 | @inline function mldivide_step_plus_sym2x2!(u, M₀, z, τ) | |
552 | a = 1/τ+M₀[1, 1] | |
553 | c = M₀[1, 2] | |
554 | d = 1/τ+M₀[2, 2] | |
555 | u .= ([d -c; -c a]*z)./(a*d-c*c) | |
556 | end | |
557 | ||
558 | function horn_schunck_reg_prox!(u::DisplacementConstant, bnext::Image, b::Image, | |
559 | θ, λ, T, τ) | |
560 | hs=ConstantDisplacementHornSchunckData() | |
561 | horn_schunck_reg_prox_op!(hs, bnext, b, θ, λ, T) | |
562 | mldivide_step_plus_sym2x2!(u, hs.M₀, (u./τ)-hs.z, τ) | |
563 | end | |
564 | ||
565 | function flow_grad!(x::Image, vtmp::Gradient, u::Displacement; δ=nothing) | |
566 | if !isnothing(δ) | |
567 | u = δ.*u | |
568 | end | |
569 | pointwise_gradiprod_2d!(x, vtmp, u, x; add=true) | |
570 | end | |
571 | ||
572 | # Error b-b_prev+⟨⟨u, ∇b⟩⟩ for Horn–Schunck type penalisation | |
573 | function linearised_optical_flow_error(u::Displacement, b::Image, b_prev::Image) | |
574 | imdim = size(b) | |
575 | vtmp = zeros(2, imdim...) | |
576 | tmp = b-b_prev | |
577 | pointwise_gradiprod_2d!(tmp, vtmp, u, b_prev; add=true) | |
578 | return tmp | |
579 | end | |
580 | ||
581 | ############################################## | |
582 | # Helper to smooth data for Horn–Schunck term | |
583 | ############################################## | |
584 | ||
585 | function filter_hs(b, b_next, b_next_filt, kernel) | |
586 | if kernel==nothing | |
587 | f = x -> x | |
588 | else | |
589 | f = x -> simple_imfilter(x, kernel; threads=true) | |
590 | end | |
591 | ||
36 | 592 | # We already filtered b in the previous step (b_next in that step) |
0 | 593 | b_filt = b_next_filt==nothing ? f(b) : b_next_filt |
594 | b_next_filt = f(b_next) | |
595 | ||
596 | return b_filt, b_next_filt | |
597 | end | |
598 | ||
599 | end # Module |