How to fit a conic using least-square method?

2.4k Views Asked by At

I have a problem and I haven´t been able to solve it. The problem is in the area of least-square fitting. Someone drew a sort of "conic" figure on a canvas (i.e. a MATLAB plot) so I have a series of points ($x_i$, $y_i$). Now I need for this points to adjust to a perfect "conic" using least-square fitting.

I now how to do this over a line $f(x) = ax + b$, but I don't know what to do with the general conic equation $Ax^2+Bxy+Cy^2+Dx+Ey+F = 0$

Can someone point me into the right direction.

Some remarks I've been working around:

  • that is an equation, not a function, do i need to parametrize it? how to?
  • Don't know what to do with rectangle term ($Bxy$)

Thanks in advance.

PS: If there was something unclear, please say so and I'll try to explain myself.

PS.1: MATLAB code will be appreciated

Edit:

After checking what @ClaudeLeibovici suggested I got working an example on MATLAB where after getting the points I solve the system of equations given in the french paper and It plotted a perfet circle as I needed (using ezplot with the "explicit equation"), but it does not satisfies Hyperbolas or Parabolas, just circles and elipses. And i need to fit any conic.

Sometimes I get what it looks like to be an hyperbola, sometimes not in the right direction and sometimes not even close to the one I "drew".

Anyhow, I will apreciate if someone could explain me where did the "Generalization for conics" from the paper came from? and what does it means in a least-square sense?

I tried reading it, but couldn't understand it very well, cause I do not speak nor understand french, and the translation wasn't good. I need to understand what is going on cause I need to do a presentation about this.

Thanks in advance

PS: I used $F=1$ as suggested.

2

There are 2 best solutions below

4
On

The basic idea is to minimize $$\Phi=\sum_{i=1}^n (Ax_i^2+Bx_iy_i+Cy_i^2+Dx_i+Ey_i+F)^2$$ Take the derivatives with respect to each parameter and set it equal to $0$.

If you are lazy, define $z_i=0$ for all $i$'s and perform a least square fit for $$z=Ax^2+Bxy+Cy^2+Dx+Ey+F$$ Just a multilinear regression then.

Warning : As JeanMarie commented, set $F=1$ or whatever number you want. This fix to $5$ the number of parameters to be adjusted. And $5$ is the minimum number of points which define a general conic not going through the origin.

2
On

I will assume that this drawing is closed, so that you have a set of points that you want to fit an ellipse to and that these points are approximately equally spaced.

First, get the average $x$ and $y$ values as $x_0$ and $y_0$. This shows the center of the ellipse. Subtract $(x_0, y_0)$ from each point.

Next, do an orientation-independent linear least squares fit to the modified data points. Here is a link to my method of doing this:

linear least squares minimizing distance from points to rays - is it possible?

For each angle $\theta$ in the expression $D=D_1+R\cos(2\theta-\phi)$, where $R$ and $\phi$ are specified in my answer, the value of $D$ is the sum of the squares of the errors of the line at angle $\theta$.

By choosing $\theta$ so $\cos(2\theta-\phi) = -1$ (i.e. $\theta = (\phi+\pi)/2$), the sum is minimized, and by choosing $\theta$ so $\cos(2\theta-\phi) = 1$ (i.e. $\theta = (\phi-\pi)/2$), the sum is maximized.

These values, $D_1 \pm R$, give the square of the values of the major and minor axes of the ellipse, scaled by $\sqrt{2}$ (this is my vague recollection - I did this over thirty years ago).

This method is linear and gives you the center, major axis, minor axis, and orientation of the major axis of the ellipse.

You may have to play around with it to scale and select the length of the axes, but it should work.