diff --git a/src/cost_function/ModuleCostFunction.jl b/src/cost_function/ModuleCostFunction.jl index 4d5ab66..bce7453 100644 --- a/src/cost_function/ModuleCostFunction.jl +++ b/src/cost_function/ModuleCostFunction.jl @@ -1,6 +1,6 @@ module ModuleCostFunction using MultipleViewGeometry.ModuleTypes, MultipleViewGeometry.ModuleOperators, MultipleViewGeometry.ModuleMathAliases using MultipleViewGeometry.ModuleCarriers -export cost, X +export cost, X, H include("cost_functions.jl") end diff --git a/src/cost_function/cost_functions.jl b/src/cost_function/cost_functions.jl index 0b19f16..0c5f615 100644 --- a/src/cost_function/cost_functions.jl +++ b/src/cost_function/cost_functions.jl @@ -64,3 +64,64 @@ end function ฮฃโ‚™(entity::FundamentalMatrix, ๐›‰::Matrix, ๐โ‚™::Matrix) ๐›‰' * ๐โ‚™ * ๐›‰ end + +function H(c::CostFunction, entity::FundamentalMatrix, ๐›‰::Matrix, ฮ›::Vector{T1}, matches...) where T1 <: Matrix + โ„ณ, โ„ณสน = matches + N = length(โ„ณ) + if (N != length(โ„ณสน)) + throw(ArgumentError("There should be an equal number of points for each view.")) + end + if (N != length(ฮ›)) + throw(ArgumentError("There should be a covariance matrix for each point correspondence.")) + end + _H(c, entity, ๐›‰, ฮ›, โ„ณ, โ„ณสน) +end + + +function _H(c::CostFunction, entity::ProjectiveEntity, ๐›‰::Matrix, ฮ›::Vector{T1}, ๐’Ÿ...) where T1 <: Matrix + ๐— = X(c, entity, ๐›‰, ฮ›, ๐’Ÿ...) + ๐“ = T(c, entity, ๐›‰, ฮ›, ๐’Ÿ...) + ๐‡ = 2*(๐—-๐“) +end + + +function T(c::CostFunction, entity::ProjectiveEntity, ๐›‰::Matrix, ฮ›::Vector{T1}, ๐’Ÿ...) where T1 <: Matrix + l = length(๐›‰) + ๐ˆโ‚— = eye(l) + ๐ˆโ‚˜ = Iโ‚˜(entity) + ๐ = fill(0.0,(l,l)) + ๐Œ = fill(0.0,(l,l)) + ๐“ = fill(0.0,(l,l)) + n = 1 + for dataโ‚™ in zip(๐’Ÿ...) + ๐’Ÿโ‚™ = dataโ‚™ + ๐šฒโ‚™ = ฮ›[n] + ๐”โ‚™ = uโ‚“(entity,๐’Ÿโ‚™) + โˆ‚โ‚“๐ฎโ‚™ = โˆ‚โ‚“u(entity, ๐’Ÿโ‚™) + ๐โ‚™ = โˆ‚โ‚“๐ฎโ‚™ * ๐šฒโ‚™ * โˆ‚โ‚“๐ฎโ‚™' + ๐šบโ‚™ = ฮฃโ‚™(entity,๐›‰, ๐โ‚™) + ๐šบโ‚™โปยน = inv(๐šบโ‚™) + ๐“โ‚ = fill(0.0,(l,l)) + ๐“โ‚‚ = fill(0.0,(l,l)) + ๐“โ‚ƒ = fill(0.0,(l,l)) + ๐“โ‚„ = fill(0.0,(l,l)) + ๐“โ‚… = fill(0.0,(l,l)) + for k = 1:l + ๐žโ‚– = fill(0.0,(l,1)) + ๐žโ‚–[k] = 1 + โˆ‚๐žโ‚–๐šบโ‚™ = (๐ˆโ‚˜ โŠ— ๐žโ‚–') * ๐โ‚™ * (๐ˆโ‚˜ โŠ— ๐›‰) + (๐ˆโ‚˜ โŠ— ๐›‰') * ๐โ‚™ * (๐ˆโ‚˜ โŠ— ๐žโ‚–) + ๐“โ‚ = ๐“โ‚ + ๐”โ‚™ * ๐šบโ‚™โปยน * (โˆ‚๐žโ‚–๐šบโ‚™) * ๐šบโ‚™โปยน * ๐”โ‚™' * ๐›‰ * ๐žโ‚–' + ๐“โ‚‚ = ๐“โ‚‚ + (๐žโ‚–' * ๐”โ‚™ * ๐šบโ‚™โปยน โŠ— ๐ˆโ‚—) * ๐โ‚™ * (๐šบโ‚™โปยน * ๐”โ‚™' * ๐›‰ โŠ— ๐ˆโ‚—) * ๐›‰ * ๐žโ‚–' + ๐“โ‚ƒ = ๐“โ‚ƒ + (๐›‰' * ๐”โ‚™ * ๐šบโ‚™โปยน โŠ— ๐ˆโ‚—) * ๐โ‚™ * (๐ˆโ‚˜ โŠ— ๐›‰) * ๐šบโ‚™โปยน * ๐”โ‚™' + ๐“โ‚„ = ๐“โ‚„ + (๐›‰' * ๐”โ‚™ * ๐šบโ‚™โปยน * (โˆ‚๐žโ‚–๐šบโ‚™) * ๐šบโ‚™โปยน โŠ— ๐ˆโ‚—) * ๐โ‚™ * (๐šบโ‚™โปยน * ๐”โ‚™' * ๐›‰ โŠ— ๐ˆโ‚—) * ๐›‰ * ๐žโ‚–' + ๐“โ‚… = ๐“โ‚… + (๐›‰' * ๐”โ‚™ * ๐šบโ‚™โปยน โŠ— ๐ˆโ‚—) * ๐โ‚™ * (๐šบโ‚™โปยน * (โˆ‚๐žโ‚–๐šบโ‚™) * ๐šบโ‚™โปยน * ๐”โ‚™' * ๐›‰ โŠ— ๐ˆโ‚—) * ๐›‰ * ๐žโ‚–' + end + ๐“ = ๐“ + ๐“โ‚ + ๐“โ‚‚ + ๐“โ‚ƒ - ๐“โ‚„ - ๐“โ‚… + n = n + 1 + end + ๐“ +end + +function Iโ‚˜(entity::FundamentalMatrix) + eye(1) +end diff --git a/test/cost_functions_tests.jl b/test/cost_functions_tests.jl index 638d33e..5ff7f82 100644 --- a/test/cost_functions_tests.jl +++ b/test/cost_functions_tests.jl @@ -49,6 +49,15 @@ Jโ‚โ‚˜โ‚— = cost(AML(),FundamentalMatrix(), ๐Ÿ , # The true parameters should lie in the null space of the matrix X. @test all(isapprox.(๐— * ๐Ÿ, 0.0; atol = 1e-10)) +# Verify that the the vectorised fundamental matrix is in the null space of H. +# H represents the Hessian matrix of the AML cost function. +๐‡ = H(AML(),FundamentalMatrix(), reshape(๐…,9,1), + [eye(4) for i = 1:length(โ„ณ)], โ„ณ, โ„ณสน) + +# The true parameters should lie in the null space of the matrix H. +@test all(isapprox.(๐‡ * ๐Ÿ, 0.0; atol = 1e-10)) + + # matches = โ„ณ, โ„ณสน # ฮ› = [eye(4) for i = 1:length(โ„ณ)] # zip(matches, ฮ›)