diff -r 000000000000 -r a55e35d20336 src/AlgorithmBothMulti.jl.orig --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/AlgorithmBothMulti.jl.orig Tue Apr 07 14:19:48 2020 -0500 @@ -0,0 +1,277 @@ +###################################################################### +# Predictive online PDPS for optical flow with unknown velocity field +###################################################################### + +__precompile__() + +module AlgorithmBothMulti + +using Printf + +using AlgTools.Util +import AlgTools.Iterate +using ImageTools.Gradient +using ImageTools.ImFilter + +using ..OpticalFlow: Image, + ImageSize, + DisplacementConstant, + pdflow!, + horn_schunck_reg_prox!, + pointwise_gradiprod_2d!, + horn_schunck_reg_prox_op!, + mldivide_step_plus_sym2x2!, + ConstantDisplacementHornSchunckData, + filter_hs + +using ..Algorithm: step_lengths + +######################### +# Iterate initialisation +######################### + +function init_displ(xinit::Image, ::Type{DisplacementConstant}, n::Integer) + return xinit, zeros(n, 2) +end + +# function init_displ(xinit::Image, ::Type{DisplacementFull}, n::Integer) +# return xinit, zeros(n, 2, size(xinit)...) +# end + +function init_rest(x::Image, u::DisplacementT) where DisplacementT + imdim=size(x) + + y = zeros(2, imdim...) + Δx = copy(x) + Δy = copy(y) + x̄ = copy(x) + + return x, y, Δx, Δy, x̄, u +end + +function init_iterates( :: Type{DisplacementT}, + xinit::Image, + n::Integer) where DisplacementT + return init_rest(init_displ(copy(xinit), DisplacementT, n)...) +end + +function init_iterates( :: Type{DisplacementT}, + dim::ImageSize, + n::Integer) where DisplacementT + return init_rest(init_displ(zeros(dim...), DisplacementT, n)...) +end + +############ +# Algorithm +############ + +function solve( :: Type{DisplacementT}; + dim :: ImageSize, + iterate = AlgTools.simple_iterate, + params::NamedTuple) where DisplacementT + + ###################### + # Initialise iterates + ###################### + + n = params.displacement_count + k = 0 # number of displacements we have already + + x, y, Δx, Δy, x̄, u = init_iterates(DisplacementT, dim, n) + init_data = (params.init == :data) + hs = [ConstantDisplacementHornSchunckData() for i=1:n] + #hs = Array{ConstantDisplacementHornSchunckData}(undef, n) + A = Array{Float64,3}(undef, n, 2, 2) + d = Array{Float64,2}(undef, n, 2) + + # … for tracking cumulative movement + ucumulbase = [0.0, 0.0] + + ############################################# + # Extract parameters and set up step lengths + ############################################# + + α, ρ, λ, θ = params.α, params.ρ, params.λ, params.θ + R_K² = ∇₂_norm₂₂_est² + γ = 1 + τ, σ, σ̃, ρ̃ = step_lengths(params, γ, R_K²) + + kernel = params.kernel + T = params.timestep + + #################### + # Run the algorithm + #################### + + b_next_filt = nothing + diffu = similar(u[1, :]) + + v = iterate(params) do verbose :: Function, + b :: Image, + 🚫unused_v_known :: DisplacementT, + b_next :: Image + + #################################### + # Smooth data for Horn–Schunck term + #################################### + + b_filt, b_next_filt = filter_hs(b, b_next, b_next_filt, kernel) + + ################################################ + # Prediction step + ################################################ + + # Predict x and y + if k==0 + if init_data + x .= b + init_data = false + end + else + # Displacement from previous to this image is estimated as + # the difference of their displacements from beginning of window. + if k>1 + @. @views diffu = u[k, :] - u[k-1, :] + else + @. @views diffu = u[k, :] + end + + pdflow!(x, Δx, y, Δy, diffu, params.dual_flow) + end + + # Shift stored prox matrices + if k==n + tmp = copy(u[1, :]) + ucumulbase .+= tmp + for j=1:(n-1) + @. @views u[j, :] = u[j+1, :] - tmp + hs[j] = hs[j+1] + end + # Create new struct as original contains references to objects that + # have been moved to index n-1. + hs[n]=ConstantDisplacementHornSchunckData() + else + k += 1 + end + + # Predict u: zero displacement from current to next image, i.e., + # same displacement to beginning of window. + if k==1 + @. @views u[k, :] = 0.0 + else + @. @views u[k, :] = u[k-1, :] + end + + # Predictor proximals tep + if params.prox_predict + ∇₂!(Δy, x) + @. y = (y + σ̃*Δy)/(1 + σ̃*(ρ̃+ρ/α)) + proj_norm₂₁ball!(y, α) + end + + ################################################################################# + # PDPS step + # + # For the displacements, with τ̃=τ/k, we need to solve for 2≤j