嗯,近似一个带有多边形的圆圈和毕达哥拉斯的故事可能是众所周知的。 但另一种方式呢?
我有一些多边形,实际上应该是圆圈。但是,由于测量误差,它们不是。所以,我正在寻找的是最能“近似”给定多边形的圆。
在下图中,我们可以看到两个不同的例子。
我的第一个Ansatz是找到点到中心的最大距离以及最小值。我们正在寻找的圈子可能介于两者之间。
这个问题有没有算法?
答案 0 :(得分:5)
我会使用scipy
来最好地“适应”我的观点。您可以通过简单的质心计算得到中心和半径的起点。如果点均匀分布在圆上,则这很有效。如果它们不是,如下例所示,它仍然比没有好!
拟合功能很简单,因为圆圈很简单。您只需找到从拟合圆到点的径向距离,因为切线(径向)曲面始终是最佳拟合。
import numpy as np
from scipy.spatial.distance import cdist
from scipy.optimize import fmin
import scipy
# Draw a fuzzy circle to test
N = 15
THETA = np.random.random(15)*2*np.pi
R = 1.5 + (.1*np.random.random(15) - .05)
X = R*np.cos(THETA) + 5
Y = R*np.sin(THETA) - 2
# Choose the inital center of fit circle as the CM
xm = X.mean()
ym = Y.mean()
# Choose the inital radius as the average distance to the CM
cm = np.array([xm,ym]).reshape(1,2)
rm = cdist(cm, np.array([X,Y]).T).mean()
# Best fit a circle to these points
def err((w,v,r)):
pts = [np.linalg.norm([x-w,y-v])-r for x,y in zip(X,Y)]
return (np.array(pts)**2).sum()
xf,yf,rf = scipy.optimize.fmin(err,[xm,ym,rm])
# Viszualize the results
import pylab as plt
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
# Show the inital guess circle
circ = plt.Circle((xm, ym), radius=rm, color='y',lw=2,alpha=.5)
ax.add_patch(circ)
# Show the fit circle
circ = plt.Circle((xf, yf), radius=rf, color='b',lw=2,alpha=.5)
ax.add_patch(circ)
plt.axis('equal')
plt.scatter(X,Y)
plt.show()
答案 1 :(得分:1)
也许一个简单的算法首先要计算点的质心(假设它们通常大致有规律地间隔)。这是圆心。一旦你有了,你可以计算点的平均半径,给出圆的半径。
更复杂的答案可能是进行简单的最小化,最小化点到圆的边缘的距离之和(或距离的平方)。
答案 2 :(得分:0)
有两种不同的O(n)算法可用于确定您绘制的最小圆圈,其中包含维基百科页面smallest-circle problem上的一系列点。从这里绘制第二个圆圈应该相当容易,只需确定您之前找到的圆的中心,并找到最接近该点的点。第二个圆的半径是。
这可能不是你想要的,但这就是我的开始。
答案 3 :(得分:0)
该问题可能与Smallest-circle problem相同。
但是,由于您的测量误差可能包括异常值,因此RANSAC是一个不错的选择。有关该方法的概述(以及其他基本技术),请参阅http://cs.gmu.edu/~kosecka/cs482/lect-fitting.pdf,在http://www.asl.ethz.ch/education/master/info-process-rob/Hough-Ransac.pdf中有更多专门用于圆拟合的信息。
答案 4 :(得分:-1)
很容易找到一些近似值:
def find_circle_deterministically(x,y):
center = x.mean(), y.mean()
radius = np.sqrt((x-center[0])**2 + (y-center[1])**2).mean()
return center, radius
解释:将圆心置于平均值x和平均值y。然后,对于每个点,确定到中心的距离并取所有点的平均值。那是你的半径。
这个完整的脚本:
import numpy as np
import matplotlib.pyplot as plt
n_points = 10
radius = 4
noise_std = 0.3
angles = np.linspace(0,2*np.pi,n_points,False)
x = np.cos(angles) * radius
y = np.sin(angles) * radius
x += np.random.normal(0,noise_std,x.shape)
y += np.random.normal(0,noise_std,y.shape)
plt.axes(aspect="equal")
plt.plot(x,y,"bx")
def find_circle_deterministically(x,y):
center = x.mean(), y.mean()
radius = np.sqrt((x-center[0])**2 + (y-center[1])**2).mean()
return center, radius
center, radius2 = find_circle_deterministically(x,y)
angles2 = np.linspace(0,2*np.pi,100,True)
x2 = center[0] + np.cos(angles2) * radius2
y2 = center[1] + np.sin(angles2) * radius2
plt.plot(x2,y2,"r-")
plt.show()
产生这个情节:
这样可以正常工作,因为您的多边形有测量误差。如果您的点在角度[0,2pi[
上的分布不均匀,则表现不佳。
更一般地说,您可以使用优化。