[python][pcl]python-pcl案例之为平面模型构造凹凸外壳多边形

news/2024/11/6 21:26:55/

测试环境:

pcl==1.12.1

python-pcl==0.3.1

python==3.7

代码:

# -*- coding: utf-8 -*-
# Construct a concave or convex hull polygon for a plane model
# http://pointclouds.org/documentation/tutorials/hull_2d.php#hull-2dimport numpy as np
import pcl
import randomdef main():#  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>),#                                      cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>),#                                      cloud_projected (new pcl::PointCloud<pcl::PointXYZ>);# cloud = pcl.PointCloud()# cloud_filtered = pcl.PointCloud()# cloud_projected = pcl.PointCloud()#  pcl::PCDReader reader;#  reader.read ("table_scene_mug_stereo_textured.pcd", *cloud);cloud = pcl.load(r"C:\Users\Administrator\Desktop\python-pcl\examples\pcldata\tutorials\table_scene_mug_stereo_textured.pcd")# // Build a filter to remove spurious NaNs# pcl::PassThrough<pcl::PointXYZ> pass;# pass.setInputCloud (cloud);# pass.setFilterFieldName ("z");# pass.setFilterLimits (0, 1.1);# pass.filter (*cloud_filtered);# std::cerr << "PointCloud after filtering has: "#           << cloud_filtered->points.size () << " data points." << std::endl;passthrough = cloud.make_passthrough_filter()passthrough.set_filter_field_name("z")passthrough.set_filter_limits(0.0, 1.1)cloud_filtered = passthrough.filter()print('PointCloud after filtering has: ' +str(cloud_filtered.size) + ' data points.')# 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_filtered);# seg.segment (*inliers, *coefficients);# std::cerr << "PointCloud after segmentation has: "#           << inliers->indices.size () << " inliers." << std::endl;seg = cloud_filtered.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)indices, model = seg.segment()print('PointCloud after segmentation has: ' +str(indices.count) + ' inliers.')#   // Project the model inliers#   pcl::ProjectInliers<pcl::PointXYZ> proj;#   proj.setModelType (pcl::SACMODEL_PLANE);#   proj.setIndices (inliers);#   proj.setInputCloud (cloud_filtered);#   proj.setModelCoefficients (coefficients);#   proj.filter (*cloud_projected);#   std::cerr << "PointCloud after projection has: "#             << cloud_projected->points.size () << " data points." << std::endl;proj = cloud_filtered.make_ProjectInliers()proj.set_model_type(pcl.SACMODEL_PLANE)#   proj.setIndices (inliers);#   proj.setModelCoefficients (coefficients)cloud_projected = proj.filter()print('PointCloud after projection has: ' +str(cloud_projected.size) + ' data points.')#   // Create a Concave Hull representation of the projected inliers#   pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_hull (new pcl::PointCloud<pcl::PointXYZ>);#   pcl::ConcaveHull<pcl::PointXYZ> chull;#   chull.setInputCloud (cloud_projected);#   chull.setAlpha (0.1);#   chull.reconstruct (*cloud_hull);#   std::cerr << "Concave hull has: " << cloud_hull->points.size ()#             << " data points." << std::endl;# cloud_projected = pcl.PointCloud()chull = cloud_projected.make_ConcaveHull()chull.set_Alpha(0.1)cloud_hull = chull.reconstruct()print('Concave hull has: ' + str(cloud_hull.size) + ' data points.')#   pcl::PCDWriter writer;#   writer.write ("table_scene_mug_stereo_textured_hull.pcd", *cloud_hull, false);if cloud_hull.size != 0:pcl.save(cloud_hull, 'table_scene_mug_stereo_textured_hull.pcd')if __name__ == "__main__":# import cProfile# cProfile.run('main()', sort='time')main()

运行结果:

PointCloud after filtering has: 139656 data points.
PointCloud after segmentation has: <built-in method count of list object at 0x0000028CA3429A48> inliers.
PointCloud after projection has: 139656 data points.
Concave hull has: 281 data points.

table_scene_mug_stereo_textured.pcd文件需要去这个地址下载:

https://github.com/strawlab/python-pcl/blob/master/examples/pcldata/tutorials/table_scene_mug_stereo_textured.pcd


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

相关文章

Flutter 使用 ORM 框架管理数据库版本升级

前言 我们前面一篇介绍了使用 floor 这个 ORM 框架处理 Flutter 本地的 SQLite 数据库。使用 ORM 框架最大的好处是简化了数据库维护的代码量,使得我们可以专注于业务代码实现。在之前,我们也讲到了不同 App 版本的 数据表结构变化后,如何使用 sqflite 来处理版本升级。本篇…

1寸,2的照片多大啊

照片规格(英寸) &#xff08;厘米&#xff09; (像素) 数码相机类型1寸 2.5*3.5cm 413*295 身份证大头照 3.3*2.2 390*2602寸 3.5*5.3cm 626*413小2寸&#xff08;护照&#xff09; 4.8*3.3cm 567*390 5 寸 5x3.5 12.7*8.9 1200x840以上 100万像素6 寸 6x4 15.2*10.2 1440x960以…

Android应用图标的尺寸大小

1. 程序启动图标&#xff08;icon launcher&#xff09; 放在mipmap-*dpi下&#xff0c;文件名为ic_launcher.png L DPI ( Low Density Screen&#xff0c;120 DPI )&#xff0c;其图标大小为 36 x 36 pxM DPI ( Medium Density Screen, 160 DPI )&#xff0c;其图标大小为 4…

#10045. 「一本通 2.2 练习 1」Radio Transmission

题目 题目描述 给你一个字符串&#xff0c;它是由某个字符串不断自我连接形成的。但是这个字符串是不确定的&#xff0c;现在只想知道它的最短长度是多少。 输入格式 第一行给出字符串的长度 L&#xff0c;第二行给出一个字符串&#xff0c;全由小写字母组成。 输出格式 …

wx.TextDropTarget中添加拖拽过程的触发函数

除了最为常用的&#xff1a; class ViewerDropTarget(wx.TextDropTarget):def __init__(self, parent):wx.TextDropTarget.__init__(self)def OnDropText(self, x, y, data):pass OnDropText被用于拖拽结束的处理函数之外。另外其实还有很多可以使用的事件&#xff1a; wx.D…

利用二维码实现小程序或公众号推广功能

利用二维码推广小程序或者公众号&#xff0c;比如A分享给B、C等多人&#xff0c;别人扫码后&#xff0c;给A增加一定积分&#xff0c;思路如下&#xff1a; 链接可以做成&#xff1a; http://www.tianyutaoquan.com?uid123 &#xff0c;然后把链接生成二维码&#xff0c;链接…

垂直行业公众号推广(二) 导入客户

☆1、 导入老客户  不管企业的规模怎么样&#xff0c;老客户都会有一定的比例&#xff0c;我们可以通过短信或电话等方式&#xff0c;通知他们&#xff0c;这部分客户转化为粉丝比例是目前最高的&#xff0c;差不多有50%。 ☆2、 让潜在客户关注你  企业跟客户面对面接触适…

微信公众号推广实用教程:H5游戏引流

大家好&#xff0c;我是小豆&#xff0c;一个负责H5游戏定制和公众号运营的老司机&#xff0c;今天继续为大家带来实战经验分享&#xff1a;H5游戏引流公众号~ 如何依靠游戏让公众号用户愿意分享给其他人呢&#xff1f;以下就为大家列举了3大核心要素 排行榜 每个游戏世界都…