Fri, 03 May 2024 09:13:05 -0500
data sources
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) |
48
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
208 | return (1/(1 + exp(-10000(x - 0.05)))) # best for shepp logan |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
209 | # return -abs(x-1)^1/5 + 1 # best for lighthouse and brainphantom |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
210 | # return x^(5) |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
211 | # return (1/(1 + exp(-1000(x - 0.075)))) |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
212 | # return 4*(x-0.5)^3 + 0.5 |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
213 | # return (1/(1 + exp(-1000(x - 0.05)))) |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
214 | # return -3(x-0.5)^2 + 0.75 |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
215 | # return (x-1)^51 + 1 |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
216 | # return -abs(x-1)^1/3 + 1 |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
217 | # return 16*(x-0.5)^5 + 0.5 |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
218 | end |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
219 | |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
220 | function activationD(x) |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
221 | # return (1/(1 + exp(-500(x - 0.1)))) |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
222 | # return 4*(x-0.5)^3 + 0.5 |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
223 | #return (1/(1 + exp(-1000(x - 0.1)))) |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
224 | return 1.0 |
47
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
225 | end |
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
226 | |
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
227 | # Experimental predictor for dual scaling based on activation |
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
228 | 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
|
229 | @assert(size(u)==(2,)) |
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
230 | oldx = copy(x) |
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
231 | flow!(x, u; threads=(threads==:inner)) |
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
232 | C = similar(y) |
48
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
233 | cc = abs.(x-oldx) |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
234 | cm = max(1e-12,maximum(cc)) |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
235 | c = activation.(cc ./ cm) |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
236 | C[1,:,:] .= c |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
237 | C[2,:,:] .= c |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
238 | # y .= C.*y |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
239 | y .= y .- 0.75.*C.*y # 0.75 for brain phantom and shepp logan |
47
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
240 | end |
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
241 | |
36 | 242 | function pdflow!(x, Δx, y, Δy, u, :: ZeroDual; threads=:none) |
243 | @assert(size(u)==(2,)) | |
244 | flow!(x, u; threads=(threads==:inner)) | |
245 | y .= 0.0 | |
246 | end | |
247 | ||
248 | function pdflow!(x, Δx, y, Δy, u, :: PrimalOnly; threads=:none) | |
249 | @assert(size(u)==(2,)) | |
250 | flow!(x, u; threads=(threads==:inner)) | |
251 | end | |
8
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
252 | |
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 | # PET |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
255 | ########################## |
36 | 256 | |
8
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
257 | 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
|
258 | threads = false) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
259 | tmp .= x |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
260 | center_point = center(x) .+ u |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
261 | tform = recenter(RotMatrix(theta_known[1]), center_point) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
262 | tmp = warp(x, tform, axes(x), fillvalue=Flat()) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
263 | x .= tmp |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
264 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
265 | |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
266 | petflow! = petflow_interp! |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
267 | |
36 | 268 | 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
|
269 | if dual_flow |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
270 | @backgroundif (threads==:outer) begin |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
271 | 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
|
272 | end begin |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
273 | 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
|
274 | 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
|
275 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
276 | else |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
277 | petflow!(x, Δx, u, theta_known) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
278 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
279 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
280 | |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
281 | # Method for greedy predictor |
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
282 | 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
|
283 | oldx = copy(x) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
284 | center_point = center(x) .+ u |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
285 | tform = recenter(RotMatrix(theta_known[1]), center_point) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
286 | Δx = warp(x, tform, axes(x), fillvalue=Flat()) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
287 | @. x = Δx |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
288 | @. Δy = y |
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
289 | Dxx = copy(Δy) |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
290 | DΔx = copy(Δy) |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
291 | ∇₂!(Dxx, x) |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
292 | ∇₂!(DΔx, oldx) |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
293 | inds = abs.(Dxx) .≤ 1e-2 |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
294 | Dxx[inds] .= 1 |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
295 | DΔx[inds] .= 1 |
36 | 296 | y .= y.* DΔx ./ Dxx |
8
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 | |
26
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
299 | # Method for dual scaling predictor |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
300 | 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
|
301 | oldx = copy(x) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
302 | center_point = center(x) .+ u |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
303 | tform = recenter(RotMatrix(theta_known[1]), center_point) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
304 | Δx = warp(x, tform, axes(x), fillvalue=Flat()) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
305 | @. x = Δx |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
306 | C = similar(y) |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
307 | cc = abs.(x-oldx) |
47
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
308 | cm = max(flow.threshold,maximum(cc)) |
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
309 | 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
|
310 | C[1,:,:] .= c |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
311 | C[2,:,:] .= c |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
312 | y .= C.*y |
8
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
313 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
314 | |
47
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
315 | # Experimental predictor for dual scaling based on activation |
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
316 | 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
|
317 | oldx = copy(x) |
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
318 | center_point = center(x) .+ u |
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
319 | tform = recenter(RotMatrix(theta_known[1]), center_point) |
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
320 | Δx = warp(x, tform, axes(x), fillvalue=Flat()) |
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
321 | @. x = Δx |
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
322 | C = similar(y) |
48
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
323 | cc = abs.(x-oldx) |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
324 | cm = max(1e-12,maximum(cc)) |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
325 | c = activation.(cc ./ cm) |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
326 | C[1,:,:] .= c |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
327 | C[2,:,:] .= c |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
328 | # Δx .= activation.(sqrt.(abs.(y[1,:,:]).^2 + abs.(y[2,:,:]).^2))./0.25 |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
329 | # D = similar(y) |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
330 | # D[1,:,:] .= Δx |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
331 | # D[2,:,:] .= Δx |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
332 | # y .= C.*y |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
333 | # y .= y .- 1.0.*C.*D.*y |
06f95cdd9abe
activation function trials
Neil Dizon <neil.dizon@helsinki.fi>
parents:
47
diff
changeset
|
334 | y .= y .- 1.0.*C.*y |
47
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
335 | end |
70fd92ac9da0
experimental dual scaling
Neil Dizon <neil.dizon@helsinki.fi>
parents:
42
diff
changeset
|
336 | |
8
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
337 | # 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
|
338 | 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
|
339 | @backgroundif (threads==:outer) begin |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
340 | 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
|
341 | end begin |
ccd22bbbb02f
added dummy structs for different flows
Neil Dizon <neil.dizon@helsinki.fi>
parents:
8
diff
changeset
|
342 | 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
|
343 | 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
|
344 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
345 | end |
e4ad8f7ce671
Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
5
diff
changeset
|
346 | |
36 | 347 | function petpdflow!(x, Δx, y, Δy, u, theta_known, :: ZeroDual; threads=:none) |
348 | petflow!(x, Δx, u, theta_known, threads=(threads==:inner)) | |
349 | y .= 0.0 | |
350 | end | |
351 | ||
352 | function petpdflow!(x, Δx, y, Δy, u, theta_known, :: PrimalOnly; threads=:none) | |
353 | petflow!(x, Δx, u, theta_known, threads=(threads==:inner)) | |
354 | end | |
355 | ||
0 | 356 | ########################## |
357 | # Linearised optical flow | |
358 | ########################## | |
359 | ||
360 | # ⟨⟨u, ∇b⟩⟩ | |
361 | function pointwise_gradiprod_2d!(y::Image, vtmp::Gradient, | |
362 | u::DisplacementFull, b::Image; | |
363 | add = false) | |
364 | ∇₂c!(vtmp, b) | |
365 | ||
366 | u′=reshape(u, (size(u, 1), prod(size(u)[2:end]))) | |
367 | vtmp′=reshape(vtmp, (size(vtmp, 1), prod(size(vtmp)[2:end]))) | |
368 | y′=reshape(y, prod(size(y))) | |
369 | ||
370 | if add | |
371 | @simd for i = 1:length(y′) | |
372 | @inbounds y′[i] += dot(@view(u′[:, i]), @view(vtmp′[:, i])) | |
373 | end | |
374 | else | |
375 | @simd for i = 1:length(y′) | |
376 | @inbounds y′[i] = dot(@view(u′[:, i]), @view(vtmp′[:, i])) | |
377 | end | |
378 | end | |
379 | end | |
380 | ||
381 | function pointwise_gradiprod_2d!(y::Image, vtmp::Gradient, | |
382 | u::DisplacementConstant, b::Image; | |
383 | add = false) | |
384 | ∇₂c!(vtmp, b) | |
385 | ||
386 | vtmp′=reshape(vtmp, (size(vtmp, 1), prod(size(vtmp)[2:end]))) | |
387 | y′=reshape(y, prod(size(y))) | |
388 | ||
389 | if add | |
390 | @simd for i = 1:length(y′) | |
391 | @inbounds y′[i] += dot(u, @view(vtmp′[:, i])) | |
392 | end | |
393 | else | |
394 | @simd for i = 1:length(y′) | |
395 | @inbounds y′[i] = dot(u, @view(vtmp′[:, i])) | |
396 | end | |
397 | end | |
398 | end | |
399 | ||
400 | # ∇b ⋅ y | |
401 | function pointwise_gradiprod_2dᵀ!(u::DisplacementFull, y::Image, b::Image) | |
402 | ∇₂c!(u, b) | |
403 | ||
404 | u′=reshape(u, (size(u, 1), prod(size(u)[2:end]))) | |
405 | y′=reshape(y, prod(size(y))) | |
406 | ||
407 | @simd for i=1:length(y′) | |
408 | @inbounds @. u′[:, i] *= y′[i] | |
409 | end | |
410 | end | |
411 | ||
412 | function pointwise_gradiprod_2dᵀ!(u::DisplacementConstant, y::Image, b::Image) | |
413 | @assert(size(y)==size(b) && size(u)==(2,)) | |
414 | u .= 0 | |
415 | ∇₂cfold!(b, nothing) do g, st, (i, j) | |
416 | @inbounds u .+= g.*y[i, j] | |
417 | return st | |
418 | end | |
419 | # Reweight to be with respect to 𝟙^*𝟙 inner product. | |
420 | u ./= prod(size(b)) | |
421 | end | |
422 | ||
423 | mutable struct ConstantDisplacementHornSchunckData | |
424 | M₀::Array{Float64,2} | |
425 | z::Array{Float64,1} | |
426 | Mv::Array{Float64,2} | |
427 | av::Array{Float64,1} | |
428 | cv::Float64 | |
429 | ||
430 | function ConstantDisplacementHornSchunckData() | |
431 | return new(zeros(2, 2), zeros(2), zeros(2,2), zeros(2), 0) | |
432 | end | |
433 | end | |
434 | ||
435 | # For DisplacementConstant, for the simple prox step | |
436 | # | |
437 | # (1) argmin_u 1/(2τ)|u-ũ|^2 + (θ/2)|b⁺-b+<<u-ŭ,∇b>>|^2 + (λ/2)|u-ŭ|^2, | |
438 | # | |
439 | # construct matrix M₀ and vector z such that we can solve u from | |
440 | # | |
441 | # (2) (I/τ+M₀)u = M₀ŭ + ũ/τ - z | |
442 | # | |
443 | # Note that the problem | |
444 | # | |
445 | # argmin_u 1/(2τ)|u-ũ|^2 + (θ/2)|b⁺-b+<<u-ŭ,∇b>>|^2 + (λ/2)|u-ŭ|^2 | |
446 | # + (θ/2)|b⁺⁺-b⁺+<<uʹ-u,∇b⁺>>|^2 + (λ/2)|u-uʹ|^2 | |
447 | # | |
448 | # has with respect to u the system | |
449 | # | |
450 | # (I/τ+M₀+M₀ʹ)u = M₀ŭ + M₀ʹuʹ + ũ/τ - z + zʹ, | |
451 | # | |
452 | # where the primed variables correspond to (2) for (1) for uʹ in place of u: | |
453 | # | |
454 | # argmin_uʹ 1/(2τ)|uʹ-ũʹ|^2 + (θ/2)|b⁺⁺-b⁺+<<uʹ-u,∇b⁺>>|^2 + (λ/2)|uʹ-u|^2 | |
455 | # | |
456 | function horn_schunck_reg_prox_op!(hs::ConstantDisplacementHornSchunckData, | |
457 | bnext::Image, b::Image, θ, λ, T) | |
458 | @assert(size(b)==size(bnext)) | |
459 | w = prod(size(b)) | |
460 | z = hs.z | |
461 | cv = 0 | |
462 | # Factors of symmetric matrix [a c; c d] | |
463 | a, c, d = 0.0, 0.0, 0.0 | |
464 | # This used to use ∇₂cfold but it is faster to allocate temporary | |
465 | # storage for the full gradient due to probably better memory and SIMD | |
36 | 466 | # instruction usage. |
0 | 467 | g = zeros(2, size(b)...) |
468 | ∇₂c!(g, b) | |
469 | @inbounds for i=1:size(b, 1) | |
470 | for j=1:size(b, 2) | |
471 | δ = bnext[i,j]-b[i,j] | |
472 | @. z += g[:,i,j]*δ | |
473 | cv += δ*δ | |
474 | a += g[1,i,j]*g[1,i,j] | |
475 | c += g[1,i,j]*g[2,i,j] | |
476 | d += g[2,i,j]*g[2,i,j] | |
477 | end | |
478 | end | |
479 | w₀ = λ | |
480 | w₂ = θ/w | |
481 | aʹ = w₀ + w₂*a | |
482 | cʹ = w₂*c | |
483 | dʹ = w₀ + w₂*d | |
484 | hs.M₀ .= [aʹ cʹ; cʹ dʹ] | |
485 | hs.Mv .= [w*λ+θ*a θ*c; θ*c w*λ+θ*d] | |
486 | hs.cv = cv*θ | |
487 | hs.av .= hs.z.*θ | |
488 | hs.z .*= w₂/T | |
489 | end | |
490 | ||
491 | # Solve the 2D system (I/τ+M₀)u = z | |
492 | @inline function mldivide_step_plus_sym2x2!(u, M₀, z, τ) | |
493 | a = 1/τ+M₀[1, 1] | |
494 | c = M₀[1, 2] | |
495 | d = 1/τ+M₀[2, 2] | |
496 | u .= ([d -c; -c a]*z)./(a*d-c*c) | |
497 | end | |
498 | ||
499 | function horn_schunck_reg_prox!(u::DisplacementConstant, bnext::Image, b::Image, | |
500 | θ, λ, T, τ) | |
501 | hs=ConstantDisplacementHornSchunckData() | |
502 | horn_schunck_reg_prox_op!(hs, bnext, b, θ, λ, T) | |
503 | mldivide_step_plus_sym2x2!(u, hs.M₀, (u./τ)-hs.z, τ) | |
504 | end | |
505 | ||
506 | function flow_grad!(x::Image, vtmp::Gradient, u::Displacement; δ=nothing) | |
507 | if !isnothing(δ) | |
508 | u = δ.*u | |
509 | end | |
510 | pointwise_gradiprod_2d!(x, vtmp, u, x; add=true) | |
511 | end | |
512 | ||
513 | # Error b-b_prev+⟨⟨u, ∇b⟩⟩ for Horn–Schunck type penalisation | |
514 | function linearised_optical_flow_error(u::Displacement, b::Image, b_prev::Image) | |
515 | imdim = size(b) | |
516 | vtmp = zeros(2, imdim...) | |
517 | tmp = b-b_prev | |
518 | pointwise_gradiprod_2d!(tmp, vtmp, u, b_prev; add=true) | |
519 | return tmp | |
520 | end | |
521 | ||
522 | ############################################## | |
523 | # Helper to smooth data for Horn–Schunck term | |
524 | ############################################## | |
525 | ||
526 | function filter_hs(b, b_next, b_next_filt, kernel) | |
527 | if kernel==nothing | |
528 | f = x -> x | |
529 | else | |
530 | f = x -> simple_imfilter(x, kernel; threads=true) | |
531 | end | |
532 | ||
36 | 533 | # We already filtered b in the previous step (b_next in that step) |
0 | 534 | b_filt = b_next_filt==nothing ? f(b) : b_next_filt |
535 | b_next_filt = f(b_next) | |
536 | ||
537 | return b_filt, b_next_filt | |
538 | end | |
539 | ||
540 | end # Module |