[SciPy-user] Fitting Sphere to Data Points

The Helmbolds helmrp at yahoo.com
Thu Jan 25 16:41:03 EST 2007


Try:
  
err_sq[i]=(x[i]-a)**2 + (y[i]-b)**2 + (z[i]-c)**2 - r**2
  err_sum_of_squares = sum(err_sq)
   
  and minimize err_sum_of_squares by varying the parameters (a,b,c,r).
   
  
scipy-user-request at scipy.org wrote:
  Send SciPy-user mailing list submissions to
scipy-user at scipy.org

To subscribe or unsubscribe via the World Wide Web, visit
http://projects.scipy.org/mailman/listinfo/scipy-user
or, via email, send a message with subject or body 'help' to
scipy-user-request at scipy.org

You can reach the person managing the list at
scipy-user-owner at scipy.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of SciPy-user digest..."


Today's Topics:

1. Re: Fitting sphere to 3d data points (James Vincent)
2. Re: Fitting sphere to 3d data points (ckkart at hoc.net)
3. Re: Fitting sphere to 3d data points (David Douard)
4. Re: Fitting sphere to 3d data points (James Vincent)


----------------------------------------------------------------------

Message: 1
Date: Thu, 25 Jan 2007 09:47:25 -0500
From: James Vincent 
Subject: Re: [SciPy-user] Fitting sphere to 3d data points
To: SciPy Users List 
Message-ID: 
Content-Type: text/plain; charset="us-ascii"

David,

Thanks for the reply. I think I should have been clearer about the 
problem. I have a surface patch of data points, not an actual whole 
sphere. It will probably be a very small section of total sphere 
surface. I would like to fit the sphere that best fits just those 
points that I have. The center of the sphere will be highly dependent 
on the curvature of the points.

I think the leastsq routine is right, I just can't figure out how 
to pass the data in yet (it's my first work with scipy).

Jim


On Jan 25, 2007, at 9:31 AM, David Huard wrote:

> Hi James,
>
> As a first guess, I'd say the center of the sphere is simply the 
> mean of your data points, if they're all weighted equally. With 
> only one parameter left to fit, it should be easy enough. However, 
> you may want to look at the paper:
>
> Werman, Michael and Keren, Daniel
> A Bayesian method for fitting parametric and nonparametric models 
> to noisy data
> Ieee Transactions on Pattern Analysis and Machine Intelligence, 23, 
> 2001.
>
> They write that the Mean Square Error approach overestimates the 
> radius in the case of circles. They don't talk about the 3D case, 
> but I'd guess similar problems arise. They provide a method to fit 
> parametric shapes with some robustness to data errors.
>
> Cheers,
>
> David
>
>
>
> 2007/1/25, James Vincent :
> Hello,
>
> Is it possible to fit a sphere to 3D data points using 
> scipy.optimize.leastsq? I'd like to minimize the residual for the 
> distance from the actual x,y,z point and the fitted sphere surface. 
> I can see how to minimize for z, but that's not really what I'm 
> looking for. Is there a better way to do this? Thanks for any help.
>
> params = a,b,c and r
> a,b,c are the fitted center point of the sphere, r is the radius
>
> err = distance-to-center - radius
> err = sqrt( x-a)**2 + (y-b)**2 + (z-c)**2) - r
>
>
>
> ----
> James J. Vincent, Ph.D.
> National Cancer Institute
> National Institutes of Health
> Laboratory of Molecular Biology
> Building 37, Room 5120
> 37 Convent Drive, MSC 4264
> Bethesda, MD 20892 USA
>
> 301-451-8755
> jjv5 at nih.gov
>
>
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
>
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user

----
James J. Vincent, Ph.D.
National Cancer Institute
National Institutes of Health
Laboratory of Molecular Biology
Building 37, Room 5120
37 Convent Drive, MSC 4264
Bethesda, MD 20892 USA

301-451-8755
jjv5 at nih.gov


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20070125/dcf1b801/attachment-0001.html 

------------------------------

Message: 2
Date: Thu, 25 Jan 2007 23:57:57 +0900
From: ckkart at hoc.net
Subject: Re: [SciPy-user] Fitting sphere to 3d data points
To: SciPy Users List 
Message-ID: <45B8C575.5060609 at hoc.net>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

