Thu, 18 Apr 2024 11:31:32 +0300
added seed restart
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 | ||
14 | ########## | |
15 | # Exports | |
16 | ########## | |
17 | ||
18 | export flow!, | |
19 | pdflow!, | |
20 | flow_grad!, | |
21 | flow_interp!, | |
22 | estimate_Λ², | |
23 | estimate_linear_Λ², | |
24 | pointwise_gradiprod_2d!, | |
25 | pointwise_gradiprod_2dᵀ!, | |
26 | horn_schunck_reg_prox!, | |
27 | horn_schunck_reg_prox_op!, | |
28 | mldivide_step_plus_sym2x2!, | |
29 | linearised_optical_flow_error, | |
30 | Image, AbstractImage, ImageSize, | |
31 | Gradient, Displacement, | |
32 | DisplacementFull, DisplacementConstant, | |
33 | HornSchunckData, | |
34 | filter_hs | |
35 | ||
36 | ############################################### | |
37 | # Types (several imported from ImageTools.Translate) | |
38 | ############################################### | |
39 | ||
40 | Image = Translate.Image | |
41 | AbstractImage = AbstractArray{Float64,2} | |
42 | Displacement = Translate.Displacement | |
43 | DisplacementFull = Translate.DisplacementFull | |
44 | DisplacementConstant = Translate.DisplacementConstant | |
45 | Gradient = Array{Float64,3} | |
46 | ImageSize = Tuple{Int64,Int64} | |
47 | ||
48 | ################################# | |
49 | # Displacement field based flow | |
50 | ################################# | |
51 | ||
52 | function flow_interp!(x::AbstractImage, u::Displacement, tmp::AbstractImage; | |
53 | threads = false) | |
54 | tmp .= x | |
55 | Translate.translate_image!(x, tmp, u; threads=threads) | |
56 | end | |
57 | ||
58 | function flow_interp!(x::AbstractImage, u::Displacement; | |
59 | threads = false) | |
60 | tmp = copy(x) | |
61 | Translate.translate_image!(x, tmp, u; threads=threads) | |
62 | end | |
63 | ||
64 | flow! = flow_interp! | |
65 | ||
66 | function pdflow!(x, Δx, y, Δy, u, dual_flow; threads=:none) | |
67 | if dual_flow | |
68 | #flow!((x, @view(y[1, :, :]), @view(y[2, :, :])), diffu, | |
69 | # (Δx, @view(Δy[1, :, :]), @view(Δy[2, :, :]))) | |
70 | @backgroundif (threads==:outer) begin | |
71 | flow!(x, u, Δx; threads=(threads==:inner)) | |
72 | end begin | |
73 | flow!(@view(y[1, :, :]), u, @view(Δy[1, :, :]); threads=(threads==:inner)) | |
74 | flow!(@view(y[2, :, :]), u, @view(Δy[2, :, :]); threads=(threads==:inner)) | |
75 | end | |
76 | else | |
77 | flow!(x, u, Δx) | |
78 | end | |
79 | end | |
80 | ||
81 | function pdflow!(x, Δx, y, Δy, z, Δz, u, dual_flow; threads=:none) | |
82 | if dual_flow | |
83 | @backgroundif (threads==:outer) begin | |
84 | flow!(x, u, Δx; threads=(threads==:inner)) | |
85 | flow!(z, u, Δz; threads=(threads==:inner)) | |
86 | end begin | |
87 | flow!(@view(y[1, :, :]), u, @view(Δy[1, :, :]); threads=(threads==:inner)) | |
88 | flow!(@view(y[2, :, :]), u, @view(Δy[2, :, :]); threads=(threads==:inner)) | |
89 | end | |
90 | else | |
91 | flow!(x, u, Δx; threads=(threads==:inner)) | |
92 | flow!(z, u, Δz; threads=(threads==:inner)) | |
93 | end | |
94 | end | |
95 | ||
5 | 96 | # Additional method for Greedy |
97 | function pdflow!(x, Δx, y, Δy, u; threads=:none) | |
98 | @assert(size(u)==(2,)) | |
99 | Δx .= x | |
100 | Δy .= y | |
101 | flow!(x, u; threads=(threads==:inner)) | |
102 | Dxx = similar(Δy) | |
103 | DΔx = similar(Δy) | |
104 | ∇₂!(Dxx, x) | |
105 | ∇₂!(DΔx, Δx) | |
106 | inds = abs.(Dxx) .≤ 1e-1 | |
107 | Dxx[inds] .= 1 | |
108 | DΔx[inds] .= 1 | |
109 | y .= y.* DΔx ./ Dxx | |
110 | end | |
111 | ||
112 | # Additional method for Rotation | |
113 | function pdflow!(x, Δx, y, u; threads=:none) | |
114 | @assert(size(u)==(2,)) | |
115 | Δx .= x | |
116 | flow!(x, u; threads=(threads==:inner)) | |
117 | ||
118 | (m,n) = size(x) | |
119 | dx = similar(y) | |
120 | dx_banana = similar(y) | |
121 | ∇₂!(dx, Δx) | |
122 | ∇₂!(dx_banana, x) | |
123 | ||
124 | for i=1:m | |
125 | for j=1:n | |
126 | ndx = @views sum(dx[:, i, j].^2) | |
127 | ndx_banana = @views sum(dx_banana[:, i, j].^2) | |
128 | if ndx > 1e-4 && ndx_banana > 1e-4 | |
129 | A = dx[:, i, j] | |
130 | B = dx_banana[:, i, j] | |
131 | theta = atan(B[1] * A[2] - B[2] * A[1], B[1] * A[1] + B[2] * A[2]) # Oriented angle from A to B | |
132 | cos_theta = cos(theta) | |
133 | sin_theta = sin(theta) | |
134 | a = cos_theta * y[1, i, j] - sin_theta * y[2, i, j] | |
135 | b = sin_theta * y[1, i, j] + cos_theta * y[2, i, j] | |
136 | y[1, i, j] = a | |
137 | y[2, i, j] = b | |
138 | end | |
139 | end | |
140 | end | |
141 | end | |
142 | ||
143 | # Additional method for Dual Scaling | |
144 | function pdflow!(x, y, u; threads=:none) | |
145 | @assert(size(u)==(2,)) | |
146 | oldx = copy(x) | |
147 | flow!(x, u; threads=(threads==:inner)) | |
148 | C = similar(y) | |
149 | cc = abs.(x-oldx) | |
150 | cm = max(1e-12,maximum(cc)) | |
151 | c = 1 .* (1 .- cc./ cm) .^(10) | |
152 | C[1,:,:] .= c | |
153 | C[2,:,:] .= c | |
154 | y .= C.*y | |
155 | end | |
156 | ||
0 | 157 | ########################## |
158 | # Linearised optical flow | |
159 | ########################## | |
160 | ||
161 | # ⟨⟨u, ∇b⟩⟩ | |
162 | function pointwise_gradiprod_2d!(y::Image, vtmp::Gradient, | |
163 | u::DisplacementFull, b::Image; | |
164 | add = false) | |
165 | ∇₂c!(vtmp, b) | |
166 | ||
167 | u′=reshape(u, (size(u, 1), prod(size(u)[2:end]))) | |
168 | vtmp′=reshape(vtmp, (size(vtmp, 1), prod(size(vtmp)[2:end]))) | |
169 | y′=reshape(y, prod(size(y))) | |
170 | ||
171 | if add | |
172 | @simd for i = 1:length(y′) | |
173 | @inbounds y′[i] += dot(@view(u′[:, i]), @view(vtmp′[:, i])) | |
174 | end | |
175 | else | |
176 | @simd for i = 1:length(y′) | |
177 | @inbounds y′[i] = dot(@view(u′[:, i]), @view(vtmp′[:, i])) | |
178 | end | |
179 | end | |
180 | end | |
181 | ||
182 | function pointwise_gradiprod_2d!(y::Image, vtmp::Gradient, | |
183 | u::DisplacementConstant, b::Image; | |
184 | add = false) | |
185 | ∇₂c!(vtmp, b) | |
186 | ||
187 | vtmp′=reshape(vtmp, (size(vtmp, 1), prod(size(vtmp)[2:end]))) | |
188 | y′=reshape(y, prod(size(y))) | |
189 | ||
190 | if add | |
191 | @simd for i = 1:length(y′) | |
192 | @inbounds y′[i] += dot(u, @view(vtmp′[:, i])) | |
193 | end | |
194 | else | |
195 | @simd for i = 1:length(y′) | |
196 | @inbounds y′[i] = dot(u, @view(vtmp′[:, i])) | |
197 | end | |
198 | end | |
199 | end | |
200 | ||
201 | # ∇b ⋅ y | |
202 | function pointwise_gradiprod_2dᵀ!(u::DisplacementFull, y::Image, b::Image) | |
203 | ∇₂c!(u, b) | |
204 | ||
205 | u′=reshape(u, (size(u, 1), prod(size(u)[2:end]))) | |
206 | y′=reshape(y, prod(size(y))) | |
207 | ||
208 | @simd for i=1:length(y′) | |
209 | @inbounds @. u′[:, i] *= y′[i] | |
210 | end | |
211 | end | |
212 | ||
213 | function pointwise_gradiprod_2dᵀ!(u::DisplacementConstant, y::Image, b::Image) | |
214 | @assert(size(y)==size(b) && size(u)==(2,)) | |
215 | u .= 0 | |
216 | ∇₂cfold!(b, nothing) do g, st, (i, j) | |
217 | @inbounds u .+= g.*y[i, j] | |
218 | return st | |
219 | end | |
220 | # Reweight to be with respect to 𝟙^*𝟙 inner product. | |
221 | u ./= prod(size(b)) | |
222 | end | |
223 | ||
224 | mutable struct ConstantDisplacementHornSchunckData | |
225 | M₀::Array{Float64,2} | |
226 | z::Array{Float64,1} | |
227 | Mv::Array{Float64,2} | |
228 | av::Array{Float64,1} | |
229 | cv::Float64 | |
230 | ||
231 | function ConstantDisplacementHornSchunckData() | |
232 | return new(zeros(2, 2), zeros(2), zeros(2,2), zeros(2), 0) | |
233 | end | |
234 | end | |
235 | ||
236 | # For DisplacementConstant, for the simple prox step | |
237 | # | |
238 | # (1) argmin_u 1/(2τ)|u-ũ|^2 + (θ/2)|b⁺-b+<<u-ŭ,∇b>>|^2 + (λ/2)|u-ŭ|^2, | |
239 | # | |
240 | # construct matrix M₀ and vector z such that we can solve u from | |
241 | # | |
242 | # (2) (I/τ+M₀)u = M₀ŭ + ũ/τ - z | |
243 | # | |
244 | # Note that the problem | |
245 | # | |
246 | # argmin_u 1/(2τ)|u-ũ|^2 + (θ/2)|b⁺-b+<<u-ŭ,∇b>>|^2 + (λ/2)|u-ŭ|^2 | |
247 | # + (θ/2)|b⁺⁺-b⁺+<<uʹ-u,∇b⁺>>|^2 + (λ/2)|u-uʹ|^2 | |
248 | # | |
249 | # has with respect to u the system | |
250 | # | |
251 | # (I/τ+M₀+M₀ʹ)u = M₀ŭ + M₀ʹuʹ + ũ/τ - z + zʹ, | |
252 | # | |
253 | # where the primed variables correspond to (2) for (1) for uʹ in place of u: | |
254 | # | |
255 | # argmin_uʹ 1/(2τ)|uʹ-ũʹ|^2 + (θ/2)|b⁺⁺-b⁺+<<uʹ-u,∇b⁺>>|^2 + (λ/2)|uʹ-u|^2 | |
256 | # | |
257 | function horn_schunck_reg_prox_op!(hs::ConstantDisplacementHornSchunckData, | |
258 | bnext::Image, b::Image, θ, λ, T) | |
259 | @assert(size(b)==size(bnext)) | |
260 | w = prod(size(b)) | |
261 | z = hs.z | |
262 | cv = 0 | |
263 | # Factors of symmetric matrix [a c; c d] | |
264 | a, c, d = 0.0, 0.0, 0.0 | |
265 | # This used to use ∇₂cfold but it is faster to allocate temporary | |
266 | # storage for the full gradient due to probably better memory and SIMD | |
267 | # instruction usage. | |
268 | g = zeros(2, size(b)...) | |
269 | ∇₂c!(g, b) | |
270 | @inbounds for i=1:size(b, 1) | |
271 | for j=1:size(b, 2) | |
272 | δ = bnext[i,j]-b[i,j] | |
273 | @. z += g[:,i,j]*δ | |
274 | cv += δ*δ | |
275 | a += g[1,i,j]*g[1,i,j] | |
276 | c += g[1,i,j]*g[2,i,j] | |
277 | d += g[2,i,j]*g[2,i,j] | |
278 | end | |
279 | end | |
280 | w₀ = λ | |
281 | w₂ = θ/w | |
282 | aʹ = w₀ + w₂*a | |
283 | cʹ = w₂*c | |
284 | dʹ = w₀ + w₂*d | |
285 | hs.M₀ .= [aʹ cʹ; cʹ dʹ] | |
286 | hs.Mv .= [w*λ+θ*a θ*c; θ*c w*λ+θ*d] | |
287 | hs.cv = cv*θ | |
288 | hs.av .= hs.z.*θ | |
289 | hs.z .*= w₂/T | |
290 | end | |
291 | ||
292 | # Solve the 2D system (I/τ+M₀)u = z | |
293 | @inline function mldivide_step_plus_sym2x2!(u, M₀, z, τ) | |
294 | a = 1/τ+M₀[1, 1] | |
295 | c = M₀[1, 2] | |
296 | d = 1/τ+M₀[2, 2] | |
297 | u .= ([d -c; -c a]*z)./(a*d-c*c) | |
298 | end | |
299 | ||
300 | function horn_schunck_reg_prox!(u::DisplacementConstant, bnext::Image, b::Image, | |
301 | θ, λ, T, τ) | |
302 | hs=ConstantDisplacementHornSchunckData() | |
303 | horn_schunck_reg_prox_op!(hs, bnext, b, θ, λ, T) | |
304 | mldivide_step_plus_sym2x2!(u, hs.M₀, (u./τ)-hs.z, τ) | |
305 | end | |
306 | ||
307 | function flow_grad!(x::Image, vtmp::Gradient, u::Displacement; δ=nothing) | |
308 | if !isnothing(δ) | |
309 | u = δ.*u | |
310 | end | |
311 | pointwise_gradiprod_2d!(x, vtmp, u, x; add=true) | |
312 | end | |
313 | ||
314 | # Error b-b_prev+⟨⟨u, ∇b⟩⟩ for Horn–Schunck type penalisation | |
315 | function linearised_optical_flow_error(u::Displacement, b::Image, b_prev::Image) | |
316 | imdim = size(b) | |
317 | vtmp = zeros(2, imdim...) | |
318 | tmp = b-b_prev | |
319 | pointwise_gradiprod_2d!(tmp, vtmp, u, b_prev; add=true) | |
320 | return tmp | |
321 | end | |
322 | ||
323 | ############################################## | |
324 | # Helper to smooth data for Horn–Schunck term | |
325 | ############################################## | |
326 | ||
327 | function filter_hs(b, b_next, b_next_filt, kernel) | |
328 | if kernel==nothing | |
329 | f = x -> x | |
330 | else | |
331 | f = x -> simple_imfilter(x, kernel; threads=true) | |
332 | end | |
333 | ||
334 | # We already filtered b in the previous step (b_next in that step) | |
335 | b_filt = b_next_filt==nothing ? f(b) : b_next_filt | |
336 | b_next_filt = f(b_next) | |
337 | ||
338 | return b_filt, b_next_filt | |
339 | end | |
340 | ||
341 | end # Module |