博客
关于我
Python-pcl 随机采样一致性算法
阅读量:209 次
发布时间:2019-02-28

本文共 4563 字,大约阅读时间需要 15 分钟。

RANSAC 随机采样一致性算法

RANSAC是一种随机参数估计算法。RANSAC从样本中随机抽选出一个样本子集,使用最小方差估计算法对这个子集计算模型参数,然后计算所有样本与该模型的偏差,在使用一个预先设定好的阈值与偏差比较,当偏差小于阈值时,该样本点属于模型内样本点(inliers),简称局内点或内点。否则为模型外样本点(outliers)。

然后迭代,取inliers的个数最多的作为结果返回;

PCL中Sample_Consensus支持的几种几何模型有:

  • SACMODEL_PLANE:平面模型
  • SACMODEL_LINE:直线模型
  • SACMODEL_CIRCLE2D:二维圆的圆周模型
  • SACMODEL_SPHERE:三维球体模型
  • SACMODEL_CYLINDER:圆柱体模型
  • SACMODEL_CONE:圆锥模型,尚未实现
  • SACMODEL_TORUS: 圆环面模型,尚未实现
  • SACMODEL_PARALLEL_LINE:有条件的直线模型,直线模型与给定轴线平行
  • SACMODEL_PERPENDICULAR_PLANE:有条件限制的平面模型,平面模型与给定轴线垂直
  • SACMODEL_NORMAL_PLANE: 有条件限制的平面模型,第一个局内点的发现必须与估计的平面模型的法线平行
  • SACMODEL_PARALLEL_PLANE: 有条件限制的平明模型 在规定的最大角度偏差限制下,平面模型与给定的轴线平行
  • SACMODEL_NORMAL_PARLLEL_PLANE:有条件限制的平面模型 三维平面模型与用户设定的轴线平行

下边的例子演示了

无参数时原始点云平面内点与立方体外点
参数为-f 提取得到的平面上的内点
参数为-s 提取得到圆球内点与立方体外点
参数为 -sf 提取圆球上的点为内点

无参数

在这里插入图片描述参数为-f
在这里插入图片描述

参数为-s

在这里插入图片描述参数为-sf
在这里插入图片描述

# -*- coding: utf-8 -*-# How to use Random Sample Consensus model# http://pointclouds.org/documentation/tutorials/random_sample_consensus.php#random-sample-consensusimport numpy as npimport randomimport pcl.pcl_visualizationimport mathimport sysargvs = sys.argv  # for random model# argvs = '-f' # plane model# argvs = '-sf' # sphere modelargc = len(argvs)  # 参数个数print(argc)print('argvs: ',argvs)# 初始化点云cloud = pcl.PointCloud()final = pcl.PointCloud()points = np.zeros((5000, 3), dtype=np.float32)RAND_MAX = 1024for i in range(0, 5000):    if argc > 1:        if argvs[1] == "-s" or argvs[1] == "-sf":            points[i][0] = 1024 * random.random() / (RAND_MAX + 1.0)            points[i][1] = 1024 * random.random() / (RAND_MAX + 1.0)            if i % 5 == 0:                points[i][2] = 1024 * random.random() / (RAND_MAX + 1.0)            elif i % 2 == 0:                points[i][2] = math.sqrt(math.fabs(1 - (points[i][0] * points[i][0]) - (points[i][1] * points[i][1])))            else:                points[i][2] = -1 * math.sqrt(                    math.fabs(1 - (points[i][0] * points[i][0]) - (points[i][1] * points[i][1])))        else:            points[i][0] = 1024 * random.random() / RAND_MAX            points[i][1] = 1024 * random.random() / RAND_MAX            if i % 2 == 0:                points[i][2] = 1024 * random.random() / RAND_MAX            else:                points[i][2] = -1 * (points[i][0] + points[i][1])    else:        points[i][0] = 1024 * random.random() / RAND_MAX        points[i][1] = 1024 * random.random() / RAND_MAX        if i % 2 == 0:            points[i][2] = 1024 * random.random() / RAND_MAX        else:            points[i][2] = -1 * (points[i][0] + points[i][1])cloud.from_array(points)model_s = pcl.SampleConsensusModelSphere(cloud)model_p = pcl.SampleConsensusModelPlane(cloud)if argc > 1:    if argvs[1] == "-f":        ransac = pcl.RandomSampleConsensus(model_p)        ransac.set_DistanceThreshold(.01)        ransac.computeModel()        inliers = ransac.get_Inliers()    elif argvs[1] == "-sf":        ransac = pcl.RandomSampleConsensus(model_s)        ransac.set_DistanceThreshold(.01)        ransac.computeModel()        inliers = ransac.get_Inliers()    else:        inliers = []else:    inliers = []print(str(len(inliers)))if len(inliers) != 0:    finalpoints = np.zeros((len(inliers), 3), dtype=np.float32)    for i in range(0, len(inliers)):        finalpoints[i][0] = cloud[inliers[i]][0]        finalpoints[i][1] = cloud[inliers[i]][1]        finalpoints[i][2] = cloud[inliers[i]][2]    final.from_array(finalpoints)isWindows = Trueif isWindows == True:    if argc > 1:        if argvs[1] == "-f" or argvs[1] == "-sf":            viewer = pcl.pcl_visualization.PCLVisualizering('3D Viewer')            viewer.SetBackgroundColor(0, 0, 0)            viewer.AddPointCloud(final, b'inliers cloud')            viewer.SetPointCloudRenderingProperties(pcl.pcl_visualization.PCLVISUALIZER_POINT_SIZE, 3, b'inliers cloud')            viewer.InitCameraParameters()        else:            viewer = pcl.pcl_visualization.PCLVisualizering('3D Viewer')            viewer.SetBackgroundColor(0, 0, 0)            viewer.AddPointCloud(cloud, b'sample cloud')            viewer.SetPointCloudRenderingProperties(pcl.pcl_visualization.PCLVISUALIZER_POINT_SIZE, 3, b'sample cloud')            viewer.InitCameraParameters()    else:        viewer = pcl.pcl_visualization.PCLVisualizering('3D Viewer')        viewer.SetBackgroundColor(0, 0, 0)        viewer.AddPointCloud(cloud, b'sample cloud')        viewer.SetPointCloudRenderingProperties(pcl.pcl_visualization.PCLVISUALIZER_POINT_SIZE, 3, b'sample cloud')        viewer.InitCameraParameters()    while viewer.WasStopped() != True:        viewer.SpinOnce(100)else:    pass

转载地址:http://sxai.baihongyu.com/

你可能感兴趣的文章
mysql CPU使用率过高的一次处理经历
查看>>
Multisim中555定时器使用技巧
查看>>
MySQL CRUD 数据表基础操作实战
查看>>
multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
查看>>
mysql csv import meets charset
查看>>
multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
查看>>
MySQL DBA 数据库优化策略
查看>>
multi_index_container
查看>>
mutiplemap 总结
查看>>
MySQL Error Handling in Stored Procedures---转载
查看>>
MVC 区域功能
查看>>
MySQL FEDERATED 提示
查看>>
mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
查看>>
Mysql group by
查看>>
MySQL I 有福啦,窗口函数大大提高了取数的效率!
查看>>
mysql id自动增长 初始值 Mysql重置auto_increment初始值
查看>>
MySQL in 太多过慢的 3 种解决方案
查看>>
Mysql Innodb 锁机制
查看>>
MySQL InnoDB中意向锁的作用及原理探
查看>>
MySQL InnoDB事务隔离级别与锁机制深入解析
查看>>