src/OpticalFlow.jl

Thu, 18 Apr 2024 10:51:10 +0300

author
Neil Dizon <neil.dizon@helsinki.fi>
date
Thu, 18 Apr 2024 10:51:10 +0300
changeset 6
72bcc5f492df
parent 5
843e7611b068
child 8
e4ad8f7ce671
permissions
-rw-r--r--

commit before adding PET

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
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 ##########
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 # Exports
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 ##########
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 export flow!,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19 pdflow!,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 flow_grad!,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 flow_interp!,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 estimate_Λ²,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 estimate_linear_Λ²,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 pointwise_gradiprod_2d!,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 pointwise_gradiprod_2dᵀ!,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 horn_schunck_reg_prox!,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 horn_schunck_reg_prox_op!,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 mldivide_step_plus_sym2x2!,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 linearised_optical_flow_error,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 Image, AbstractImage, ImageSize,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 Gradient, Displacement,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 DisplacementFull, DisplacementConstant,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 HornSchunckData,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 filter_hs
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 ###############################################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 # Types (several imported from ImageTools.Translate)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 ###############################################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 Image = Translate.Image
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 AbstractImage = AbstractArray{Float64,2}
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 Displacement = Translate.Displacement
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43 DisplacementFull = Translate.DisplacementFull
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 DisplacementConstant = Translate.DisplacementConstant
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 Gradient = Array{Float64,3}
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 ImageSize = Tuple{Int64,Int64}
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47
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 # Displacement field based flow
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 #################################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52 function flow_interp!(x::AbstractImage, u::Displacement, tmp::AbstractImage;
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 threads = false)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54 tmp .= x
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55 Translate.translate_image!(x, tmp, u; threads=threads)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 end
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 function flow_interp!(x::AbstractImage, u::Displacement;
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59 threads = false)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
60 tmp = copy(x)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
61 Translate.translate_image!(x, tmp, u; threads=threads)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
62 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
63
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
64 flow! = flow_interp!
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
65
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
66 function pdflow!(x, Δx, y, Δy, u, dual_flow; threads=:none)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
67 if dual_flow
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
68 #flow!((x, @view(y[1, :, :]), @view(y[2, :, :])), diffu,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
69 # (Δx, @view(Δy[1, :, :]), @view(Δy[2, :, :])))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
70 @backgroundif (threads==:outer) begin
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
71 flow!(x, u, Δx; threads=(threads==:inner))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
72 end begin
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
73 flow!(@view(y[1, :, :]), u, @view(Δy[1, :, :]); threads=(threads==:inner))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
74 flow!(@view(y[2, :, :]), u, @view(Δy[2, :, :]); threads=(threads==:inner))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
75 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
76 else
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
77 flow!(x, u, Δx)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
78 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
79 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
80
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
81 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
82 if dual_flow
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
83 @backgroundif (threads==:outer) begin
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
84 flow!(x, u, Δx; threads=(threads==:inner))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
85 flow!(z, u, Δz; threads=(threads==:inner))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
86 end begin
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
87 flow!(@view(y[1, :, :]), u, @view(Δy[1, :, :]); threads=(threads==:inner))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
88 flow!(@view(y[2, :, :]), u, @view(Δy[2, :, :]); threads=(threads==:inner))
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 else
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
91 flow!(x, u, Δx; threads=(threads==:inner))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
92 flow!(z, u, Δz; threads=(threads==:inner))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
93 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
94 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
95
5
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
96 # Additional method for Greedy
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
97 function pdflow!(x, Δx, y, Δy, u; threads=:none)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
98 @assert(size(u)==(2,))
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
99 Δx .= x
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
100 Δy .= y
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
101 flow!(x, u; threads=(threads==:inner))
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
102 Dxx = similar(Δy)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
103 DΔx = similar(Δy)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
104 ∇₂!(Dxx, x)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
105 ∇₂!(DΔx, Δx)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
106 inds = abs.(Dxx) .≤ 1e-1
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
107 Dxx[inds] .= 1
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
108 DΔx[inds] .= 1
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
109 y .= y.* DΔx ./ Dxx
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
110 end
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
111
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
112 # Additional method for Rotation
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
113 function pdflow!(x, Δx, y, u; threads=:none)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
114 @assert(size(u)==(2,))
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
115 Δx .= x
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
116 flow!(x, u; threads=(threads==:inner))
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
117
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
118 (m,n) = size(x)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
119 dx = similar(y)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
120 dx_banana = similar(y)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
121 ∇₂!(dx, Δx)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
122 ∇₂!(dx_banana, x)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
123
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
124 for i=1:m
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
125 for j=1:n
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
126 ndx = @views sum(dx[:, i, j].^2)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
127 ndx_banana = @views sum(dx_banana[:, i, j].^2)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
128 if ndx > 1e-4 && ndx_banana > 1e-4
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
129 A = dx[:, i, j]
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
130 B = dx_banana[:, i, j]
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
131 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
132 cos_theta = cos(theta)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
133 sin_theta = sin(theta)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
134 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
135 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
136 y[1, i, j] = a
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
137 y[2, i, j] = b
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
138 end
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
139 end
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
140 end
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
141 end
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
142
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
143 # Additional method for Dual Scaling
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
144 function pdflow!(x, y, u; threads=:none)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
145 @assert(size(u)==(2,))
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
146 oldx = copy(x)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
147 flow!(x, u; threads=(threads==:inner))
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
148 C = similar(y)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
149 cc = abs.(x-oldx)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
150 cm = max(1e-12,maximum(cc))
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
151 c = 1 .* (1 .- cc./ cm) .^(10)
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
152 C[1,:,:] .= c
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
153 C[2,:,:] .= c
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
154 y .= C.*y
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
155 end
843e7611b068 added new predictors
Neil Dizon <neil.dizon@helsinki.fi>
parents: 0
diff changeset
156
0
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
157 ##########################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
158 # Linearised optical flow
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
159 ##########################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
160
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
161 # ⟨⟨u, ∇b⟩⟩
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
162 function pointwise_gradiprod_2d!(y::Image, vtmp::Gradient,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
163 u::DisplacementFull, b::Image;
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
164 add = false)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
165 ∇₂c!(vtmp, b)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
166
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
167 u′=reshape(u, (size(u, 1), prod(size(u)[2:end])))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
168 vtmp′=reshape(vtmp, (size(vtmp, 1), prod(size(vtmp)[2:end])))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
169 y′=reshape(y, prod(size(y)))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
170
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
171 if add
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
172 @simd for i = 1:length(y′)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
173 @inbounds y′[i] += dot(@view(u′[:, i]), @view(vtmp′[:, i]))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
174 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
175 else
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
176 @simd for i = 1:length(y′)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
177 @inbounds y′[i] = dot(@view(u′[:, i]), @view(vtmp′[:, i]))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
178 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
179 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
180 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
181
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
182 function pointwise_gradiprod_2d!(y::Image, vtmp::Gradient,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
183 u::DisplacementConstant, b::Image;
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
184 add = false)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
185 ∇₂c!(vtmp, b)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
186
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
187 vtmp′=reshape(vtmp, (size(vtmp, 1), prod(size(vtmp)[2:end])))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
188 y′=reshape(y, prod(size(y)))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
189
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
190 if add
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
191 @simd for i = 1:length(y′)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
192 @inbounds y′[i] += dot(u, @view(vtmp′[:, i]))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
193 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
194 else
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
195 @simd for i = 1:length(y′)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
196 @inbounds y′[i] = dot(u, @view(vtmp′[:, i]))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
197 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
198 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
199 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
200
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
201 # ∇b ⋅ y
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
202 function pointwise_gradiprod_2dᵀ!(u::DisplacementFull, y::Image, b::Image)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
203 ∇₂c!(u, b)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
204
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
205 u′=reshape(u, (size(u, 1), prod(size(u)[2:end])))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
206 y′=reshape(y, prod(size(y)))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
207
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
208 @simd for i=1:length(y′)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
209 @inbounds @. u′[:, i] *= y′[i]
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
210 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
211 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
212
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
213 function pointwise_gradiprod_2dᵀ!(u::DisplacementConstant, y::Image, b::Image)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
214 @assert(size(y)==size(b) && size(u)==(2,))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
215 u .= 0
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
216 ∇₂cfold!(b, nothing) do g, st, (i, j)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
217 @inbounds u .+= g.*y[i, j]
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
218 return st
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
219 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
220 # Reweight to be with respect to 𝟙^*𝟙 inner product.
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
221 u ./= prod(size(b))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
222 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
223
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
224 mutable struct ConstantDisplacementHornSchunckData
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
225 M₀::Array{Float64,2}
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
226 z::Array{Float64,1}
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
227 Mv::Array{Float64,2}
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
228 av::Array{Float64,1}
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
229 cv::Float64
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
230
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
231 function ConstantDisplacementHornSchunckData()
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
232 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
233 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
234 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
235
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
236 # For DisplacementConstant, for the simple prox step
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
237 #
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
238 # (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
239 #
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
240 # 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
241 #
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
242 # (2) (I/τ+M₀)u = M₀ŭ + ũ/τ - z
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
243 #
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
244 # Note that the problem
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
245 #
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
246 # 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
247 # + (θ/2)|b⁺⁺-b⁺+<<uʹ-u,∇b⁺>>|^2 + (λ/2)|u-uʹ|^2
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
248 #
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
249 # has with respect to u the system
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 # (I/τ+M₀+M₀ʹ)u = M₀ŭ + M₀ʹuʹ + ũ/τ - z + zʹ,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
252 #
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
253 # 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
254 #
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
255 # 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
256 #
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
257 function horn_schunck_reg_prox_op!(hs::ConstantDisplacementHornSchunckData,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
258 bnext::Image, b::Image, θ, λ, T)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
259 @assert(size(b)==size(bnext))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
260 w = prod(size(b))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
261 z = hs.z
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
262 cv = 0
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
263 # Factors of symmetric matrix [a c; c d]
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
264 a, c, d = 0.0, 0.0, 0.0
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
265 # This used to use ∇₂cfold but it is faster to allocate temporary
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
266 # storage for the full gradient due to probably better memory and SIMD
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
267 # instruction usage.
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
268 g = zeros(2, size(b)...)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
269 ∇₂c!(g, b)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
270 @inbounds for i=1:size(b, 1)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
271 for j=1:size(b, 2)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
272 δ = bnext[i,j]-b[i,j]
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
273 @. z += g[:,i,j]*δ
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
274 cv += δ*δ
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
275 a += g[1,i,j]*g[1,i,j]
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
276 c += g[1,i,j]*g[2,i,j]
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
277 d += g[2,i,j]*g[2,i,j]
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
278 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
279 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
280 w₀ = λ
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
281 w₂ = θ/w
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
282 aʹ = w₀ + w₂*a
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
283 cʹ = w₂*c
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
284 dʹ = w₀ + w₂*d
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
285 hs.M₀ .= [aʹ cʹ; cʹ dʹ]
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
286 hs.Mv .= [w*λ+θ*a θ*c; θ*c w*λ+θ*d]
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
287 hs.cv = cv*θ
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
288 hs.av .= hs.z.*θ
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
289 hs.z .*= w₂/T
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
290 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
291
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
292 # Solve the 2D system (I/τ+M₀)u = z
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
293 @inline function mldivide_step_plus_sym2x2!(u, M₀, z, τ)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
294 a = 1/τ+M₀[1, 1]
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
295 c = M₀[1, 2]
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
296 d = 1/τ+M₀[2, 2]
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
297 u .= ([d -c; -c a]*z)./(a*d-c*c)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
298 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
299
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
300 function horn_schunck_reg_prox!(u::DisplacementConstant, bnext::Image, b::Image,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
301 θ, λ, T, τ)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
302 hs=ConstantDisplacementHornSchunckData()
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
303 horn_schunck_reg_prox_op!(hs, bnext, b, θ, λ, T)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
304 mldivide_step_plus_sym2x2!(u, hs.M₀, (u./τ)-hs.z, τ)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
305 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
306
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
307 function flow_grad!(x::Image, vtmp::Gradient, u::Displacement; δ=nothing)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
308 if !isnothing(δ)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
309 u = δ.*u
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
310 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
311 pointwise_gradiprod_2d!(x, vtmp, u, x; add=true)
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 # Error b-b_prev+⟨⟨u, ∇b⟩⟩ for Horn–Schunck type penalisation
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
315 function linearised_optical_flow_error(u::Displacement, b::Image, b_prev::Image)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
316 imdim = size(b)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
317 vtmp = zeros(2, imdim...)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
318 tmp = b-b_prev
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
319 pointwise_gradiprod_2d!(tmp, vtmp, u, b_prev; add=true)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
320 return tmp
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
321 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
322
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
323 ##############################################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
324 # Helper to smooth data for Horn–Schunck term
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
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
327 function filter_hs(b, b_next, b_next_filt, kernel)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
328 if kernel==nothing
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
329 f = x -> x
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
330 else
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
331 f = x -> simple_imfilter(x, kernel; threads=true)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
332 end
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 # 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
335 b_filt = b_next_filt==nothing ? f(b) : b_next_filt
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
336 b_next_filt = f(b_next)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
337
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
338 return b_filt, b_next_filt
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
339 end
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 end # Module

mercurial