【发布时间】:2021-12-13 22:06:11
【问题描述】:
我一直在使用 lsq-ellipse 包,通过以下代码获取椭圆的坐标:
from ellipse import LsqEllipse
from matplotlib.patches import Ellipse
coords_D0 = np.array(coords_D0)
reg = LsqEllipse().fit(coords_D0)
center_D0, width_D0, height_D0, phi_D0 = reg.as_parameters()
print(f'center: {center_D0[0]:.3f}, {center_D0[1]:.3f}')
print(f'width: {width_D0:.3f}')
print(f'height: {height_D0:.3f}')
print(f'phi: {phi_D0:.3f}')
但是,我的 coords_D0 变量由三个坐标组成,导致以下错误:
ValueError: Received too few samplesGot 3 features, 5 or more required.
但是,在查看了一些包和在线后,我发现 sympy 也可以做椭圆,我知道你可以从 centre, vradius and hradius 中提取em>sympy。但是,我想知道如何从 sympy 中获取 width、height 和 phi,它是否与 Ellipse 中使用的 lsq-ellipse 包相同matplotlib?我使用 matplotlib 中 lsq-ellipse 包中的值来形成椭圆部分,它可以在以下代码行中找到:
代码:
ellipse_D0 = Ellipse(xy=center_D0, width=2*width_D0, height=2*height_D0, angle=np.rad2deg(phi_D0),edgecolor='b', fc='None', lw=2, label='Fit', zorder=2)
我的坐标如下:
coords_D0 =
-1.98976 -1.91574
-0.0157721 2.5438
2.00553 -0.628061
# another points
coords_D1 =
-0.195518 0.0273673
-0.655686 -1.45848
-0.447061 -0.168108
# another points
coords_D2 =
-2.28529 0.91896
-2.43207 0.446211
-2.23044 0.200087
附加问题:
有没有办法将椭圆拟合到这些坐标(通常是 3 个坐标或更多)?
【问题讨论】:
-
你使用
fit。两个交叉椭圆可以有 4 个共同点,因此没有唯一的解决方案给出小于 5。您阅读过文档吗? -
@mikuszefski 你在暗示哪个包?你的意思是适合 Lsq 包我收到错误,因为需要更多的功能超过 5 个。
-
嗨,如果我做对了,
LsqEllipse().fit()方法会使用(x, y)的列表来拟合椭圆。要给出正确的唯一结果,您需要 5 个或更多值。所以很自然地你会得到一个错误。 -
@mikuszefski 我明白了。我一直在网上查找这些链接 (stackoverflow.com/questions/39693869/…) (stackoverflow.com/questions/47873759/…) (stackoverflow.com/questions/52818206/…) (stackoverflow.com/questions/39693869/…),但是,我无法使用我的坐标获得正确的中心点、宽度、高度和 phi。
-
所以我们很清楚你不能拟合提供 3 个点的椭圆。那我们说的是哪个坐标?我想我放了一个易于遵循的代码here
标签: python matplotlib sympy ellipse data-fitting