• 【编程实践】利用pcl实现点云凸包点生成


    1 运行结果

    在这里插入图片描述
    生成的凸包点与原点云的可视化
    在这里插入图片描述

    2 代码实现

    // convex hull
    
    #include <pcl/point_types.h>
    #include <pcl/io/pcd_io.h>
    #include <pcl/io/vtk_io.h>
    #include <pcl/surface/convex_hull.h>
    #include <pcl/visualization/pcl_visualizer.h>
    #include <pcl/surface/gp3.h>
    #include <pcl/surface/organized_fast_mesh.h>
    #include <pcl/features/normal_3d.h>
    #include <pcl/filters/project_inliers.h>
    #include <ctime>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in(new pcl::PointCloud<pcl::PointXYZ>); //存放读取点云的对象
    	pcl::PCDReader reader;	//定义点云读取对象
    	if (reader.read(".\\input\\Kuangshan_long.pcd", *cloud_in) < 0)
    	{
    		PCL_ERROR("\a->The file does not exist.\n");
    		system("pause");
    		return -1;
    	}
    	cout << "You have loaded " << cloud_in->points.size() << " points. " << endl;
    	//=========================创建凸包可视化==========================
    	pcl::ConvexHull<pcl::PointXYZ> convex_hull;
    	convex_hull.setInputCloud(cloud_in);
    	pcl::PointCloud<pcl::PointXYZ>::Ptr hull_out(new pcl::PointCloud<pcl::PointXYZ>);
    	convex_hull.reconstruct(*hull_out);
    	pcl::visualization::PCLVisualizer viewer("ConvexHull Vis");
    	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> color(cloud_in, 255, 0, 0);
    	viewer.addPointCloud<pcl::PointXYZ>(cloud_in, color, "cloud_in");
    	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> color_cx(hull_out, 0, 255, 0);
    	viewer.addPointCloud<pcl::PointXYZ>(hull_out, color_cx, "hull_out");
    	for (size_t i = 0; i < hull_out->size(); ++i)
    	{
    		size_t next_index = (i + 1) % hull_out->size();
    		pcl::PointXYZ point1 = hull_out->points[i];
    		pcl::PointXYZ point2 = hull_out->points[next_index];
    		viewer.addLine<pcl::PointXYZ>(point1, point2, 255, 255, 255, "line" + std::to_string(i));
    
    	}
    	viewer.setBackgroundColor(0, 0, 0);
    	viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud_in");
    	viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "hull_out");
    	//获取当前时间
    	std::time_t now = std::time(nullptr);
    	//时间转化为本地时间
    	std::tm* localTime = std::localtime(&now);
    	int year_lt = localTime->tm_year + 1900;
    	int month_lt = localTime->tm_mon + 1;
    	int day_lt = localTime->tm_mday;
    	int ymd_lt = year_lt * 10000 + month_lt * 100 + day_lt;
    	std::string ymd_str = std::to_string(ymd_lt);
    	///保存为PCD格式
    	if (!hull_out->empty())
    	{
    		pcl::io::savePCDFile(".\\output\\0912\\ConvexHull_result" + ymd_str + ".pcd", *hull_out);
    		cout << "->(ASICC)The number of PointHull saved is: " << hull_out->points.size() << endl;
    	}
    	else
    	{
    		PCL_ERROR("\a->保存点云为空!\n");
    		system("pause");
    		return -1;
    	}
    	cout << "Process done!" << endl;
    	
    	while (!viewer.wasStopped())
    	{
    		viewer.spinOnce();
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
  • 相关阅读:
    Go——数组
    Docker 网络虚拟化基础之网络 namespace
    Conda常用命令及Pycharm使用虚拟环境
    王颖奇:ONES.ai 上线,以及我的一些思考
    LCR 177. 撞色搭配
    SQLite3数据库的介绍和使用(面向业务编程-数据库)
    portainer管理远程docker和docker-swarm集群
    Oracle GoldenGate(OGG)到入土
    js中各种数据类型检测与判定
    uniapp抽取组件绑定事件中箭头函数含花括号无法解析
  • 原文地址:https://blog.csdn.net/weixin_41512747/article/details/132851199