首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在python中用最小二乘拟合求圆心?

如何在python中用最小二乘拟合求圆心?
EN

Stack Overflow用户
提问于 2014-10-27 00:02:57
回答 4查看 14.9K关注 0票数 7

我正在尝试拟合一些数据点,以便找到圆的中心。以下所有点都是圆周周围的噪声数据点:

data = [(2.2176383052987667, 4.218574252410221),
(3.3041214516913033, 5.223500807396272),
(4.280815855023374, 6.461487709813785),
(4.946375258539319, 7.606952538212697),
(5.382428804463699, 9.045717060494576),
(5.752578028217334, 10.613667377465823),
(5.547729017414035, 11.92662513852466),
(5.260208374620305, 13.57722448066025),
(4.642126672822957, 14.88238955729078),
(3.820310290976751, 16.10605425390148),
(2.8099420132544024, 17.225880123445773),
(1.5731539516426183, 18.17052077121059),
(0.31752822350872545, 18.75261434891438),
(-1.2408437559671106, 19.119355580780265),
(-2.680901948575409, 19.15018791257732),
(-4.190406775175328, 19.001321726517297),
(-5.533990404926917, 18.64857428377178),
(-6.903383826792998, 17.730112542165955),
(-8.082883753215347, 16.928080323602334),
(-9.138397388219254, 15.84088004983959),
(-9.92610373064812, 14.380575762984085),
(-10.358670204629814, 13.018017342781242),
(-10.600053524240247, 11.387283417089911),
(-10.463673966507077, 10.107554951600699),
(-10.179820255235496, 8.429558128401448),
(-9.572153386953028, 7.1976672709797676),
(-8.641475289758178, 5.8312286526738175),
(-7.665976739804268, 4.782663065707469),
(-6.493033077746997, 3.8549965442534684),
(-5.092340806635571, 3.384419909199452),
(-3.6530364510489073, 2.992272643733981),
(-2.1522365767310796, 3.020780664301393),
(-0.6855406924835704, 3.0767643753777447),
(0.7848958776292426, 3.6196842530995332),
(2.0614188482646947, 4.32795711960546),
(3.2705467984691508, 5.295836809444288),
(4.359297538484424, 6.378324784240816),
(4.981264502955681, 7.823851404553242)]

我试图使用像Scipy这样的库,但是我在使用可用的函数时遇到了问题。

例如:

#  == METHOD 2 ==
from scipy      import optimize

method_2 = "leastsq"

def calc_R(xc, yc):
    """ calculate the distance of each 2D points from the center (xc, yc) """
    return sqrt((x-xc)**2 + (y-yc)**2)

def f_2(c):
    """ calculate the algebraic distance between the data points and the mean circle centered at c=(xc, yc) """
    Ri = calc_R(*c)
    return Ri - Ri.mean()

center_estimate = x_m, y_m
center_2, ier = optimize.leastsq(f_2, center_estimate)

xc_2, yc_2 = center_2
Ri_2       = calc_R(*center_2)
R_2        = Ri_2.mean()
residu_2   = sum((Ri_2 - R_2)**2)

但这似乎使用了单个xy?关于如何将此函数插入到我的数据示例中,您有什么想法吗?

EN

回答 4

Stack Overflow用户

发布于 2014-10-27 02:36:03

我没有任何拟合圆的经验,但我使用过更一般的椭圆拟合情况。以正确的方式处理有噪声的数据不是微不足道的。对于这个问题,Halir和Flusser在Numerically stable direct least squares fitting of ellipses中描述的算法工作得很好。本文包括Matlab代码,这些代码应该可以直接转换为Numpy。也许你可以用这个算法来拟合一个椭圆,然后取两个轴的平均值作为半径。论文中的一些参考文献还提到了拟合圆圈,您可能想要查找这些参考资料。

票数 3
EN

Stack Overflow用户

发布于 2017-03-22 03:26:50

作为Bas Swinckels文章的后续,我想我应该发布实现Halir和Flusser方法拟合椭圆的代码

https://github.com/bdhammel/least-squares-ellipse-fitting

使用上面的代码,您可以使用以下方法找到中心。

from ellipses import LSqEllipse
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse

lsqe = LSqEllipse()
lsqe.fit(data)
center, width, height, phi = lsqe.parameters()

plt.close('all')
fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(111)
ax.axis('equal')
ax.plot(data[0], data[1], 'ro', label='test data', zorder=1)

ellipse = Ellipse(xy=center, width=2*width, height=2*height, angle=np.rad2deg(phi),
               edgecolor='b', fc='None', lw=2, label='Fit', zorder = 2)
ax.add_patch(ellipse)

plt.legend()
plt.show()

票数 3
EN

Stack Overflow用户

发布于 2019-03-20 01:49:28

我知道这是一个古老的问题,但在2019年,python中有一个名为circle-fit的圆拟合库。

pip install circle-fit

您可以使用两种算法中的一种来求解,即least_squares_circlehyper_fit

import circle_fit as cf
xc,yc,r,_ = cf.least_squares_circle((data)

然后,您将获得xc, yc作为解圆中心的坐标对。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26574945

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档