如何使用python中的最小二乘法找到圆的中心? [英] How to find the center of circle using the least square fit in python?

查看:635
本文介绍了如何使用python中的最小二乘法找到圆的中心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一些数据点以找到一个圆圈的中心。以下所有点都是圆圈周围的嘈杂数据点:$ b​​
$ b

  data = [(2.2176383052987667,4.218574252410221),
(3.3041214516913033,5.223500807396272),
(4.280815855023374,6.4141487709813785),
(4.946375258539319,7.606952538212697),
(5.382428804463699,9.045717060494576),
(5.752578028217334,10.613667377465823),
$ b(5.547729017414035,11.92662513852466),
(5.260208374620305,13.57722448066025),
(4.642126672822957,14.88238955729078),
(3.820310290976751,16.10605425390148),
(2.8099420132544024,17.225880123445773),
b $ b(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.3444419909199452),
(-3.6530364510489073,2.992272643733981),
(-2.1522365767310796,3.020780664301393),
(-0.6855406924835704,3.0767643753777447),
(0.7848958776292426,3.6196842530995332),
(2.0614188482646947,4.2795711960546),
(3.2705467984691508,5.295836809444288),
(4.359297538484424,6.378324784240816),
(4.981264502955681,7.823851404553242)]

我试图使用一些像Scipy http://wiki.scipy.org/Cookbook/Least_Squares_Circle ,但我无法使用可用的函数。



例如:

 #==方法2 == scipy import中的
优化

method_2 =leastsq

def calc_R(xc,yc):
计算每个2D点与中心的距离(xc,yc)
return sqrt((x-xc)** 2 +(y-yc)** 2)

def f_2(c):
计算之间的代数距离数据点和以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?有关如何将此函数插入​​到我的数据示例的任何想法?

解决方案

您的数据点看起来相当干净,所以很多圈子拟合算法都可以工作。



我建议你从Coope方法开始,它通过神奇线性化问题来工作:
$









$ b 2XcX + 2YcY +R²-Xc²-Yc²=X²+Y²,那么

AX + BY + C = X 2 + Y 2 ,由线性最小二乘法解决。

I'm trying to fit some data points in order to find the center of a circle. All of the following points are noisy data points around the circumference of the circle:

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)]

I was trying to use some library like Scipy http://wiki.scipy.org/Cookbook/Least_Squares_Circle but I'm having trouble using the available functions.

There is for example:

#  == 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)

But this seems to be using a single xy? Any ideas on how to plug this function to my data example?

解决方案

Your data points seem fairly clean and I see no outliers, so many circle fitting algorithms will work.

I recommend you to start with the Coope method, which works by magically linearizing the problem:

(X-Xc)^2+(Y-Yc)^2=R² is rewritten as

2XcX+2YcY+R²-Xc²-Yc²=X²+Y², then

AX+BY+C=X²+Y², solved by linear least squares.

这篇关于如何使用python中的最小二乘法找到圆的中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