Help with Levenberg-Marquardt Optimization in Homography


I have to solve an optmization problem of refining a Homography matrix using the Levenberg-Marquardt method. My reference is in Java and calls a function in the following way:

Optimize(Val; Jac; X; Y; h)


Val is the value function (vector of projected coordinates)
Jac is the Jacobian (partial derivatives to the nine homograpgy parameters)
X are the model points
Y are the observed points
h is the initial estimate to the homography matrix

I have looked around but cant figure a way to call a optimization function in julia in a similar way, maybe even with another method or with a automated jacobian (I dont know if it is possible).

Any help? I appreciate it.

You will find most of what you need in the following package:

LM is part of NonlinearSolve.jl (NonlinearSolve.jl Solvers · NonlinearSolve.jl)

1 Like

Hello there! I have made some progress in this topic. I managed to use the LeastSquaresOptim package without error messages, however, it seems that even though I get a number of iterations done, the minimizer is almost the same as the initial set of parameters. I will post here the error function i am trying to minimize and the results.

hom() and invhom() are functions to convert points to homogeneous and back to cartesian coordinates. XYworld are the world points positions and XYobs are the observed image points.

Am I doing something wrong?

function Eproj(h)

    H = [h[1] h[2] h[3];h[4] h[5] h[6];h[7] h[8] h[9]]
    Error = 0

    for j in 1:nPoints

        XYw = XYworld[j,:]
        XYo = XYobs[j,:]

        Error += norm(XYo - invhom(H*hom(XYw)))^2

    return Error


h = [H[1,1];H[1,2];H[1,3];H[2,1];H[2,2];H[2,3];H[3,1];H[3,2];H[3,3]] #initial set of parameters

res = LeastSquaresOptim.optimize(Eproj,h,LeastSquaresOptim.LevenbergMarquardt()) 

The initial set of parameters:

julia> h
9-element Vector{Float64}:

The results:

Results of Optimization Algorithm
 * Algorithm: LevenbergMarquardt
 * Minimizer: [0.5064311920323017,-0.029262951827130228,255.07130841748545,-0.11872288091356972,0.23194964095314136,194.75919975350502,7.778410652589478e-5,-0.0004114037953009441,0.5869894007853151]
 * Sum of squares at Minimum: 0.004988
 * Iterations: 9
 * Convergence: true
 * |x - x'| < 1.0e-08: false
 * |f(x) - f(x')| / |f(x)| < 1.0e-08: true
 * |g(x)| < 1.0e-08: false
 * Function Calls: 10
 * Gradient Calls: 1
 * Multiplication Calls: 27