James Vincent wrote:
> David,
> 
> Thanks for the reply. I think I should have been clearer about the 
> problem. I have a surface patch of data points, not an actual whole 
> sphere. It will probably be a very small section of total sphere 
> surface. I would like to fit the sphere that best fits just those points 
> that I have. The center of the sphere will be highly dependent on the 
> curvature of the points.
> 
> I think the leastsq routine is right, I just can't figure out how 
> to pass the data in yet (it's my first work with scipy).

Have a look at scipy.sandbox.odr, too. It is a module to do orthogonal 
distance regression in contrast to ordinary least squares of the z-values.

Christian



------------------------------

Message: 3
Date: Thu, 25 Jan 2007 16:47:37 +0100
From: David Douard 
Subject: Re: [SciPy-user] Fitting sphere to 3d data points
To: SciPy Users List 
Message-ID: <20070125154737.GB19408 at crater.logilab.fr>
Content-Type: text/plain; charset="iso-8859-1"

On Thu, Jan 25, 2007 at 09:31:10AM -0500, David Huard wrote:
> Hi James,
> 
> As a first guess, I'd say the center of the sphere is simply the mean of
> your data points, if they're all weighted equally. 

Hello,

I would have rather said that you need to find the point that minimize
the distance to the normals of the triangles you have from your data
points (not sure this is really meaningful...).
If the points really are on a sphere, all the normal will cut on one
point. If not, there really is a minimization problema to solve.

David



> With only one parameter
> left to fit, it should be easy enough. However, you may want to look at the
> paper:
> 
> Werman, Michael and Keren, Daniel
> A Bayesian method for fitting parametric and nonparametric models to noisy
> data
> Ieee Transactions on Pattern Analysis and Machine Intelligence, 23, 2001.
> 
> They write that the Mean Square Error approach overestimates the radius in
> the case of circles. They don't talk about the 3D case, but I'd guess
> similar problems arise. They provide a method to fit parametric shapes with
> some robustness to data errors.
> 
> Cheers,
> 
> David
> 
> 
> 
> 2007/1/25, James Vincent :
> >
> >Hello,
> >Is it possible to fit a sphere to 3D data points using
> >scipy.optimize.leastsq? I'd like to minimize the residual for the distance
> >from the actual x,y,z point and the fitted sphere surface. I can see how to
> >minimize for z, but that's not really what I'm looking for. Is there a
> >better way to do this? Thanks for any help.
> >
> >params = a,b,c and r
> >a,b,c are the fitted center point of the sphere, r is the radius
> >
> >err = distance-to-center - radius
> >err = sqrt( x-a)**2 + (y-b)**2 + (z-c)**2) - r
> >
> >
> >
> >----
> >James J. Vincent, Ph.D.
> >National Cancer Institute
> >National Institutes of Health
> >Laboratory of Molecular Biology
> >Building 37, Room 5120
> >37 Convent Drive, MSC 4264
> >Bethesda, MD 20892 USA
> >
> >301-451-8755
> >jjv5 at nih.gov
> >
> >
> >
> >_______________________________________________
> >SciPy-user mailing list
> >SciPy-user at scipy.org
> >http://projects.scipy.org/mailman/listinfo/scipy-user
> >
> >
> >

> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user


-- 
David Douard LOGILAB, Paris (France)
Formations Python, Zope, Plone, Debian : http://www.logilab.fr/formations
D?veloppement logiciel sur mesure : http://www.logilab.fr/services
Informatique scientifique : http://www.logilab.fr/science
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://projects.scipy.org/pipermail/scipy-user/attachments/20070125/21445e8d/attachment-0001.bin 

------------------------------

Message: 4
Date: Thu, 25 Jan 2007 10:53:56 -0500
From: James Vincent 
Subject: Re: [SciPy-user] Fitting sphere to 3d data points
To: SciPy Users List 
Message-ID: <0C9AE871-55EE-4008-80AE-3B4DA0897615 at nih.gov>
Content-Type: text/plain; charset="iso-8859-1"

Thanks for all the input. I think I've got it. This works:


def resSphere(p,x,y,z):
""" residuals from sphere fit """

