[python][pcl]python-pcl案例之平面模型分割

news/2024/10/9 12:30:43/

测试环境:

pcl==1.12.1

python-pcl==0.3.1

python==3.7

代码:

# -*- coding: utf-8 -*-
# http://pointclouds.org/documentation/tutorials/planar_segmentation.php#planar-segmentationimport pcl
import numpy as np
import randomdef main():#   pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);##   // Fill in the cloud data#   cloud->width  = 15;#   cloud->height = 1;#   cloud->points.resize (cloud->width * cloud->height);##   // Generate the data#   for (size_t i = 0; i < cloud->points.size (); ++i)#   {#     cloud->points[i].x = 1024 * rand () / (RAND_MAX + 1.0f);#     cloud->points[i].y = 1024 * rand () / (RAND_MAX + 1.0f);#     cloud->points[i].z = 1.0;#   }##   // Set a few outliers#   cloud->points[0].z = 2.0;#   cloud->points[3].z = -2.0;#   cloud->points[6].z = 4.0;###cloud = pcl.PointCloud()points = np.zeros((15, 3), dtype=np.float32)RAND_MAX = 1024.0for i in range(0, 15):points[i][0] = 1024 * random.random() / (RAND_MAX + 1.0)points[i][1] = 1024 * random.random() / (RAND_MAX + 1.0)points[i][2] = 1.0points[0][2] = 2.0points[3][2] = -2.0points[6][2] = 4.0cloud.from_array(points)#   std::cerr << "Point cloud data: " << cloud->points.size () << " points" << std::endl;#   for (size_t i = 0; i < cloud->points.size (); ++i)#     std::cerr << "    " << cloud->points[i].x << " "#                         << cloud->points[i].y << " "#                         << cloud->points[i].z << std::endl;#print('Point cloud data: ' + str(cloud.size) + ' points')for i in range(0, cloud.size):print('x: ' + str(cloud[i][0]) + ', y : ' +str(cloud[i][1]) + ', z : ' + str(cloud[i][2]))#   pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);#   pcl::PointIndices::Ptr inliers (new pcl::PointIndices);#   // Create the segmentation object#   pcl::SACSegmentation<pcl::PointXYZ> seg;#   // Optional#   seg.setOptimizeCoefficients (true);#   // Mandatory#   seg.setModelType (pcl::SACMODEL_PLANE);#   seg.setMethodType (pcl::SAC_RANSAC);#   seg.setDistanceThreshold (0.01);##   seg.setInputCloud (cloud);#   seg.segment (*inliers, *coefficients);#### http://www.pcl-users.org/pcl-SACMODEL-CYLINDER-is-not-working-td4037530.html# NG?# seg = cloud.make_segmenter()# seg.set_optimize_coefficients(True)# seg.set_model_type(pcl.SACMODEL_NORMAL_PLANE)# seg.set_method_type(pcl.SAC_RANSAC)# seg.set_distance_threshold(0.01)# indices, coefficients = seg.segment()seg = cloud.make_segmenter_normals(ksearch=50)seg.set_optimize_coefficients(True)seg.set_model_type(pcl.SACMODEL_NORMAL_PLANE)seg.set_method_type(pcl.SAC_RANSAC)seg.set_distance_threshold(0.01)seg.set_normal_distance_weight(0.01)seg.set_max_iterations(100)indices, coefficients = seg.segment()#   if (inliers->indices.size () == 0)#   {#     PCL_ERROR ("Could not estimate a planar model for the given dataset.");#     return (-1);#   }#   std::cerr << "Model coefficients: " << coefficients->values[0] << " "#                                       << coefficients->values[1] << " "#                                       << coefficients->values[2] << " "#                                       << coefficients->values[3] << std::endl;###if len(indices) == 0:print('Could not estimate a planar model for the given dataset.')exit(0)print('Model coefficients: ' + str(coefficients[0]) + ' ' + str(coefficients[1]) + ' ' + str(coefficients[2]) + ' ' + str(coefficients[3]))#   std::cerr << "Model inliers: " << inliers->indices.size () << std::endl;#   for (size_t i = 0; i < inliers->indices.size (); ++i)#     std::cerr << inliers->indices[i] << "    " << cloud->points[inliers->indices[i]].x << " "#                                                << cloud->points[inliers->indices[i]].y << " "#                                                << cloud->points[inliers->indices[i]].z << std::endl;###print('Model inliers: ' + str(len(indices)))for i in range(0, len(indices)):print(str(indices[i]) + ', x: ' + str(cloud[indices[i]][0]) + ', y : ' +str(cloud[indices[i]][1]) + ', z : ' + str(cloud[indices[i]][2]))if __name__ == "__main__":# import cProfile# cProfile.run('main()', sort='time')main()

运行结果:

