MrSinan commited on
Commit
4b9031a
·
1 Parent(s): e12b6c3

Upload fit_ellipse.py

Browse files
Files changed (1) hide show
  1. fit_ellipse.py +64 -0
fit_ellipse.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Author: aqeelanwar
2
+ # Created: 4 May,2020, 1:30 AM
3
+ # Email: aqeel.anwar@gatech.edu
4
+
5
+ import numpy as np
6
+ from numpy.linalg import eig, inv
7
+
8
+ def fitEllipse(x,y):
9
+ x = x[:,np.newaxis]
10
+ y = y[:,np.newaxis]
11
+ D = np.hstack((x*x, x*y, y*y, x, y, np.ones_like(x)))
12
+ S = np.dot(D.T,D)
13
+ C = np.zeros([6,6])
14
+ C[0,2] = C[2,0] = 2; C[1,1] = -1
15
+ E, V = eig(np.dot(inv(S), C))
16
+ n = np.argmax(np.abs(E))
17
+ a = V[:,n]
18
+ return a
19
+
20
+ def ellipse_center(a):
21
+ b,c,d,f,g,a = a[1]/2, a[2], a[3]/2, a[4]/2, a[5], a[0]
22
+ num = b*b-a*c
23
+ x0=(c*d-b*f)/num
24
+ y0=(a*f-b*d)/num
25
+ return np.array([x0,y0])
26
+
27
+
28
+ def ellipse_angle_of_rotation( a ):
29
+ b,c,d,f,g,a = a[1]/2, a[2], a[3]/2, a[4]/2, a[5], a[0]
30
+ return 0.5*np.arctan(2*b/(a-c))
31
+
32
+
33
+ def ellipse_axis_length( a ):
34
+ b,c,d,f,g,a = a[1]/2, a[2], a[3]/2, a[4]/2, a[5], a[0]
35
+ up = 2*(a*f*f+c*d*d+g*b*b-2*b*d*f-a*c*g)
36
+ down1=(b*b-a*c)*( (c-a)*np.sqrt(1+4*b*b/((a-c)*(a-c)))-(c+a))
37
+ down2=(b*b-a*c)*( (a-c)*np.sqrt(1+4*b*b/((a-c)*(a-c)))-(c+a))
38
+ res1=np.sqrt(up/down1)
39
+ res2=np.sqrt(up/down2)
40
+ return np.array([res1, res2])
41
+
42
+ def ellipse_angle_of_rotation2( a ):
43
+ b,c,d,f,g,a = a[1]/2, a[2], a[3]/2, a[4]/2, a[5], a[0]
44
+ if b == 0:
45
+ if a > c:
46
+ return 0
47
+ else:
48
+ return np.pi/2
49
+ else:
50
+ if a > c:
51
+ return np.arctan(2*b/(a-c))/2
52
+ else:
53
+ return np.pi/2 + np.arctan(2*b/(a-c))/2
54
+
55
+ # a = fitEllipse(x,y)
56
+ # center = ellipse_center(a)
57
+ # #phi = ellipse_angle_of_rotation(a)
58
+ # phi = ellipse_angle_of_rotation2(a)
59
+ # axes = ellipse_axis_length(a)
60
+ #
61
+ # print("center = ", center)
62
+ # print("angle of rotation = ", phi)
63
+ # print("axes = ", axes)
64
+