博客
关于我
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/

你可能感兴趣的文章
MyEclipse设置当前行背景颜色、选中单词前景色、背景色
查看>>
myeclipse配置springmvc教程
查看>>
MyEclipse配置SVN
查看>>
MTCNN 人脸检测
查看>>
MyEcplise中SpringBoot怎样定制启动banner?
查看>>
MyPython
查看>>
MTD技术介绍
查看>>
MySQL
查看>>
MySQL
查看>>
mysql
查看>>
MTK Android 如何获取系统权限
查看>>
MySQL - 4种基本索引、聚簇索引和非聚索引、索引失效情况、SQL 优化
查看>>
MySQL - ERROR 1406
查看>>
mysql - 视图
查看>>
MySQL - 解读MySQL事务与锁机制
查看>>
MTTR、MTBF、MTTF的大白话理解
查看>>
mt_rand
查看>>
mysql /*! 50100 ... */ 条件编译
查看>>
mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>