As in the title, I want to fit a cylinder to a group of 3D points with PYTHON. This is a nice solution with MATLAB. How can we do it with Python?

解决方案

Using scipy.optimize.leastsq, we can create an error function in which the difference between the observed cylinder radius and the modelled radius is minimized. The following is an example of fitting a vertical cylinder

import numpy as np

from scipy.optimize import leastsq

def cylinderFitting(xyz,p,th):

"""

This is a fitting for a vertical cylinder fitting

Reference:

http://www.int-arch-photogramm-remote-sens-spatial-inf-sci.net/XXXIX-B5/169/2012/isprsarchives-XXXIX-B5-169-2012.pdf

xyz is a matrix contain at least 5 rows, and each row stores x y z of a cylindrical surface

p is initial values of the parameter;

p[0] = Xc, x coordinate of the cylinder centre

P[1] = Yc, y coordinate of the cylinder centre

P[2] = alpha, rotation angle (radian) about the x-axis

P[3] = beta, rotation angle (radian) about the y-axis

P[4] = r, radius of the cylinder

th, threshold for the convergence of the least squares

"""

x = xyz[:,0]

y = xyz[:,1]

z = xyz[:,2]

fitfunc = lambda p, x, y, z: (- np.cos(p[3])*(p[0] - x) - z*np.cos(p[2])*np.sin(p[3]) - np.sin(p[2])*np.sin(p[3])*(p[1] - y))**2 + (z*np.sin(p[2]) - np.cos(p[2])*(p[1] - y))**2 #fit function

errfunc = lambda p, x, y, z: fitfunc(p, x, y, z) - p[4]**2 #error function

est_p , success = leastsq(errfunc, p, args=(x, y, z), maxfev=1000)

return est_p

if __name__=="__main__":

np.set_printoptions(suppress=True)

xyz = np.loadtxt('cylinder11.xyz')

#print xyz

print "Initial Parameters: "

p = np.array([-13.79,-8.45,0,0,0.3])

print p

print " "

print "Performing Cylinder Fitting ... "

est_p = cylinderFitting(xyz,p,0.00001)

print "Fitting Done!"

print " "

print "Estimated Parameters: "

print est_p

python圆柱体,用PYTHON将圆柱体分散到3D XYZ点数据相关推荐

  1. Python之路,Python基础1

    本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...

  2. Python之路 - Python基础1

    本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...

  3. 【python】用python实现wordcloud大数据词云图分析

    [python]用python实现wordcloud大数据词云图分析 一.应用场景 二.工具包安装及代码 三.中文不能正常显示问题 四.关键词重复问题 五.最终结论 一.应用场景 大数据词云(word ...

  4. Python培训:Python有哪些函数?你了解几种?

    本期小编要为大家带来的Python教程就是关于Python函数这方面的,我们都知道Python函数,一般是指组织好的.可重复使用的.用来实现单一或相关联功能的代码段,Python函数包含系统中自带的一 ...

  5. python新手任务:python循环嵌套

    2019独角兽企业重金招聘Python工程师标准>>> Python 循环嵌套 Python 语言允许在一个循环体里面嵌入另一个循环. Python for 循环嵌套语法: Pyth ...

  6. python pandas库读取excel/csv中指定行或列数据详解

    通过阅读表格,可以发现Pandas中提供了非常丰富的数据读写方法,下面这篇文章主要给大家介绍了关于python利用pandas库读取excel/csv中指定行或列数据的相关资料,需要的朋友可以参考下 ...

  7. python文件读取方法read(size)的含义是_在Python中可使用read([size])来读取文件中的数据,如果参数size省略,则读取文件中的()。...

    [单选题]李明在他所属的公司工作五年,每天都很认真地处理繁杂的事情,同事们都夸他认真,但是依然没有建树,这是因为: [多选题]品牌标志的作用表现在 [单选题]新产品开发的第一个阶段是_______. ...

  8. python中numpy数组和字符串互转(互转后数据完全一致)

    python中numpy数组和字符串互转(互转后数据完全一致) 代码: import numpy as np # 创建数组arr arr = np.array([ -1.8264365 , -0.63 ...

  9. Python入门 Python自学路线 Python如何学习

    本文介绍Python入门 Python自学路线 Python如何学习.先说点题外话吧:首先呢,我刚开始接触编程的时候,学的是C,那时候Python还没有这么火,后来学了C++,PHP,Java,前端. ...

最新文章

  1. Linux设备模型(热插拔、mdev 与 firmware)
  2. 快速了解和使用Photon Server
  3. dede:arclist 不能调用文章的副栏目或多个副栏目的解决方法
  4. SQL正在击败NoSQL,这对未来的数据意味着什么
  5. mysql注册成功为啥启动不了mysql_mysql启动不成功的解决方法
  6. LeetCode 995. K 连续位的最小翻转次数(差分思想)
  7. NodeJs本地搭建服务器,模拟接口请求,获取json数据
  8. python基础(1)——简介与安装
  9. python sorted函数
  10. Backpropogation反向传播公式推导【李宏毅深度学习版】
  11. 高斯加权滤波matlab,简单易懂的高斯滤波
  12. 计算机等级考试如何评改试题,全国计算机考试上机考试是如何改卷的
  13. 电流测试c语言算法,真有效值的定义及其C语言算法推导
  14. 2022年31省市数字化转型路线图
  15. MySQL的binLog、redoLog、undoLog是什么?
  16. Java微信开发_Exception_02_errcode:40164,errmsg:invalid ip 61.172.68.219, not in whitelist hint...
  17. 7-18 二分法求多项式单根 (C语言)
  18. oracle数据linux中的位置,Oracle中执行计划的存储位置
  19. 分享郑州买房后转LPR贷款利率带来的效益
  20. C语言界杠把子的书籍,你读过几本?

热门文章

  1. 面向对象八大设计原则
  2. Android的系统服务一览
  3. 【PC工具】Windows下用RaiDrive挂载各种网盘为本地硬盘,挂载Google Drive网盘为本地硬盘使用...
  4. 揭开雷达的面纱(科普)探测能力
  5. 网联靴子将落地,银行将关闭第三方支付直接代扣通道
  6. 新JEP将简化Java类型变异
  7. QT中封装的IP地址的widget
  8. WCF Membership Provider
  9. 2021考研数一李正元400题、张宇4套卷题目整理
  10. 像证券交易员一样思考_2.理解绝招