1
$\begingroup$

I would need to fit a given ellipse to a set of points and I know its size and orientation. In other words, I want to find the best translation of a given ellipse to fit a set of points. I have implemented a least squares fit like in this post : How to find the best fit ellipse to a given set of 2D points? but how to include this known ellipse shape ?

This is what I did already :

Let's pose an ellipse with center $(h, k)$ and semiaxes a and b, and tilt $\theta$. $(a,b)$ and $\theta$ are known and I am looking for the best (h,k) given a set of of $n$ two-dimensional points $\{(x_i, y_i), i=1,.., n\}$.

I used the conic equation,

$$ A x^2 + B xy + C y^2 + D x + E y + F = 0 $$

with the parametric to conic equations I have A, B and C known and $$ A = (b\cos\theta) ^2 + (a\sin\theta)^2 $$ $$ B = -2\cos\theta\sin\theta(a^2-b^2) $$ $$ C = (b\sin\theta) ^2 + (a\cos\theta)^2 $$

So I have a linear system $ G a = b $

where $ a = [D, E, F]^T$, and

$ G = \begin{bmatrix} x_1 && y_1 && 1 \\ x_2 && y_2 && 1 \\ x_3 && y_3 && 1 \\ \vdots \\ x_n && y_n && 1 \end{bmatrix}\hspace{5pt}, \hspace{25pt} b = \begin{bmatrix} - (Ax_1^2 +Bx_1y_1 + Cy_1^2) \\ - (Ax_2^2 +Bx_2y_2 + Cy_2^2) \\ - (Ax_3^2 +Bx_3y_2 + Cy_3^2) \\ \vdots \\ - (Ax_n^2 +Bx_ny_n + Cy_n^2) \end{bmatrix} $

By solving the equation I find an ellipse which definitely fit my set of points, but it also changes slightly the shape of my given ellipse, which is not what I want.

Any idea ? Thanks

$\endgroup$

1 Answer 1

1
$\begingroup$

The best fit ellipse has the equation

$ (r - r_0)^T Q (r - r0) = 1 $

where

$ r = [x,y]^T $ is the position vector of each of the data points, and $r_0=[h, k]^T$ is the center of the best fit ellipse, and $Q$ is given and is equal to,

$ Q = \begin{bmatrix} \dfrac{1}{a^2} \cos^2 \theta + \dfrac{1}{b^2} \sin^2 \theta && \cos \theta \sin \theta \bigg( \dfrac{1}{b^2} - \dfrac{1}{a^2} \bigg ) \\ \cos \theta \sin \theta \bigg( \dfrac{1}{b^2} - \dfrac{1}{a^2} \bigg) && \dfrac{1}{a^2} \sin^2 \theta + \dfrac{1}{b^2} \cos^2 \theta \end{bmatrix} $

The error function to be minimized is the sum of the squared errors from the above ellipse model and is given by

$ E = \displaystyle \sum_i \bigg( (r_i - r_0)^T Q (r - r_0 ) - 1 \bigg)^2 = \sum_i \bigg( r_i^T Q r_i - 2 r_i^T Q r_0 + r_0^T Q r_0 - 1 \bigg)^2 $

The gradient of $E$ with respect to $r_0$ is given by

$ \dfrac{\partial E}{\partial r_0} = \displaystyle 2 \sum_i \bigg( r_i^T Q r_i - 2 r_i^T Q r_0 + r_0^T Q r_0 - 1 \bigg) \bigg( - 2 Q (r_i - r_0) \bigg) = 0 $

The above equation is a set of $2$ equations in the two unknowns $r_{0x} = h $ and $r_{0y}= k $

Since these equations are nonlinear, we can use the well-known multivariate Newton-Raphson method to find the solution.

Simply select an initial guess of $r_0$, let's call it $r_0^{(0)} $, for $k = 1, 2, 3, \dots $ you can generate the next guess as follows

  1. Set $ y^{(k)} = \displaystyle \sum_i \bigg( r_i^T Q r_i - 2 r_i^T Q r_0^{(k)} + {r_0^{(k)}}^T Q {r_0^{(k)}} - 1 \bigg) \bigg( Q (r_i - r_0^{(k)}) \bigg) $

  2. Calculate the Jacobian matrix $J = [J_{ij} ]= \bigg[ \dfrac{\partial y_i}{\partial x_j} \bigg]$, where $x_1 = r_{0x}$, and $x_2 = r_{0y} $

  3. Update the estimate of the solution by the Newton-Raphson recurrence

$$ r_{0}^{(k+1)} = r_{0}^{(k)} - J^{-1} y^{(k)} $$

  1. Stop when $\| y^{(k)} \| \le \epsilon $

To calculate the Jacobian, define

$ v_i = r_i - {r_0}^{(k)}$

$ f_i = {v_i}^T Q {v_i} - 1 $

Then

$ y^{(k)} = \displaystyle \sum_i f_i Q v_i $

Then by direct differentiation, we have

$ J =\displaystyle \sum_i \bigg( -2 Q v_i v_i^T Q - f_i Q \bigg) $


$\endgroup$
4
  • $\begingroup$ Thank you very much for this detailed answer ! This may be trivial to you but could you please detail how you would calculate this Jacobian matrix J ? $\endgroup$ Commented Nov 21, 2022 at 9:43
  • $\begingroup$ I've appended my solution to include how to calculate the Jacobian. $\endgroup$ Commented Nov 21, 2022 at 10:55
  • $\begingroup$ Thank you so much, it is working great ! $\endgroup$ Commented Nov 21, 2022 at 13:51
  • $\begingroup$ @BLegu You're welcome, glad I could help. $\endgroup$ Commented Nov 21, 2022 at 14:49

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.