Thu, 25 Apr 2024 14:48:54 -0500
oops
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!, |
36 | 45 | DualScaling, Greedy, Rotation, ZeroDual, PrimalOnly, |
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 |
65 | exponent :: Integer | |
66 | threshold :: Real | |
67 | DualScaling(e = 10, t = 1e-1) = new(e, t) | |
68 | end | |
69 | ||
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
70 | struct Greedy end |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
71 | struct Rotation end |
36 | 72 | struct ZeroDual end |
73 | struct PrimalOnly end | |
74 | ||
75 | function identifier(::DualScaling) | |
76 | "pdps_known_dualscaling" | |
77 | end | |
78 | ||
79 | function identifier(::Rotation) | |
80 | "pdps_known_rotation" | |
81 | end | |
82 | ||
83 | function identifier(::Greedy) | |
84 | "pdps_known_greedy" | |
85 | end | |
86 | ||
87 | function identifier(::ZeroDual) | |
88 | "pdps_known_zerodual" | |
89 | end | |
90 | ||
91 | function identifier(::PrimalOnly) | |
92 | "pdps_known_primalonly" | |
93 | end | |
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
94 | |
0 | 95 | ################################# |
96 | # Displacement field based flow | |
97 | ################################# | |
98 | ||
99 | function flow_interp!(x::AbstractImage, u::Displacement, tmp::AbstractImage; | |
100 | threads = false) | |
101 | tmp .= x | |
102 | Translate.translate_image!(x, tmp, u; threads=threads) | |
103 | end | |
104 | ||
105 | function flow_interp!(x::AbstractImage, u::Displacement; | |
106 | threads = false) | |
107 | tmp = copy(x) | |
108 | Translate.translate_image!(x, tmp, u; threads=threads) | |
109 | end | |
110 | ||
111 | flow! = flow_interp! | |
112 | ||
113 | function pdflow!(x, Δx, y, Δy, u, dual_flow; threads=:none) | |
114 | if dual_flow | |
115 | #flow!((x, @view(y[1, :, :]), @view(y[2, :, :])), diffu, | |
116 | # (Δx, @view(Δy[1, :, :]), @view(Δy[2, :, :]))) | |
117 | @backgroundif (threads==:outer) begin | |
118 | flow!(x, u, Δx; threads=(threads==:inner)) | |
119 | end begin | |
120 | flow!(@view(y[1, :, :]), u, @view(Δy[1, :, :]); threads=(threads==:inner)) | |
121 | flow!(@view(y[2, :, :]), u, @view(Δy[2, :, :]); threads=(threads==:inner)) | |
122 | end | |
123 | else | |
124 | flow!(x, u, Δx) | |
125 | end | |
126 | end | |
127 | ||
36 | 128 | function pdflow!(x, Δx, y, Δy, z, Δz, u, dual_flow :: Bool; threads=:none) |
0 | 129 | if dual_flow |
130 | @backgroundif (threads==:outer) begin | |
131 | flow!(x, u, Δx; threads=(threads==:inner)) | |
132 | flow!(z, u, Δz; threads=(threads==:inner)) | |
133 | end begin | |
134 | flow!(@view(y[1, :, :]), u, @view(Δy[1, :, :]); threads=(threads==:inner)) | |
135 | flow!(@view(y[2, :, :]), u, @view(Δy[2, :, :]); threads=(threads==:inner)) | |
136 | end | |
137 | else | |
138 | flow!(x, u, Δx; threads=(threads==:inner)) | |
139 | flow!(z, u, Δz; threads=(threads==:inner)) | |
140 | end | |
141 | end | |
142 | ||
5 | 143 | # Additional method for Greedy |
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
144 | function pdflow!(x, Δx, y, Δy, u, flow :: Greedy; threads=:none) |
36 | 145 | @assert(size(u)==(2,)) |
5 | 146 | Δx .= x |
147 | Δy .= y | |
148 | flow!(x, u; threads=(threads==:inner)) | |
149 | Dxx = similar(Δy) | |
150 | DΔx = similar(Δy) | |
151 | ∇₂!(Dxx, x) | |
152 | ∇₂!(DΔx, Δx) | |
153 | inds = abs.(Dxx) .≤ 1e-1 | |
154 | Dxx[inds] .= 1 | |
155 | DΔx[inds] .= 1 | |
36 | 156 | y .= y.* DΔx ./ Dxx |
5 | 157 | end |
158 | ||
159 | # Additional method for Rotation | |
36 | 160 | function pdflow!(x, Δx, y, Δy, u, flow :: Rotation; threads=:none) |
5 | 161 | @assert(size(u)==(2,)) |
162 | Δx .= x | |
163 | flow!(x, u; threads=(threads==:inner)) | |
164 | (m,n) = size(x) | |
165 | dx = similar(y) | |
166 | dx_banana = similar(y) | |
167 | ∇₂!(dx, Δx) | |
168 | ∇₂!(dx_banana, x) | |
169 | for i=1:m | |
170 | for j=1:n | |
171 | ndx = @views sum(dx[:, i, j].^2) | |
172 | ndx_banana = @views sum(dx_banana[:, i, j].^2) | |
173 | if ndx > 1e-4 && ndx_banana > 1e-4 | |
174 | A = dx[:, i, j] | |
175 | B = dx_banana[:, i, j] | |
176 | theta = atan(B[1] * A[2] - B[2] * A[1], B[1] * A[1] + B[2] * A[2]) # Oriented angle from A to B | |
177 | cos_theta = cos(theta) | |
178 | sin_theta = sin(theta) | |
179 | a = cos_theta * y[1, i, j] - sin_theta * y[2, i, j] | |
180 | b = sin_theta * y[1, i, j] + cos_theta * y[2, i, j] | |
181 | y[1, i, j] = a | |
182 | y[2, i, j] = b | |
183 | end | |
184 | end | |
185 | end | |
186 | end | |
187 | ||
188 | # Additional method for Dual Scaling | |
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
189 | function pdflow!(x, Δx, y, Δy, u, flow :: DualScaling; threads=:none) |
5 | 190 | @assert(size(u)==(2,)) |
191 | oldx = copy(x) | |
192 | flow!(x, u; threads=(threads==:inner)) | |
193 | C = similar(y) | |
194 | cc = abs.(x-oldx) | |
35 | 195 | cm = max(flow.threshold,maximum(cc)) |
196 | c = 1 .* (1 .- cc./ cm) .^flow.exponent | |
5 | 197 | C[1,:,:] .= c |
198 | C[2,:,:] .= c | |
36 | 199 | y .= C.*y |
5 | 200 | end |
201 | ||
36 | 202 | function pdflow!(x, Δx, y, Δy, u, :: ZeroDual; threads=:none) |
203 | @assert(size(u)==(2,)) | |
204 | flow!(x, u; threads=(threads==:inner)) | |
205 | y .= 0.0 | |
206 | end | |
207 | ||
208 | function pdflow!(x, Δx, y, Δy, u, :: PrimalOnly; threads=:none) | |
209 | @assert(size(u)==(2,)) | |
210 | flow!(x, u; threads=(threads==:inner)) | |
211 | end | |
8
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
212 | |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
213 | ########################## |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
214 | # PET |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
215 | ########################## |
36 | 216 | |
8
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
217 | 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
|
218 | threads = false) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
219 | tmp .= x |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
220 | center_point = center(x) .+ u |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
221 | tform = recenter(RotMatrix(theta_known[1]), center_point) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
222 | tmp = warp(x, tform, axes(x), fillvalue=Flat()) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
223 | x .= tmp |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
224 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
225 | |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
226 | petflow! = petflow_interp! |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
227 | |
36 | 228 | 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
|
229 | if dual_flow |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
230 | @backgroundif (threads==:outer) begin |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
231 | 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
|
232 | end begin |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
233 | 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
|
234 | 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
|
235 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
236 | else |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
237 | petflow!(x, Δx, u, theta_known) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
238 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
239 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
240 | |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
241 | # Method for greedy predictor |
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
242 | 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
|
243 | oldx = copy(x) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
244 | center_point = center(x) .+ u |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
245 | tform = recenter(RotMatrix(theta_known[1]), center_point) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
246 | Δx = warp(x, tform, axes(x), fillvalue=Flat()) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
247 | @. x = Δx |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
248 | @. Δy = y |
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
249 | Dxx = copy(Δy) |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
250 | DΔx = copy(Δy) |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
251 | ∇₂!(Dxx, x) |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
252 | ∇₂!(DΔx, oldx) |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
253 | inds = abs.(Dxx) .≤ 1e-2 |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
254 | Dxx[inds] .= 1 |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
255 | DΔx[inds] .= 1 |
36 | 256 | y .= y.* DΔx ./ Dxx |
8
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
257 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
258 | |
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
259 | # Method for dual scaling predictor |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
260 | 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
|
261 | oldx = copy(x) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
262 | center_point = center(x) .+ u |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
263 | tform = recenter(RotMatrix(theta_known[1]), center_point) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
264 | Δx = warp(x, tform, axes(x), fillvalue=Flat()) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
265 | @. x = Δx |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
266 | C = similar(y) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
267 | cc = abs.(x-oldx) |
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
268 | cm = max(1e-12,maximum(cc)) |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
269 | c = 1 .* (1 .- cc./ cm) .^(10) |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
270 | C[1,:,:] .= c |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
271 | C[2,:,:] .= c |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
272 | y .= C.*y |
8
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
273 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
274 | |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
275 | # Method for rotation prediction (exploiting property of inverse rotation) |
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
276 | function petpdflow!(x, Δx, y, Δy, u, theta_known, flow :: Rotation; threads=:none) |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
277 | @backgroundif (threads==:outer) begin |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
278 | 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
|
279 | end begin |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
280 | 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
|
281 | 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
|
282 | end |
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 | |
36 | 285 | function petpdflow!(x, Δx, y, Δy, u, theta_known, :: ZeroDual; threads=:none) |
286 | petflow!(x, Δx, u, theta_known, threads=(threads==:inner)) | |
287 | y .= 0.0 | |
288 | end | |
289 | ||
290 | function petpdflow!(x, Δx, y, Δy, u, theta_known, :: PrimalOnly; threads=:none) | |
291 | petflow!(x, Δx, u, theta_known, threads=(threads==:inner)) | |
292 | end | |
293 | ||
0 | 294 | ########################## |
295 | # Linearised optical flow | |
296 | ########################## | |
297 | ||
298 | # ⟨⟨u, ∇b⟩⟩ | |
299 | function pointwise_gradiprod_2d!(y::Image, vtmp::Gradient, | |
300 | u::DisplacementFull, b::Image; | |
301 | add = false) | |
302 | ∇₂c!(vtmp, b) | |
303 | ||
304 | u′=reshape(u, (size(u, 1), prod(size(u)[2:end]))) | |
305 | vtmp′=reshape(vtmp, (size(vtmp, 1), prod(size(vtmp)[2:end]))) | |
306 | y′=reshape(y, prod(size(y))) | |
307 | ||
308 | if add | |
309 | @simd for i = 1:length(y′) | |
310 | @inbounds y′[i] += dot(@view(u′[:, i]), @view(vtmp′[:, i])) | |
311 | end | |
312 | else | |
313 | @simd for i = 1:length(y′) | |
314 | @inbounds y′[i] = dot(@view(u′[:, i]), @view(vtmp′[:, i])) | |
315 | end | |
316 | end | |
317 | end | |
318 | ||
319 | function pointwise_gradiprod_2d!(y::Image, vtmp::Gradient, | |
320 | u::DisplacementConstant, b::Image; | |
321 | add = false) | |
322 | ∇₂c!(vtmp, b) | |
323 | ||
324 | vtmp′=reshape(vtmp, (size(vtmp, 1), prod(size(vtmp)[2:end]))) | |
325 | y′=reshape(y, prod(size(y))) | |
326 | ||
327 | if add | |
328 | @simd for i = 1:length(y′) | |
329 | @inbounds y′[i] += dot(u, @view(vtmp′[:, i])) | |
330 | end | |
331 | else | |
332 | @simd for i = 1:length(y′) | |
333 | @inbounds y′[i] = dot(u, @view(vtmp′[:, i])) | |
334 | end | |
335 | end | |
336 | end | |
337 | ||
338 | # ∇b ⋅ y | |
339 | function pointwise_gradiprod_2dᵀ!(u::DisplacementFull, y::Image, b::Image) | |
340 | ∇₂c!(u, b) | |
341 | ||
342 | u′=reshape(u, (size(u, 1), prod(size(u)[2:end]))) | |
343 | y′=reshape(y, prod(size(y))) | |
344 | ||
345 | @simd for i=1:length(y′) | |
346 | @inbounds @. u′[:, i] *= y′[i] | |
347 | end | |
348 | end | |
349 | ||
350 | function pointwise_gradiprod_2dᵀ!(u::DisplacementConstant, y::Image, b::Image) | |
351 | @assert(size(y)==size(b) && size(u)==(2,)) | |
352 | u .= 0 | |
353 | ∇₂cfold!(b, nothing) do g, st, (i, j) | |
354 | @inbounds u .+= g.*y[i, j] | |
355 | return st | |
356 | end | |
357 | # Reweight to be with respect to 𝟙^*𝟙 inner product. | |
358 | u ./= prod(size(b)) | |
359 | end | |
360 | ||
361 | mutable struct ConstantDisplacementHornSchunckData | |
362 | M₀::Array{Float64,2} | |
363 | z::Array{Float64,1} | |
364 | Mv::Array{Float64,2} | |
365 | av::Array{Float64,1} | |
366 | cv::Float64 | |
367 | ||
368 | function ConstantDisplacementHornSchunckData() | |
369 | return new(zeros(2, 2), zeros(2), zeros(2,2), zeros(2), 0) | |
370 | end | |
371 | end | |
372 | ||
373 | # For DisplacementConstant, for the simple prox step | |
374 | # | |
375 | # (1) argmin_u 1/(2τ)|u-ũ|^2 + (θ/2)|b⁺-b+<<u-ŭ,∇b>>|^2 + (λ/2)|u-ŭ|^2, | |
376 | # | |
377 | # construct matrix M₀ and vector z such that we can solve u from | |
378 | # | |
379 | # (2) (I/τ+M₀)u = M₀ŭ + ũ/τ - z | |
380 | # | |
381 | # Note that the problem | |
382 | # | |
383 | # argmin_u 1/(2τ)|u-ũ|^2 + (θ/2)|b⁺-b+<<u-ŭ,∇b>>|^2 + (λ/2)|u-ŭ|^2 | |
384 | # + (θ/2)|b⁺⁺-b⁺+<<uʹ-u,∇b⁺>>|^2 + (λ/2)|u-uʹ|^2 | |
385 | # | |
386 | # has with respect to u the system | |
387 | # | |
388 | # (I/τ+M₀+M₀ʹ)u = M₀ŭ + M₀ʹuʹ + ũ/τ - z + zʹ, | |
389 | # | |
390 | # where the primed variables correspond to (2) for (1) for uʹ in place of u: | |
391 | # | |
392 | # argmin_uʹ 1/(2τ)|uʹ-ũʹ|^2 + (θ/2)|b⁺⁺-b⁺+<<uʹ-u,∇b⁺>>|^2 + (λ/2)|uʹ-u|^2 | |
393 | # | |
394 | function horn_schunck_reg_prox_op!(hs::ConstantDisplacementHornSchunckData, | |
395 | bnext::Image, b::Image, θ, λ, T) | |
396 | @assert(size(b)==size(bnext)) | |
397 | w = prod(size(b)) | |
398 | z = hs.z | |
399 | cv = 0 | |
400 | # Factors of symmetric matrix [a c; c d] | |
401 | a, c, d = 0.0, 0.0, 0.0 | |
402 | # This used to use ∇₂cfold but it is faster to allocate temporary | |
403 | # storage for the full gradient due to probably better memory and SIMD | |
36 | 404 | # instruction usage. |
0 | 405 | g = zeros(2, size(b)...) |
406 | ∇₂c!(g, b) | |
407 | @inbounds for i=1:size(b, 1) | |
408 | for j=1:size(b, 2) | |
409 | δ = bnext[i,j]-b[i,j] | |
410 | @. z += g[:,i,j]*δ | |
411 | cv += δ*δ | |
412 | a += g[1,i,j]*g[1,i,j] | |
413 | c += g[1,i,j]*g[2,i,j] | |
414 | d += g[2,i,j]*g[2,i,j] | |
415 | end | |
416 | end | |
417 | w₀ = λ | |
418 | w₂ = θ/w | |
419 | aʹ = w₀ + w₂*a | |
420 | cʹ = w₂*c | |
421 | dʹ = w₀ + w₂*d | |
422 | hs.M₀ .= [aʹ cʹ; cʹ dʹ] | |
423 | hs.Mv .= [w*λ+θ*a θ*c; θ*c w*λ+θ*d] | |
424 | hs.cv = cv*θ | |
425 | hs.av .= hs.z.*θ | |
426 | hs.z .*= w₂/T | |
427 | end | |
428 | ||
429 | # Solve the 2D system (I/τ+M₀)u = z | |
430 | @inline function mldivide_step_plus_sym2x2!(u, M₀, z, τ) | |
431 | a = 1/τ+M₀[1, 1] | |
432 | c = M₀[1, 2] | |
433 | d = 1/τ+M₀[2, 2] | |
434 | u .= ([d -c; -c a]*z)./(a*d-c*c) | |
435 | end | |
436 | ||
437 | function horn_schunck_reg_prox!(u::DisplacementConstant, bnext::Image, b::Image, | |
438 | θ, λ, T, τ) | |
439 | hs=ConstantDisplacementHornSchunckData() | |
440 | horn_schunck_reg_prox_op!(hs, bnext, b, θ, λ, T) | |
441 | mldivide_step_plus_sym2x2!(u, hs.M₀, (u./τ)-hs.z, τ) | |
442 | end | |
443 | ||
444 | function flow_grad!(x::Image, vtmp::Gradient, u::Displacement; δ=nothing) | |
445 | if !isnothing(δ) | |
446 | u = δ.*u | |
447 | end | |
448 | pointwise_gradiprod_2d!(x, vtmp, u, x; add=true) | |
449 | end | |
450 | ||
451 | # Error b-b_prev+⟨⟨u, ∇b⟩⟩ for Horn–Schunck type penalisation | |
452 | function linearised_optical_flow_error(u::Displacement, b::Image, b_prev::Image) | |
453 | imdim = size(b) | |
454 | vtmp = zeros(2, imdim...) | |
455 | tmp = b-b_prev | |
456 | pointwise_gradiprod_2d!(tmp, vtmp, u, b_prev; add=true) | |
457 | return tmp | |
458 | end | |
459 | ||
460 | ############################################## | |
461 | # Helper to smooth data for Horn–Schunck term | |
462 | ############################################## | |
463 | ||
464 | function filter_hs(b, b_next, b_next_filt, kernel) | |
465 | if kernel==nothing | |
466 | f = x -> x | |
467 | else | |
468 | f = x -> simple_imfilter(x, kernel; threads=true) | |
469 | end | |
470 | ||
36 | 471 | # We already filtered b in the previous step (b_next in that step) |
0 | 472 | b_filt = b_next_filt==nothing ? f(b) : b_next_filt |
473 | b_next_filt = f(b_next) | |
474 | ||
475 | return b_filt, b_next_filt | |
476 | end | |
477 | ||
478 | end # Module |