Circle least squares fit for 3D data

Hi everyone,
I have 6000 x coordinates, y coordinates and z cooridinates that form a circle that does not perfectly occupy one plane. I am looking to fit the data with a circle but I can only find functions online that do it for x and y coordinates and not including z. I would appreciate any help in creating some code for this as I am not sure where to start (I am a beginner in MATLAB!)
Thank you in advance
Ciara

8 Comments

Start to find the plane that approximate your 3D points then project point on this plane then use your prefered 2D circle fitting method.
If you have z also , it should be a sphere right?
@Bruno Luong, thank you for replying firstly! Please could you help me in terms of finding a plane, I am not sure how to go about this (sorry I am only beginners level :) ). Any help would be much appreciated!
@ KSSV thank you for the response! I was thinking this also but this is how my data appearscircle fit.PNG
I need to find the best fit for the blue data and the purple data - they appear more like a slanted circle / plane, so I worry about how accurate using a sphere will be.
My main interest is determining the circle centre as I need it to determine some offset in my data. Any advice/help would be greatly appreciated!
Torsten
Torsten on 7 Aug 2019
Edited: Torsten on 7 Aug 2019
@Bruno Luong, thank you for replying firstly! Please could you help me in terms of finding a plane, I am not sure how to go about this (sorry I am only beginners level :) ). Any help would be much appreciated!
https://stackoverflow.com/questions/48200261/fit-plane-to-n-dimensional-points-in-matlab
Bruno Luong
Bruno Luong on 7 Aug 2019
Edited: Bruno Luong on 7 Aug 2019
Can you attach your data. From the picture it is not clear any circle shape would emerge from this perspertive.
@Bruno Luong , please find the data attached in the order of x, y ,x . Thanks again
@Torsten, thank you for the links I will give them a read!

Sign in to comment.

 Accepted Answer

Bruno Luong
Bruno Luong on 7 Aug 2019
Edited: Bruno Luong on 7 Aug 2019
circle.png
X=csvread('data.csv');
XC = mean(X,1);
Y=X-XC;
[~,~,V]=svd(Y,0);
Q = V(:,[1 2]); % basis of the plane
Y=Y*Q;
xc=Y(:,1);
yc=Y(:,2);
M=[xc.^2+yc.^2,-2*xc,-2*yc];
% Fit ellipse through (xc,yc)
P = M\ones(size(xc));
a=P(1);
P = P/a;
r=sqrt(P(2)^2+P(3)^2+1/a); % radius
xyzc = XC' + Q*P(2:3); % center
theta = linspace(0,2*pi);
c = xyzc + r*Q*[cos(theta); sin(theta)]; % fit circle
close all
plot3(X(:,1),X(:,2),X(:,3),'.');
hold on
plot3(c(1,:),c(2,:),c(3,:),'r','LineWidth', 2);
axis equal

4 Comments

Thank you this is just what I was looking for !! If you don't mind, could you explain at little the steps you have done just so I can understand more? Thank you again !
The first block using the well-known technique to find the plane that fits your 3D data using the SVD. The smallest eigen vector V(:,3) correcponds to the direction orthogonal to the plane and the 2 other directions V(:,[1 2]) are the (orthonormal) basis of the plane;I call it Q.
Y is the projection of X on the plane after moving the origin to the barycenter of the points (XC).
Next I look at the equation of the circle on the plane by using least square fit to the eqt
P(1)*(x.^2 + y.^2) -2*P(2)*x - -2*P(2)*y = 1
After transformation you 'll find the 2D equation of the circle as
(x-xc).^2 + (y-yc).^2 = r^2
with xc = P(2)/P(1) and yc = P(3)/P(1) and r = sqrt((P(2)^2+P(3)^2+1)/P(1));
The last step just bring back the 2D coordinates in the plance to 3D coodinates
Amazing! Thank you very much it is greatly appreciated and you have saved me a lot of time!
This script is just brilliant!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!