# Help with Levenberg-Marquardt Optimization in Homography

**URL:** https://discourse.julialang.org/t/help-with-levenberg-marquardt-optimization-in-homography/112740
**Category:** General Usage
**Created:** [April 9, 2024, 5:06pm UTC](https://discourse.julialang.org/t/help-with-levenberg-marquardt-optimization-in-homography/112740 "2024-04-09T17:06:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dennispureza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dennispureza/32/50957_2.png) [@dennispureza](https://discourse.julialang.org/u/dennispureza)
#### Post date: [April 9, 2024, 5:06pm UTC](https://discourse.julialang.org/t/help-with-levenberg-marquardt-optimization-in-homography/112740/1 "2024-04-09T17:06:18Z")

</div>

Hi!

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)

Where:

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 can`t 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 don`t know if it is possible).

Any help? I appreciate it.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [April 9, 2024, 5:30pm UTC](https://discourse.julialang.org/t/help-with-levenberg-marquardt-optimization-in-homography/112740/2 "2024-04-09T17:30:00Z")

</div>

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

> **[GitHub - SciML/Optimization.jl: Mathematical Optimization in Julia. Local,...](https://github.com/SciML/Optimization.jl)**
>
> Mathematical Optimization in Julia. Local, global, gradient-based and derivative-free. Linear, Quadratic, Convex, Mixed-Integer, and Nonlinear Optimization in one simple, fast, and differentiable i...

---

<div class="post-metadata">

### Author: ![avikpal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avikpal/32/6550_2.png) [@avikpal](https://discourse.julialang.org/u/avikpal)
#### Post date: [April 9, 2024, 5:42pm UTC](https://discourse.julialang.org/t/help-with-levenberg-marquardt-optimization-in-homography/112740/3 "2024-04-09T17:42:11Z")

</div>

LM is part of NonlinearSolve.jl ([NonlinearSolve.jl Solvers · NonlinearSolve.jl](https://docs.sciml.ai/NonlinearSolve/stable/native/solvers/#NonlinearSolve.LevenbergMarquardt))

---

<div class="post-metadata">

### Author: ![dennispureza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dennispureza/32/50957_2.png) [@dennispureza](https://discourse.julialang.org/u/dennispureza)
#### Post date: [April 24, 2024, 5:57pm UTC](https://discourse.julialang.org/t/help-with-levenberg-marquardt-optimization-in-homography/112740/4 "2024-04-24T17:57:13Z")

</div>

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?

```julia
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
        
    end

    return Error

end

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
julia> h
9-element Vector{Float64}:
   0.5064311920323017
  -0.029262951827130214
 255.07130841748545
  -0.1187228809135697
   0.23194964095314136
 194.75919975350502
   7.778410652589481e-5
  -0.0004114037953009441
   0.5869894007853151

```

The results:

```julia
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

```
