src/OpticalFlow.jl

Fri, 05 Jul 2024 14:48:02 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Fri, 05 Jul 2024 14:48:02 -0500
changeset 68
6e7fa0639be4
parent 66
dc69a0d234ae
permissions
-rw-r--r--

Added tag v2.0.2 for changeset afeaa6f6d1ae

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

mercurial