-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[STAPLE] Fitting metrics #69
Comments
@renaultJB I did not check this on GIBOC-core before, but the SphereFit is actually a linear least-square sphere fitting. |
Yes indeed, I have used it while developing because it was faster and forgot to change it afterwards, but there is an implementation in the non linear least square fit library provided by the functions associated with the document you mention, the code is already in the repo non linear least square sphere fit function [Center,Radius,ErrorDist] = sphereFit(X)
% This fits a sphere in two steps
%
% Step 1 : Fit a linear least square sphere
%---------------------------------------------------------------------------
% this fits a sphere to a collection of data using a closed form for the
% solution (opposed to using an array the size of the data set).
% Minimizes Sum((x-xc)^2+(y-yc)^2+(z-zc)^2-r^2)^2
% x,y,z are the data, xc,yc,zc are the sphere's center, and r is the radius
% Assumes that points are not in a singular configuration, real numbers, ...
% if you have coplanar data, use a circle fit with svd for determining the
% plane, recommended Circle Fit (Pratt method), by Nikolai Chernov
% http://www.mathworks.com/matlabcentral/fileexchange/22643
% Input:
% X: n x 3 matrix of cartesian data
% Outputs:
% Center: Center of sphere
% Radius: Radius of sphere
% Author:
% Alan Jennings, University of Dayton
% Modified to add distance to sphere -> ErrorDist
% ---------------------------------------------------------------------------
% Step 2 : Fit a non linear least square sphere
%---------------------------------------------------------------------------
%
% LSSPHERE.M Least-squares sphere using Gauss-Newton.
%
% Version 1.0
% Last amended I M Smith 27 May 2002.
% Created I M Smith 08 Mar 2002
%
% ---------------------------------------------------------------------
% See A B Forbes: Least-squares best-fit geometric elements,
% NPL Report DITC 140/89.
%
% ---------------------------------------------------------------------
% Author A B Forbes
% National Physical Laboratory
% England
%
% Created November 1988
% Version 2.0 93/07/09
% Crown Copyright
% ---------------------------------------------------------------------------
%% STEP 1
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))))];
Center0=(A\B).';
Radius0 = sqrt(mean(sum([X(:,1)-Center0(1), X(:,2)-Center0(2), X(:,3)-Center0(3)].^2,2)));
% (P-Centre)^2 - Radius^2
ErrorDist0 = sum([X(:,1)-Center0(1), X(:,2)-Center0(2), X(:,3)-Center0(3)].^2,2) - Radius0^2;
%% STEP 2
% Fit a full non linear least square sphere
[Center, Radius, ErrorDist, ~, conv, ~, ~, ~, ~, ~] = lssphere(X, Center0, Radius0, 1e-5, 1e-5)
%% If STEP 2 does not converge use STEP 1 results
if conv == 0 :
warning('*** Gauss-Newton algorithm has not converged for non linear least square sphere fit***');
warning('*** Using linear least square method for sphere fit instead***');
Center = Center0;
Raduis = Radius0;
ErrorDist = ErrorDist0; |
@renaultJB good idea! I did notice that LSGE included a sphere fit but did not think about merging the two. I will give it a try. |
@renaultJB still have to do this - I will deal with it this week |
The text was updated successfully, but these errors were encountered: