Note
Go to the end to download the full example code.
Fitting and non-linear refinement of an ellipse¶
This example shows an algebraically fitted ellipse and its non-linearly refined variant, together with the orthogonal contact points on the refined ellipse.
from conics import Ellipse
from conics.fitting import fit_dlt
from conics.fitting import fit_nievergelt
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import numpy as np
pts = np.asarray([[0.35, 1.2], [1.5, 1.2], [-2.3, 4.2], [-1, -1.2], [0, -2], [-1, -3]])
pts = np.array([[1, 2, 5, 7, 9, 3, 6, 8], [7, 6, 8, 7, 5, 7, 2, 4]]).T
C = fit_dlt(pts)
C = fit_nievergelt(pts, type='ellipse', scale=True)
e = Ellipse.from_conic(C)
e1 = e.refine(pts)
contact_pts = e.contact(pts)
width, height = 2 * np.asarray(e.major_minor)
ee = mpatches.Ellipse(
e.center,
width,
height,
angle=np.rad2deg(e.alpha),
edgecolor='red',
facecolor='none',
lw=2,
)
width1, height1 = 2 * np.asarray(e1.major_minor)
ee1 = mpatches.Ellipse(
e1.center,
width1,
height1,
angle=np.rad2deg(e1.alpha),
edgecolor='blue',
facecolor='none',
lw=2,
)
plt.figure()
plt.axis('equal')
plt.gca().add_patch(ee)
plt.gca().add_patch(ee1)
plt.scatter(*pts.T)
plt.scatter(*contact_pts.T)
plt.show()
Total running time of the script: (0 minutes 0.378 seconds)