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