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