0

There is actually a post very similar to this but asking a different question from about 5 years ago.

I have a set of points which are not elliptical, and I would like to fit an ellipse to them in the least squares sense. These are the functions I found to do the computation. I checked them over briefly but couldn't find any errors.

http://nicky.vanforeest.com/misc/fitEllipse/fitEllipse.html

import numpy as np
import numpy.linalg as linalg
import matplotlib.pyplot as plt

def fitEllipse(x,y):
    x = x[:,np.newaxis]
    y = y[:,np.newaxis]
    D =  np.hstack((x*x, x*y, y*y, x, y, np.ones_like(x)))
    S = np.dot(D.T,D)
    C = np.zeros([6,6])
    C[0,2] = C[2,0] = 2; C[1,1] = -1
    E, V =  linalg.eig(np.dot(linalg.inv(S), C))
    #print E
    n = np.argmax(np.abs(E))
    a = V[:,n]
    return a

def ellipse_center(a):
    b,c,d,f,g,a = a[1]/2, a[2], a[3]/2, a[4]/2, a[5], a[0]
    num = b*b-a*c
    x0=(c*d-b*f)/num
    y0=(a*f-b*d)/num
    return np.array([x0,y0])

def ellipse_angle_of_rotation( a ):
    b,c,d,f,g,a = a[1]/2, a[2], a[3]/2, a[4]/2, a[5], a[0]
    return 0.5*np.arctan(2*b/(a-c))

def ellipse_axis_length( a ):
    b,c,d,f,g,a = a[1]/2, a[2], a[3]/2, a[4]/2, a[5], a[0]
    up = 2*(a*f*f+c*d*d+g*b*b-2*b*d*f-a*c*g)
    down1=(b*b-a*c)*( (c-a)*np.sqrt(1+4*b*b/((a-c)*(a-c)))-(c+a))
    down2=(b*b-a*c)*( (a-c)*np.sqrt(1+4*b*b/((a-c)*(a-c)))-(c+a))
    res1=np.sqrt(up/down1)
    res2=np.sqrt(up/down2)
    return np.array([res1, res2])

My problem is this: I have sets of geospatial data to which I need to fit these ellipses. In order to compare ellipses at different latitudes I need to project them all to local tangent planes. This is easy. HOWEVER: With one such sample set of data, the functions work and give me a well-formed ellipse. When I do the projection, however, it gives me an ellipse whose major axis is somehow shorter than the the minor axis, which makes computing a non-imaginary eccentricity difficult :P ($e = \sqrt{1 - b/a}$).

Even if my projection were wrong (it isn't), these functions should still be able to take these points and produce a fit. I can't figure out why with some datasets it produces nonsense geometrical data. Any ideas?

If not, does anybody have any other way to fit an ellipse to data in python?

BenL
  • 163
  • 2
  • 14
  • 1
    Considering the fact that the post contains quite some text and code you give very little information. Show a minimal "non" working example. So it is possible to reproduce your "non-sense" result – mikuszefski Dec 10 '17 at 15:03
  • I think there is a mistake in the fitting code, maybe you want to check out my answer [here](https://stackoverflow.com/a/48002645/2005415) – Jason Dec 28 '17 at 06:31

0 Answers0