# Least-squares fitting of circles

## Hypotheses

This page explains how to fit a 2D circle to a cloud of point by minimizing least squares errors.

The point cloud is given by $$n$$ points with coordinates $${x_i,y_i}$$. The aim is to estimate $$x_c$$ , $$y_c$$ and $$r$$, the parameters of the circle that fit the best the points :

• $$x_c$$ is the x-coordinate of the center of the circle
• $$y_c$$ is the y-coordinate of the center of the circle
• $$r$$ is the radius of the circle

The equation of the ideal circle is given by:

$$(x_i-x_c)^2 + (y_i-y_c)^2 = r^2$$

## The matrix system

The previous equation is rewritten as:

$$x_i^2 + x_c^2 - 2x_ix_c + y_i^2 + y_c^2 - 2y_iy_c = r^2$$

, then:

$$2x_cx_i + 2y_cy_i + r^2 - x_c^2 - y_c^2 = x_i^2 + y_i^2$$

and:

$$ax_i + by_i + c = x_i^2 + y_i^2$$

with:

• $$a = 2x_c$$
• $$b = 2y_c$$
• $$c = r^2 - x_c^2 - y_c^2$$

The whole system (for all the points) can be rewritten as:

$$\begin{array}{rcl} ax_1 + by_1 + c & = & x_1^2 + y_1^2 \\ ax_2 + by_2 + c & = & x_2^2 + y_2^2 \\ & ... & \\ ax_n + by_n + c & = & x_n^2 + y_n^2 \\ \end{array}$$

The matrix form of the system is given by:

$$\left[ \begin{matrix} x_1 & y_1 & 1 \\ x_2 & y_2 & 1 \\ ... & ... & ... \\ x_n & y_n & 1 \\ \end{matrix} \right]. \left[ \begin{matrix} a \\ b \\ c \end{matrix} \right] = \left[ \begin{matrix} x_1^2 + y_1^2 \\ x_2^2 + y_2^2 \\ ... \\ x_n^2 + y_n^2 \\ \end{matrix} \right]$$

Let's define $$A$$, $$B$$ and $$X$$:

$$\begin{matrix} A=\left[ \begin{matrix} x_1 & y_1 & 1 \\ x_2 & y_2 & 1 \\ ... & ... & ... \\ x_n & y_n & 1 \\ \end{matrix} \right] & B=\left[ \begin{matrix} x_1^2 + y_1^2 \\ x_2^2 + y_2^2 \\... \\ x_n^2 + y_n^2 \\ \end{matrix} \right] & X=\left[ \begin{matrix} a \\ b \\ c \end{matrix} \right] \end{matrix}$$

The system is now given by:

$$A.X=B$$

## Solving the system

The optimal solution is given by:

$$\hat{x}=A^{+}.B = A^{T}(A.A^{T})^{-1}.B$$

Where $$A^{+}$$ is the pseudoinverse of $$A$$. $$A^{+}$$ can be computed thanks to the following formula :

$$A^{+}=A^{T}(A.A^{T})^{-1}$$

As there has been a change of variables, it only remains to calculate $$x_c$$ , $$y_c$$ and $$r$$:

$$x_c = \dfrac{a}{2}$$ $$y_c = \dfrac{b}{2}$$ $$r = \dfrac{ \sqrt{4c + a^2 + b^2 }}{2}$$

And you get the parameters of the circle:

## Source code

The following Matlab source code was used for drawing the above figure:

close all;
clear all;
clc;

%% Parameters of the system
%% This is the parameters we need to estimate
% Center
C = [3 , 2];
R = 1;

% Number of points
N = 1000;
% Noise
k = 0.050;

%% Create noisy circle
alpha = 2*pi*rand(1,N);
noise = k*2*randn(1,N)-1;
Points = C + [ R*noise.*cos(alpha) ; R*noise.*sin(alpha) ]';

%% Draw the noisy circle
plot (Points(:,1) , Points(:,2), 'b.');
axis square equal;
grid on;

%% Prepare matrices
A = [Points(:,1) Points(:,2) ones(N,1)];
B = [Points(:,1).*Points(:,1) + Points(:,2).*Points(:,2)];

%% Least square approximation
X=pinv(A)*B;

%% Calculate circle parameter
xc = X(1)/2
yc = X(2)/2
r = sqrt(4*X(3) + X(1)*X(1) + X(2)*X(2) )/2

%% Draw the fitted circle
Circle = [ xc + r*cos([0:pi/50:2*pi]) ; yc + r*sin([0:pi/50:2*pi]) ]';
hold on;
plot (Circle(:,1), Circle(:,2), 'r', 'LineWidth', 2);
legend ('Points', 'Least-square circle');