Python-pcl 随机采样一致性算法
生活随笔
收集整理的這篇文章主要介紹了
Python-pcl 随机采样一致性算法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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 np
import random
import pcl.pcl_visualization
import math
import sysargvs = sys.argv # for random model
# argvs = '-f' # plane model
# argvs = '-sf' # sphere model
argc = 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_MAXpoints[i][1] = 1024 * random.random() / RAND_MAXif i % 2 == 0:points[i][2] = 1024 * random.random() / RAND_MAXelse:points[i][2] = -1 * (points[i][0] + points[i][1])else:points[i][0] = 1024 * random.random() / RAND_MAXpoints[i][1] = 1024 * random.random() / RAND_MAXif i % 2 == 0:points[i][2] = 1024 * random.random() / RAND_MAXelse: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 = True
if 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
總結
以上是生活随笔為你收集整理的Python-pcl 随机采样一致性算法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: lasTools laszip.exe
- 下一篇: 火狐自定义字体失败 downloadab