I know the coordinates of several scattered points in space, how can I fit them to a sphere?

1 次查看(过去 30 天)
I already know the coordinates (x,y,z) of several scattered points on a sphere in space, and my goal is to fit a sphere and get the radius. I think I just need to bring their coordinates into the code I found. I would like to ask how to bring in the coordinates of these points?
function [r,a,b,c] = sphereFit(data)
xx = data(:,1);
yy = data(:,2);
zz = data(:,3);
AA = [-2*xx, -2*yy , -2*zz , ones(size(xx))];
BB = [ -(xx.^2+yy.^2+zz.^2)];
YY = mldivide(AA,BB); %Trying to solve AA*YY = BB
a = YY(1);
b = YY(2);
c = YY(3);
D = YY(4); % D^2 = a^2 + b^2 + c^2 -r^2(where a,b,c are centers)
r = sqrt((a^2+b^2+c^2)-D);
The second code:
function [Center,Radius] = sphereFit(X)
A=[mean(X(:,1).*(X(:,1)-mean(X(:,1)))), ...
2*mean(X(:,1).*(X(:,2)-mean(X(:,2)))), ...
2*mean(X(:,1).*(X(:,3)-mean(X(:,3)))); ...
0, ...
mean(X(:,2).*(X(:,2)-mean(X(:,2)))), ...
2*mean(X(:,2).*(X(:,3)-mean(X(:,3)))); ...
0, ...
0, ...
mean(X(:,3).*(X(:,3)-mean(X(:,3))))];
A=A+A.';
B=[mean((X(:,1).^2+X(:,2).^2+X(:,3).^2).*(X(:,1)-mean(X(:,1))));...
mean((X(:,1).^2+X(:,2).^2+X(:,3).^2).*(X(:,2)-mean(X(:,2))));...
mean((X(:,1).^2+X(:,2).^2+X(:,3).^2).*(X(:,3)-mean(X(:,3))))];
Center=(A\B).';
Radius=sqrt(mean(sum([X(:,1)-Center(1),X(:,2)-Center(2),X(:,3)-Center(3)].^2,2)));
Which code should I choose and apply correctly?
  2 个评论
John D'Errico
John D'Errico 2023-2-4
编辑:John D'Errico 2023-2-4
So, instead of using the code I already gave you that fits the sphere in a well posed way in your last question, now you ask which of these poorly written pieces of code you should use. Sigh. If someone does tell you which poorly written code to use, will you then turn the question into an image processing question again?
Sterne_17
Sterne_17 2023-2-4
Sir. Thank you! The code you suggested to me is very, very well written! I just wasn't sure how to go about using it, so I replied to you to ask you about bringing a series of scattered coordinates into the code you wrote. I'm a starter and don't have any experience writing code, that's why I asked you and it has nothing to do with the type of question, I'm also just using the forum to ask questions. I meant no disrespect and I just adopted your answer. I just wanted to ask how to bring these scattered coordinates into the code, because I don't understand it and I can't tell if the code is good or bad. In this question I attached the code I don't understand either, I just want to ask how to bring the scatter coordinates into your code.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2023-2-4
编辑:Image Analyst 2023-2-4
It looks like the input data for both functions is an N-by-3 matrix.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Scatter Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by