src/OpticalFlow.jl

Sun, 21 Apr 2024 21:00:57 +0300

author
Neil Dizon <neil.dizon@helsinki.fi>
date
Sun, 21 Apr 2024 21:00:57 +0300
changeset 24
2d9e64235ba7
parent 8
e4ad8f7ce671
child 26
ccd22bbbb02f
permissions
-rw-r--r--

renamed generate_radon as generate_sinogram

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

mercurial