【编程实践】利用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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值