Fitting sphere to 3d data points
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@nih.gov
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 <jjv5@nih.gov>:
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@nih.gov
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
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 <jjv5@nih.gov>: 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@nih.gov
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
_______________________________________________ SciPy-user mailing list SciPy-user@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@nih.gov
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
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).
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 <jjv5@nih.gov <mailto:jjv5@nih.gov>>:
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@nih.gov <mailto:jjv5@nih.gov>
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org <mailto:SciPy-user@scipy.org> http://projects.scipy.org/mailman/listinfo/scipy-user
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org <mailto:SciPy-user@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@nih.gov <mailto:jjv5@nih.gov>
------------------------------------------------------------------------
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user Hi,
I have a sequence of decimal number (1,2,...16) for example, I want if there is any function which can convert each one in binary. Thanks -- Franck African Institute for Mathematical Sciences -- www.aims.ac.za
Hi Franck When you post, please don't reply to another thread of messages with a good descriptive subject. Your message not only gets lost by any email client that sorts by threads (mails with the same subject matter) it also annoys some readers who use this feature. Please start a blank message if you have a new question, and give it a descriptive subject. On Fri, Mar 02, 2007 at 12:15:50PM +0200, Franck Kalala Mutombo wrote:
I have a sequence of decimal number (1,2,...16) for example, I want if there is any function which can convert each one in binary.
A quick google of int2bin+python found this: def int2bin(num, width=32): return ''.join(['%c'%(ord('0')+bool((1<<k)&num)) for k in range((width-1),-1,-1)]) cheers, Jan -- .~. /V\ Jan Groenewald /( )\ www.aims.ac.za ^^-^^
Jan Groenewald wrote:
Hi Franck
When you post, please don't reply to another thread of messages with a good descriptive subject. Your message not only gets lost by any email client that sorts by threads (mails with the same subject matter) it also annoys some readers who use this feature.
Please start a blank message if you have a new question, and give it a descriptive subject.
On Fri, Mar 02, 2007 at 12:15:50PM +0200, Franck Kalala Mutombo wrote:
I have a sequence of decimal number (1,2,...16) for example, I want if there is any function which can convert each one in binary.
A quick google of int2bin+python found this:
def int2bin(num, width=32): return ''.join(['%c'%(ord('0')+bool((1<<k)&num)) for k in range((width-1),-1,-1)])
cheers, Jan Dear Jan
Thank you for your advices and for your help. cheers Franck -- Franck African Institute for Mathematical Sciences -- www.aims.ac.za
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 <jjv5@nih.gov>:
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@nih.gov
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
_______________________________________________ SciPy-user mailing list SciPy-user@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
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 <jjv5@nih.gov>:
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@nih.gov
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
_______________________________________________ SciPy-user mailing list SciPy-user@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@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@nih.gov
Thanks for all the input. I _think_ I've got it. (emphasis added) Have you tested it w/ points known to lie on a sphere, e.g., (+/-1, 0, 0), (0,+/-1,0), (0,0,+/-1), which should of course yield a=b=c=0, r=1 with zero residual error? One reason why I ask is that, when fitting data to a polynomial model (a sphere is the locus of the solution set of
James Vincent wrote: the polynomial A(x^2 + y^2 + z^2) + Bx + Cy + Dz -1 = 0) I usually like to take advantage of the fact that a polynomial is a linear function of the "monomial" pieces, in this case x^2 + y^2 + z^2, x, y, & z. Since in general one can't solve a polynomial for what one wants formulaically, this is the only general way to linear least squares fit data to a polynomial; of course in your quadratic case, one can solve for any one of the data variables in terms of the others (as you have done), but to do so you have to introduce the square-root function, which numerically speaking, isn't quite as exact as simply squaring. In other words, I would respectfully suggest that you fit the four quantities (x^2 + y^2 + z^2), x, y, & z to the model A(x^2 + y^2 + z^2) + Bx + Cy + Dz -1 = 0 and calculate your a, b, c, and r from the A, B, C, & D. (And even if you choose not to do that, at least test your algorithm with some pseudo-data chosen to yield known exact results.) DG PS: I've had to fit data to a circle before (using IDL); if anyone else thinks they might have to do something like this in the future, I could contribute "under-test" polyFit, circleFit, and sphereFit functions, not immediately 'cause I'm under deadline, but in the not too distant future.
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 <jjv5@nih.gov <mailto:jjv5@nih.gov>>:
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@nih.gov <mailto:jjv5@nih.gov>
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org <mailto:SciPy-user@scipy.org> http://projects.scipy.org/mailman/listinfo/scipy-user
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org <mailto:SciPy-user@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@scipy.org <mailto:SciPy-user@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@nih.gov <mailto:jjv5@nih.gov>
------------------------------------------------------------------------
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
participants (7)
-
ckkart@hoc.net
-
David Douard
-
David Goldsmith
-
David Huard
-
Franck Kalala Mutombo
-
James Vincent
-
jan@aims.ac.za