Point cloud data: 15 points
x: 0.4073253273963928, y : 0.18234382569789886, z : 2.0
x: 0.6126348376274109, y : 0.07198140025138855, z : 1.0
x: 0.20239822566509247, y : 0.7896735072135925, z : 1.0
x: 0.019813423976302147, y : 0.8002557158470154, z : -2.0
x: 0.560012936592102, y : 0.6460093855857849, z : 1.0
x: 0.11512751877307892, y : 0.29834282398223877, z : 1.0
x: 0.5930672883987427, y : 0.4660494923591614, z : 4.0
x: 0.326516330242157, y : 0.2036980241537094, z : 1.0
x: 0.0727834403514862, y : 0.2508666515350342, z : 1.0
x: 0.6440120935440063, y : 0.9432345628738403, z : 1.0
x: 0.3259447515010834, y : 0.9963557124137878, z : 1.0
x: 0.7400149703025818, y : 0.9620276689529419, z : 1.0
x: 0.16831167042255402, y : 0.2292098104953766, z : 1.0
x: 0.69353848695755, y : 0.7191517353057861, z : 1.0
x: 0.30966588854789734, y : 0.47624409198760986, z : 1.0
Model coefficients: 0.7691919207572937 -0.6329861283302307 -0.08759189397096634 0.18918786942958832
Model inliers: 4
5, x: 0.11512751877307892, y : 0.29834282398223877, z : 1.0
6, x: 0.5930672883987427, y : 0.4660494923591614, z : 4.0
8, x: 0.0727834403514862, y : 0.2508666515350342, z : 1.0
9, x: 0.6440120935440063, y : 0.9432345628738403, z : 1.0
 


http://www.ppmy.cn/news/681631.html

相关文章

JS知识点汇总(一)--数组常用方法

1. Javscript数组的常用方法有哪些&#xff1f; 1、操作方法 数组基本操作可以归纳为 增、删、改、查。 1. 增 下面前三种是对原数组产生影响的增添方法&#xff0c;第四种则不会对原数组产生影响 push()unshift()splice()concat() push() push()方法接收任意数量的参数&…

ArcMap发布arcgis sever缓存瓦片服务

1.全能电子地图下载瓦片地图 2.地图拼接 acrgis sever缓存瓦片 下载完成的文件目录 3.arcMap添加数据 连接本地文件 这一步有坑&#xff0c;一定要加载到瓦片最外层目录&#xff0c;否则看不到图层文件。 找到图层数据&#xff0c;点击添加 添加数据完成&#xff0c;开始发布服…

2017年全国硕士研究生入学统一考试管理类专业学位联考逻辑试题——纯享题目版

&#x1f3e0;个人主页&#xff1a;fo安方的博客✨ &#x1f482;个人简历&#xff1a;大家好&#xff0c;我是fo安方&#xff0c;考取过HCIE Cloud Computing、CCIE Security、CISP、RHCE、CCNP RS、PEST 3等证书。&#x1f433; &#x1f495;兴趣爱好&#xff1a;b站天天刷&…

创维电视酷开系统安装第三方软件 如创维电视安装电视家3.0

创维电视酷开系统安装第三方软件具体步骤如下&#xff1a; 以安装电视家为例&#xff1a; 1、需要用到U盘。到电视家官网下载电视版电视家的apk文件到U盘&#xff0c;把U盘插入到电视机上。 2、打开电视&#xff0c;按遥控器上的菜单&#xff0c;进入【设置】界面&#xff0…

估值近百亿,“创维三把手”酷开科技能否顺利赶考?

你会用大屏电视去看影片吗&#xff1f; 电视&#xff0c;似乎已经沦为妈妈辈们消磨空闲时光的好帮手&#xff0c;渐渐地在年轻人的世界里消失。然而&#xff0c;当越来越多巨头竞相推出大屏电视&#xff0c;办公室会议更频繁地采用大屏投影时&#xff0c;似乎彰显着大屏电视时…

OTT反超PC,酷开拯救不了创维

智能家居早有雏形&#xff0c;随着家电市场逐渐看到扩张的天花板&#xff0c;资本市场推动智能化与家电结合&#xff0c;形成独特的场景化家居市场&#xff0c;也就是当前的智能家居场景化风口。 在家庭场景中&#xff0c;从开门、开灯、开热水器等等动作&#xff0c;都需要人…

酷开系统 | 酷开科技创新突围,夯实品牌实力

过去的一年&#xff0c;在全球经济增长放缓的背景下&#xff0c;电视市场步入存量时代&#xff0c;规模持续下滑&#xff0c;与之相反的却是消费者对于电视大屏的需求越来越多样化了。为了满足消费者多样化的需求&#xff0c;OTT行业内估值近百亿的独角兽公司酷开科技在技术上持…

酷开电视adb

一、关于ADB 1、adb概念&#xff1a; adb的全称为Android Debug Bridge&#xff0c;就是起到调试桥的作用。通过adb我们可以在Eclipse中方便通过DDMS来调试Android程序&#xff0c;说白了就是debug工具。adb的工作方式比较特殊&#xff0c;采用监听Socket TCP 5554等端口的方式…