Skip to content Skip to sidebar Skip to footer

How To Find The Center Of Circle Using The Least Square Fit In Python?

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.217

Solution 1:

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)² + (Y-Yc)² = R² is rewritten as

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

A X + B Y + C = X² + Y², solved by linear least squares.

Solution 2:

As a follow up to Bas Swinckels post, I figured I'd post my code implementing the Halir and Flusser method of fitting an ellipse

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

Using the above code you can find the center with the following method.

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

enter image description here

Solution 3:

I do not have any experience fitting circles, but I have worked with the more general case of fitting ellipses. Doing this in a correct way with noisy data is not trivial. For this problem, the algorithm described in Numerically stable direct least squares fitting of ellipses by Halir and Flusser works pretty well. The paper includes Matlab code, which should be straightforward to translate to Numpy. Maybe you could use this algorithm to fit an ellipse and then take the average of the two axis as the radius or so. Some of the references in the paper also mention fitting circles, you might want to look those up.

Solution 4:

I know this is an old question, but in 2019 there's a circle fitting library in python called circle-fit.

pip install circle-fit

you can use one of two algorithms to solve, least_squares_circle or hyper_fit.

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

then you get xc, yc as the coordinate pair for the solution circle center.

Solution 5:

Ian Coope's algorithm (paper here) linearizes the problem using a variable substitution. It's the most generally robust and fastest algorithm I've come across for circle fitting so far. I've implemented the algorithm in python in scikit-guess. Using the function skg.nsphere_fit for a one-line solution:

>>>r, c = skg.nsphere_fit(data)
>>>r
8.138962707494084
>>>c
array([-2.45595128, 11.04352796])

Here is a plot of the result:

t = np.linspace(0, 2 * np.pi, 1000, endpoint=True)
plt.scatter(*np.array(data).T, color='r')
plt.plot(r * np.cos(t) + c[0], r * np.sin(t) + c[1])
plt.axis('equal')
plt.show()

enter image description here

Post a Comment for "How To Find The Center Of Circle Using The Least Square Fit In Python?"