Search code examples
imagematlabcurve-fittingleast-squaressvd

Fitting of a sphere using SVD/LMS


I would like to fit a MR binary data of 281*398*104 matrix which is not a perfect sphere, and find out the center and radius of sphere and error also. I know LMS or SVD is a good choice to fit for sphere.

I have tried sphereFit from matlab file exchange but got an error,

 >> sphereFit(data)  
    Warning: Matrix is singular to working precision. 
    > In sphereFit at 33 
    ans =
         NaN   NaN   NaN  

Would you let me know where is the problem, or any others solution?


Solution

  • If you want to use sphere fitting algorithm you should first extract the boundary points of the object you assume to be a sphere. The result should be represented by a N-by-3 array containing coordinates of the points. Then you can apply sphereFit function.

    In order to obtain boundary point of a binary object, there are several methods. One method is to apply morphological erosion (you need the "imerode" function from the image processing toolbox) with small structuring element, then compute set difference between the two images, and finally use the "find" function to transform binary image into a coordinate array.

    the idea is as follow:

    dataIn = imerode(data, ones([3 3 3]));
    bnd = data & ~data2;
    inds = find(bnd);
    [y, x, z] = ind2sub(size(data), inds); % be careful about x y order
    points = [x y z];
    sphere = sphereFitting(points);
    

    By the way, the link you gave refers to circle fitting, I suppose you wanted to point to a sphere fitting submission?

    regards,