a,b,c,r = p # a,b,c are center x,y,c 
coords to be fit, r is the radius to be fit
distance = sqrt( (x-a)**2 + (y-b)**2 + (z-c)**2 )
err = distance - r # err is distance from input 
point to current fitted surface

return err

params = [0.,0.,0.,0.]
myResult = leastsq(resSphere, params, args=(myX,myY,myZ) )
print myResult[0]




On Jan 25, 2007, at 10:47 AM, David Douard wrote:

> On Thu, Jan 25, 2007 at 09:31:10AM -0500, David Huard wrote:
>> Hi James,
>>
>> As a first guess, I'd say the center of the sphere is simply the 
>> mean of
>> your data points, if they're all weighted equally.
>
> Hello,
>
> I would have rather said that you need to find the point that minimize
> the distance to the normals of the triangles you have from your data
> points (not sure this is really meaningful...).
> If the points really are on a sphere, all the normal will cut on one
> point. If not, there really is a minimization problema to solve.
>
> David
>
>
>
>> With only one parameter
>> left to fit, it should be easy enough. However, you may want to 
>> look at the
>> paper:
>>
>> Werman, Michael and Keren, Daniel
>> A Bayesian method for fitting parametric and nonparametric models 
>> to noisy
>> data
>> Ieee Transactions on Pattern Analysis and Machine Intelligence, 
>> 23, 2001.
>>
>> They write that the Mean Square Error approach overestimates the 
>> radius in
>> the case of circles. They don't talk about the 3D case, but I'd guess
>> similar problems arise. They provide a method to fit parametric 
>> shapes with
>> some robustness to data errors.
>>
>> Cheers,
>>
>> David
>>
>>
>>
>> 2007/1/25, James Vincent :
>>>
>>> Hello,
>>> Is it possible to fit a sphere to 3D data points using
>>> scipy.optimize.leastsq? I'd like to minimize the residual for the 
>>> distance
>>> from the actual x,y,z point and the fitted sphere surface. I can 
>>> see how to
>>> minimize for z, but that's not really what I'm looking for. Is 
>>> there a
>>> better way to do this? Thanks for any help.
>>>
>>> params = a,b,c and r
>>> a,b,c are the fitted center point of the sphere, r is the radius
>>>
>>> err = distance-to-center - radius
>>> err = sqrt( x-a)**2 + (y-b)**2 + (z-c)**2) - r
>>>
>>>
>>>
>>> ----
>>> James J. Vincent, Ph.D.
>>> National Cancer Institute
>>> National Institutes of Health
>>> Laboratory of Molecular Biology
>>> Building 37, Room 5120
>>> 37 Convent Drive, MSC 4264
>>> Bethesda, MD 20892 USA
>>>
>>> 301-451-8755
>>> jjv5 at nih.gov
>>>
>>>
>>>
>>> _______________________________________________
>>> SciPy-user mailing list
>>> SciPy-user at scipy.org
>>> http://projects.scipy.org/mailman/listinfo/scipy-user
>>>
>>>
>>>
>
>> _______________________________________________
>> SciPy-user mailing list
>> SciPy-user at scipy.org
>> http://projects.scipy.org/mailman/listinfo/scipy-user
>
>
> -- 
> David Douard LOGILAB, Paris (France)
> Formations Python, Zope, Plone, Debian : http://www.logilab.fr/ 
> formations
> D?veloppement logiciel sur mesure : http://www.logilab.fr/ 
> services
> Informatique scientifique : http://www.logilab.fr/science
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user

----
James J. Vincent, Ph.D.
National Cancer Institute
National Institutes of Health
Laboratory of Molecular Biology
Building 37, Room 5120
37 Convent Drive, MSC 4264
Bethesda, MD 20892 USA

301-451-8755
jjv5 at nih.gov


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20070125/0d68f7c3/attachment.html 

------------------------------

_______________________________________________
SciPy-user mailing list
SciPy-user at scipy.org
http://projects.scipy.org/mailman/listinfo/scipy-user


End of SciPy-user Digest, Vol 41, Issue 62
******************************************

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20070125/2f9cfde6/attachment.html>


More information about the SciPy-User mailing list