strym.tools.ellipse_fit¶
-
strym.tools.
ellipse_fit
(x, y, fit_circle=False)¶ Fits an ellipse to the data point via least square method.
- Parameters
- X : numpy.array | pandas.Series | list
X-coordinates of the cluster to fit
- Y : numpy.array | pandas.Series | list
Y-coordinates of the cluster to fit
- fit_circle : bool
Fit circle instead of Ellipse (i.e. when Major Axis = Minor Axis)
- Returns
double, double, double, double, `double – X-coordinate of fitted Circle/Eillipse’s center, Y-coordinate of fitted Circle/Eillipse’s center, Length of Semi-Major Axis, Length of Semi-Minor Axis, Residual As a goodness of fit
Notes
Circle-fitting
Circle fitting is done using Equation
\[F(X) = aX^T X + b^T x + c = 0\]Minimizing algebraic distance
By putting all x, y corrdinates X=(x,y) into above equation and get the system of equaion \(Bu = 0\) with \(u = (a, b_1, b_2, c)^T\)
For \(m > 3\) we cannot expect the system to have a solution, unless all the points are on circle. Hence, we solve overdetermined system Bu = r where u is chosen to minimize \(||r||\) where \(|| ||\) is L2 norm. Hence the problem is minimize \(||Bu||\) subject to \(||u|| = 1\).
Minimizing geometrical distance
Equation can also be written as
\[(x + \cfrac{b_1}{2a})^2 + (y + \cfrac{b_2}{2a})^2 = \cfrac{||b||^2}{4a^2 - (c/a)}\]In this case, we minimize the sum of squares of the distance \(d_i^2 = (||z - xi||- r)^2\)
Let parameters be \(u = (z_1, z_2, r)\)
Then our problem becomes minimize \(\sum d_i(u)^2\)
Ellipse fitting
Ellipse is a special case of a general conic \(F(x,y) = ax^2 + bxy + cy^2 + dx + ey + f = 0\) subject to the constraint \(b^2 - 4ac < 0\).