PCL 投影点云

该博客介绍了如何利用PCL库在C++中将点云数据投影到XOY平面并进行可视化。通过加载PCD文件,设置平面模型参数,应用滤波器去除不符合条件的点,然后使用PCLVisualizer进行点云的双窗口展示,分别以蓝色和红色区分原始点云和投影后的点云。此外,还列举了PCL支持的其他投影模型,如线、圆、球体等。
摘要由CSDN通过智能技术生成

投影到XOY平面上

#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/filters/project_inliers.h>
#include <pcl/visualization/pcl_visualizer.h>

using namespace std;
using namespace pcl;

void visualize(PointCloud<PointXYZ>::Ptr source, PointCloud<PointXYZ>::Ptr target)
{
	visualization::PCLVisualizer viewer("Point Cloud Viewer");

	// 创建两个显示窗口
	int v1, v2;
	viewer.createViewPort(0, 0.0, 0.5, 1.0, v1);
	viewer.createViewPort(0.5, 0.0, 1.0, 1.0, v2);
	// 设置背景颜色
	viewer.setBackgroundColor(255, 255, 255, v1);
	viewer.setBackgroundColor(255, 255, 255, v2);

	// 给点云添加颜色
	visualization::PointCloudColorHandlerCustom<PointXYZ> source_color(source, 0, 0, 255);  // blue

	visualization::PointCloudColorHandlerCustom<PointXYZ> target_color(target, 255, 0, 0);  // red


	// 添加点云到显示窗口
	viewer.addPointCloud(source, source_color, "source cloud", v1);
	viewer.addPointCloud(target, target_color, "target cloud", v2);


	while (!viewer.wasStopped())
	{
		viewer.spinOnce(100);
		boost::this_thread::sleep(boost::posix_time::microseconds(100000));
	}

}


int main(int argc, char** argv)
{
	PointCloud<PointXYZ>::Ptr cloud(new pcl::PointCloud<PointXYZ>);
	PointCloud<PointXYZ>::Ptr filtered_cloud(new pcl::PointCloud<PointXYZ>);

	io::loadPCDFile("D:\\Data\\rabbit.pcd", *cloud);
	// 输出滤波前点的个数
	cout << "滤波前有:" << cloud->points.size() << "个点" << endl;

	// 设置模型参数的系数
	ModelCoefficients::Ptr coefficients(new ModelCoefficients());
	coefficients->values.resize(4);
	coefficients->values[0] = 0;
	coefficients->values[1] = 0;
	coefficients->values[2] = 1.0;
	coefficients->values[3] = 0;

	// 实例化滤波器对象
	ProjectInliers<PointXYZ> proj;
	proj.setModelType(SACMODEL_PLANE);
	proj.setInputCloud(cloud);
	proj.setModelCoefficients(coefficients);
	proj.filter(*filtered_cloud);

	cout << "滤波后有:" << filtered_cloud->points.size() << "个点" << endl;

	visualize(cloud, filtered_cloud);

	return 0;
}

可以投影的其它模型

可以使用的投影模型如下:
平面模型:SACMODEL_PLANE
线模型:SACMODEL_LINE
平面上的二维圆:SACMODEL_CIRCLE2D
平面上的三维圆:SACMODEL_CIRCLE3D
球体模型:SACMODEL_SPHERE
圆柱模型:SACMODEL_CYLINDER
圆锥模型:SACMODEL_CONE
圆环模型:SACMODEL_TORUS
平行于给定轴的一种线模型:SACMODEL_PARALLEL_LINE
垂直于指定轴的平面模型:SACMODEL_PERPENDICULAR_PLANE
三维棒分割模型:SACMODEL_STICK

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AICVer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值