如何将 2D 椭圆拟合到给定的点和给定的焦点
How to fit a 2D ellipse to given points and a given focus
此问题类似于如何将二维椭圆(在 x-y 平面中)拟合到给定点的问题? (见下面的link)
()
现在,通过Casey提供的代码,我们知道了如何使用最小二乘法来拟合具有给定点的二维椭圆。 (下面也提供了代码。)根据这段代码,如果我不仅要拟合给定的点,还要拟合给定的焦点(0,0),我该怎么做?或者有什么更好的方法吗?
我在考虑是否可以根据椭圆方程 Ax^2 + Bxy + Cy^2 + Dx + Ey=1 导出焦点(作为 x 和 y 的函数),其中 A, B , C, D, E 为系数,作为对最小二乘法的约束。但是很遗憾,我也不知道如何获得焦点。
代码
import numpy as np
import matplotlib.pyplot as plt
alpha = 5
beta = 3
N = 500
DIM = 2
np.random.seed(2)
# Generate random points on the unit circle by sampling uniform angles
theta = np.random.uniform(0, 2*np.pi, (N,1))
eps_noise = 0.2 * np.random.normal(size=[N,1])
circle = np.hstack([np.cos(theta), np.sin(theta)])
# Stretch and rotate circle to an ellipse with random linear tranformation
B = np.random.randint(-3, 3, (DIM, DIM))
noisy_ellipse = circle.dot(B) + eps_noise
# Extract x coords and y coords of the ellipse as column vectors
X = noisy_ellipse[:,0:1]
Y = noisy_ellipse[:,1:]
# Formulate and solve the least squares problem ||Ax - b ||^2
A = np.hstack([X**2, X * Y, Y**2, X, Y])
b = np.ones_like(X)
x = np.linalg.lstsq(A, b)[0].squeeze()
# Print the equation of the ellipse in standard form
print('The ellipse is given by {0:.3}x^2 + {1:.3}xy+{2:.3}y^2+{3:.3}x+{4:.3}y = 1'.format(x[0], x[1],x[2],x[3],x[4]))
# Plot the noisy data
plt.scatter(X, Y, label='Data Points')
# Plot the original ellipse from which the data was generated
phi = np.linspace(0, 2*np.pi, 1000).reshape((1000,1))
c = np.hstack([np.cos(phi), np.sin(phi)])
ground_truth_ellipse = c.dot(B)
plt.plot(ground_truth_ellipse[:,0], ground_truth_ellipse[:,1], 'k--', label='Generating Ellipse')
# Plot the least squares ellipse
x_coord = np.linspace(-5,5,300)
y_coord = np.linspace(-5,5,300)
X_coord, Y_coord = np.meshgrid(x_coord, y_coord)
Z_coord = x[0] * X_coord ** 2 + x[1] * X_coord * Y_coord + x[2] * Y_coord**2 + x[3] * X_coord + x[4] * Y_coord
plt.contour(X_coord, Y_coord, Z_coord, levels=[1], colors=('r'), linewidths=2)
plt.legend()
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
以(0,0)为焦点的椭圆,极坐标下的椭圆方程可写成:
给定=半长轴,=偏心,另一个焦点在角度,
现在,我们可以使用数据点(,)通过非线性最小二乘法(例如:Levenberg–Marquardt算法)来拟合上面的方程。
即找出以下等式的最小值:
此问题类似于如何将二维椭圆(在 x-y 平面中)拟合到给定点的问题? (见下面的link)
(
现在,通过Casey提供的代码,我们知道了如何使用最小二乘法来拟合具有给定点的二维椭圆。 (下面也提供了代码。)根据这段代码,如果我不仅要拟合给定的点,还要拟合给定的焦点(0,0),我该怎么做?或者有什么更好的方法吗?
我在考虑是否可以根据椭圆方程 Ax^2 + Bxy + Cy^2 + Dx + Ey=1 导出焦点(作为 x 和 y 的函数),其中 A, B , C, D, E 为系数,作为对最小二乘法的约束。但是很遗憾,我也不知道如何获得焦点。
代码
import numpy as np
import matplotlib.pyplot as plt
alpha = 5
beta = 3
N = 500
DIM = 2
np.random.seed(2)
# Generate random points on the unit circle by sampling uniform angles
theta = np.random.uniform(0, 2*np.pi, (N,1))
eps_noise = 0.2 * np.random.normal(size=[N,1])
circle = np.hstack([np.cos(theta), np.sin(theta)])
# Stretch and rotate circle to an ellipse with random linear tranformation
B = np.random.randint(-3, 3, (DIM, DIM))
noisy_ellipse = circle.dot(B) + eps_noise
# Extract x coords and y coords of the ellipse as column vectors
X = noisy_ellipse[:,0:1]
Y = noisy_ellipse[:,1:]
# Formulate and solve the least squares problem ||Ax - b ||^2
A = np.hstack([X**2, X * Y, Y**2, X, Y])
b = np.ones_like(X)
x = np.linalg.lstsq(A, b)[0].squeeze()
# Print the equation of the ellipse in standard form
print('The ellipse is given by {0:.3}x^2 + {1:.3}xy+{2:.3}y^2+{3:.3}x+{4:.3}y = 1'.format(x[0], x[1],x[2],x[3],x[4]))
# Plot the noisy data
plt.scatter(X, Y, label='Data Points')
# Plot the original ellipse from which the data was generated
phi = np.linspace(0, 2*np.pi, 1000).reshape((1000,1))
c = np.hstack([np.cos(phi), np.sin(phi)])
ground_truth_ellipse = c.dot(B)
plt.plot(ground_truth_ellipse[:,0], ground_truth_ellipse[:,1], 'k--', label='Generating Ellipse')
# Plot the least squares ellipse
x_coord = np.linspace(-5,5,300)
y_coord = np.linspace(-5,5,300)
X_coord, Y_coord = np.meshgrid(x_coord, y_coord)
Z_coord = x[0] * X_coord ** 2 + x[1] * X_coord * Y_coord + x[2] * Y_coord**2 + x[3] * X_coord + x[4] * Y_coord
plt.contour(X_coord, Y_coord, Z_coord, levels=[1], colors=('r'), linewidths=2)
plt.legend()
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
以(0,0)为焦点的椭圆,极坐标下的椭圆方程可写成:
给定=半长轴,=偏心,另一个焦点在角度,
现在,我们可以使用数据点(,)通过非线性最小二乘法(例如:Levenberg–Marquardt算法)来拟合上面的方程。
即找出以下等式的最小值: