pcl 基本操作汇总

目录

PCLVisualizer简单的点云可视化

createViewPort创建视窗

代码 

效果

点云视窗上打印文本信息

使用addText

合并多个点云

xyz+xyz

xyz + nxnynz

新建自己的Point类型

点云的刚体变换(旋转+平移)


以下是pcl点云基本操作,后面会慢慢更新。

PCLVisualizer简单的点云可视化

#include <iostream>
#include <pcl\io\pcd_io.h>
#include <pcl\visualization\pcl_visualizer.h>
 
using namespace std;
 
int main()
{
	// 加载数据
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::PCDReader reader;
	if (reader.read(".\\models\\horse.pcd", *cloud) < 0)
	{
		PCL_ERROR("not exists!\n");
		system("pause");
		return -1;
	}
 
	// 可视化点云
	boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("viewer_window_name"));
	viewer->addPointCloud<pcl::PointXYZ>(cloud, "horse_cloud");  // 将点云附加到视窗中,并对该点云定义唯一的ID“horse_cloud”
	while (!viewer->wasStopped())
	{
		viewer->spinOnce(100);  // 100 ms 刷新一次
	}
 
	return 0;
}

createViewPort创建视窗

代码 

#include <pcl/visualization/cloud_viewer.h>

int main()
{
	boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("PCL Viewer"));
	viewer->initCameraParameters();  // 初始化相机默认参数
	// 一个视窗
	//int v1(0);
	//viewer->createViewPort(0.0, 0.0, 1.0, 1.0, v1);   // x[0,1], y[0,1]
	//viewer->setBackgroundColor(255, 0, 255, v1);

	// 两个视窗
	//int v1(0), v2(1);
	//viewer->createViewPort(0.0, 0.0, 0.5, 1.0, v1);  // x1[0,0.5], y1[0,1]
	//viewer->createViewPort(0.5, 0.0, 1.0, 1.0, v2);  // x2[0.5,1], y2[0,1]
	//viewer->setBackgroundColor(255, 0, 255, v1);
	//viewer->setBackgroundColor(0, 255, 255, v2);

	// 三个视窗
	//int v1(0), v2(1), v3(2);
	//viewer->createViewPort(0.0, 0.0, 0.33, 1.0, v1);  // x1[0,0.33], y1[0,1]
	//viewer->createViewPort(0.33, 0.0, 0.66, 1.0, v2);  // x2[0.33,0.66], y2[0,1]
	//viewer->createViewPort(0.66, 0.0, 1.0, 1.0, v3);  // x3[0.66,1], y3[0,1]
	//viewer->setBackgroundColor(255, 0, 255, v1);
	//viewer->setBackgroundColor(0, 255, 255, v2);
	//viewer->setBackgroundColor(255, 255, 0, v3);

	// 四个视窗
	int v1(0), v2(1), v3(2), v4(3);
	viewer->createViewPort(0.0, 0.0, 0.5, 0.5, v1);  // x1[0,0.5], y1[0,0.5]
	viewer->createViewPort(0.5, 0.0, 1.0, 0.5, v2);  // x2[0.5,1], y2[0,0.5]
	viewer->createViewPort(0.0, 0.5, 0.5, 1.0, v3);  // x3[0.,0.5], y3[0.5,1]
	viewer->createViewPort(0.5, 0.5, 1.0, 1.0, v4);  // x3[0.5,1], y3[0.5,1]
	viewer->setBackgroundColor(255, 0, 255, v1);
	viewer->setBackgroundColor(0, 255, 255, v2);
	viewer->setBackgroundColor(255, 255, 0, v3);
	viewer->setBackgroundColor(255, 0, 0, v4);
	
	viewer->addCoordinateSystem();  // 在viewer对象下所有的视窗都可视化坐标系
	viewer->spin();  // 显示交互界面
}

效果

一个视窗。 

 两个视窗。

 三个视窗。

 四个视窗。

点云视窗上打印文本信息

使用addText

addText参数说明,注意最后一个参数可以指定在哪个视窗上显示文本。

bool addText (const std::string &text, int xpos, int ypos, int fontsize, double r, double g, double b, const std::string &id = "", int viewport = 0);

view/** \brief Add a text to screen
    * \param[in] text the text to add
    * \param[in] xpos the X position on screen where the text should be added
    * \param[in] ypos the Y position on screen where the text should be added
    * \param[in] fontsize the fontsize of the text
    * \param[in] r the red color value
    * \param[in] g the green color value
    * \param[in] b the blue color value
    * \param[in] id the text object id (default: equal to the "text" parameter)
    * \param[in] viewport the view port (default: all)
    */

pcl::visualization::PCLVisualizer viewer("demo");  // 创建可视化对象
viewer.createViewPort(0.0, 0.0, 0.5, 1.0, 0);  // 定义viewport位置. 可以定义多个视角
viewer.addText("Original point cloud\n", 10, 15, 16, 1, 1, 1, "text_id", 0);

合并多个点云

xyz+xyz

点的类型都是pcl::PointXYZ类型

#include <pcl/point_types.h>
#include <pcl/io/file_io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_cloud.h>
#include <iostream>

int
main(int argc, char** argv)
{
	pcl::PointCloud<pcl::PointXYZ> cloud_a, cloud_b;
	pcl::PointCloud<pcl::PointXYZ> cloud_c;
 
	// 创建点云数据
	cloud_a.width = 3;  // 点数3
	cloud_a.height = cloud_b.height = 1;  // 设置为无序点云
	cloud_a.points.resize(cloud_a.width * cloud_a.height);
 
	cloud_b.width = 2;
	cloud_b.points.resize(cloud_b.width * cloud_b.height);
 
	// 赋值
	for (size_t i = 0; i < cloud_a.points.size(); ++i)
	{
		cloud_a.points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
		cloud_a.points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
		cloud_a.points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
	}
	for (size_t i = 0; i < cloud_b.points.size(); ++i)
	{
		cloud_b.points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
		cloud_b.points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
		cloud_b.points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
	}
	cloud_c = cloud_a;
    cloud_c += cloud_b;
	pcl::io::savePCDFile("test.pcd", cloud_c);
 
}

结果:

# .PCD v0.7 - Point Cloud Data file format
VERSION 0.7
FIELDS x y z
SIZE 4 4 4
TYPE F F F
COUNT 1 1 1
WIDTH 5
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS 5
DATA ascii
1.28125 577.09375 197.9375
828.125 599.03125 491.375
358.6875 917.4375 842.5625
764.5 178.28125 879.53125
727.53125 525.84375 311.28125

xyz + nxnynz


#include <pcl/point_types.h>
#include <pcl/io/file_io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_cloud.h>
#include <pcl/visualization/pcl_visualizer.h>


int
main(int argc, char** argv)
{
	pcl::PointCloud<pcl::PointXYZ> cloud_a;  // 向量a
	pcl::PointCloud<pcl::Normal> n_cloud_b;  // 法向量
	pcl::PointCloud<pcl::PointNormal> p_n_cloud_c;  // xyz + nxnynz = (x,y,z,nx,ny,nz)
 
	// 创建点云数据
	cloud_a.width = 3;  // 点数3
	cloud_a.height = 1;  // 设置为无序点云
	cloud_a.points.resize(cloud_a.width * cloud_a.height);
 
	n_cloud_b.width = 3;
	n_cloud_b.height = 1;
	n_cloud_b.points.resize(n_cloud_b.width * n_cloud_b.height);
 
	// 赋值
	for (size_t i = 0; i < cloud_a.points.size(); ++i)
	{
		cloud_a.points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
		cloud_a.points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
		cloud_a.points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
	}
	for (size_t i = 0; i < n_cloud_b.points.size(); ++i)
	{
		n_cloud_b.points[i].normal[0] = 1024 * rand() / (RAND_MAX + 1.0f);
		n_cloud_b.points[i].normal[1] = 1024 * rand() / (RAND_MAX + 1.0f);
		n_cloud_b.points[i].normal[2] = 1024 * rand() / (RAND_MAX + 1.0f);
	}
 
	pcl::concatenateFields(cloud_a, n_cloud_b, p_n_cloud_c);
	pcl::io::savePCDFile("test.pcd", p_n_cloud_c);
 
}

结果:

# .PCD v0.7 - Point Cloud Data file format
VERSION 0.7
FIELDS x y z normal_x normal_y normal_z curvature
SIZE 4 4 4 4 4 4 4
TYPE F F F F F F F
COUNT 1 1 1 1 1 1 1
WIDTH 3
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS 3
DATA ascii
1.28125 577.09375 197.9375 764.5 178.28125 879.53125 0
828.125 599.03125 491.375 727.53125 525.84375 311.28125 0
358.6875 917.4375 842.5625 15.34375 93.59375 373.1875 0

新建自己的Point类型

步骤:先定义结构体,再注册。

#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/io/pcd_io.h>
 
struct MyPointType  // 定义点类型说明
{
	PCL_ADD_POINT4D;  // 该点类型有4个元素
	float val;  
	EIGEN_MAKE_ALIGNED_OPERATOR_NEW  // 确保new操作符对齐操作
}EIGEN_ALIGN16;  // 强制SSE对齐
 
POINT_CLOUD_REGISTER_POINT_STRUCT(
	MyPointType,  // 注册点类型宏
	(float, x, x)
	(float, y, y)
	(float, z, z)
	(float, val, val)
	)
 
int
main(int argc, char** argv)
{
	// 创建1行2列的点云
	pcl::PointCloud<MyPointType> cloud;
	cloud.points.resize(2);  
	cloud.width = 2;
	cloud.height = 1;
 
	// 对每个点赋值
	cloud.points[0].val = 1;
	cloud.points[0].x = cloud.points[0].y = cloud.points[0].z = 0;
	cloud.points[1].val = 2;
	cloud.points[1].x = cloud.points[1].y = cloud.points[1].z = 3;
 
	pcl::io::savePCDFile("test.pcd", cloud);
}

保存的test.pcd文件内容如下。

# .PCD v0.7 - Point Cloud Data file format
VERSION 0.7
FIELDS x y z val
SIZE 4 4 4 4
TYPE F F F F
COUNT 1 1 1 1
WIDTH 2
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS 2
DATA ascii
0 0 0 1
3 3 3 2

点云的刚体变换(旋转+平移)

#include <pcl/point_types.h>
#include <pcl/io/file_io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_cloud.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/filters/voxel_grid.h>
#include <iostream>
#include <pcl/registration/ndt.h>
#include <pcl/registration/icp.h>

#include <iostream>


int main()
{
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in(new pcl::PointCloud<pcl::PointXYZ>);  // 原始点云
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_rotate(new pcl::PointCloud<pcl::PointXYZ>);  // 只是旋转
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_rotate_shift(new pcl::PointCloud<pcl::PointXYZ>);  // 旋转加平移
	if (pcl::io::loadPCDFile("E:\\code\\c++\\PCL_demo\\PCL_demo\\models\\rabbit.pcd", *cloud_in) < 0)
    {
        PCL_ERROR("Error loading cloud %s.\n");
        return (-1);
    }
    
    // 旋转
    rigid_transformation(cloud_in, cloud_rotate, M_PI / 4, "x", 0, 0, 0);
    rigid_transformation(cloud_in, cloud_rotate_shift, M_PI / 4, "x", 0, 5, 0);

    // 可视化
    boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("Trans viewer"));
    viewer->initCameraParameters();
    int v1(0), v2(1);
    viewer->createViewPort(0.0, 0.0, 0.5, 1.0, v1);
    viewer->createViewPort(0.5, 0.0, 1.0, 1.0, v2);

    viewer->addCoordinateSystem(1);
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_in_color_r(cloud_in, 128, 128, 0);
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_in_color_b(cloud_rotate, 128, 0, 128);
    viewer->addPointCloud(cloud_in, cloud_in_color_r, "orig1", v1);
    viewer->addPointCloud(cloud_rotate, cloud_in_color_b, "trans1", v1);
    viewer->addPointCloud(cloud_in, cloud_in_color_r, "orig2", v2);
    viewer->addPointCloud(cloud_rotate_shift, cloud_in_color_b, "trans2", v2);

    viewer->spin();
}

 

<think>好的,用户想查找关于C++中使用PCL点云库的教程、示例和代码。首先,我需要回忆一下之前提供的引用内容,看看有没有相关资源。引用[1]提到了一个使用C++实现PCL点云可视化的示例程序及解析,这可能是一个很好的起点。引用[2]是2024最新版的PCL算法汇总,适合C++开发者,应该包含大量教程和示例。引用[3]有直通滤波的C++代码示例,这可以直接提供给用户参考。引用[4]虽然主要是点云配准,但里面提到的其他Python项目可能不太相关,不过用户明确需要C++的内容,所以可能可以忽略这部分。 接下来,我需要整理这些资源,分门别类地呈现给用户。用户可能需要从基础开始,比如安装配置、基本示例,然后到常用算法和进阶内容。根据引用[1],可视化是一个重要部分,应该单独列出。引用[3]中的直通滤波属于预处理步骤,可以归类到常用算法里。另外,引用[2]提到的算法汇总可能涵盖更多处理模块,比如滤波、配准、分割等,需要详细说明。 用户可能还需要知道如何获取官方文档和社区资源,比如PCL官网和GitHub仓库,这些在引用中没有提到,但作为常识应该补充进去。此外,书籍推荐也是有用的,比如《点云PCL学习教程》,不过需要确认是否有中文版或最新版本。 关于代码示例,引用[1]和引用[3]提供了具体的代码片段,可以提取关键部分展示,比如可视化示例和直通滤波的实现步骤。同时,需要确保代码格式正确,使用C++语法,并加上注释说明。 用户的问题可能还涉及到实际应用中的问题,比如性能优化、调试技巧,这些在引用中没有直接提到,但可以基于经验给出建议,比如使用并行处理或选择合适的数据结构。 最后,生成相关问题需要考虑用户可能的后续需求,比如特定算法的深入问题、性能优化、可视化技巧,或者与其他库的集成等。这些问题需要与提供的教程和资源相关联,帮助用户进一步探索。</think>以下是为您整理的C++ PCL点云处理开发资源及教程指南,包含关键知识点和代码示例: ### 一、基础开发环境配置 1. PCL 1.13.0+推荐配合VS2022使用 2. 必须安装依赖项:Boost、Eigen、FLANN、VTK 3. 环境变量配置需包含`PCL_ROOT`指向安装目录[^2] ### 二、核心功能模块 | 模块分类 | 包含算法 | 典型应用场景 | |----------------|------------------------------|-----------------------| | 点云滤波 | 直通/统计/体素格/半径滤波 | 去噪/降采样 | | 特征提取 | 法线估计/曲率计算/FPFH | 表面重建/物体识别 | | 配准算法 | ICP/NDT/SAC-IA | 多视角点云对齐 | | 分割算法 | RANSAC/区域生长/欧式聚类 | 物体提取/场景分析 | ### 三、经典代码示例 #### 1. 可视化基础框架(引用[1]实现) ```cpp #include <pcl/visualization/cloud_viewer.h> void simpleVis(pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud) { pcl::visualization::CloudViewer viewer("Simple Cloud Viewer"); viewer.showCloud(cloud); while (!viewer.wasStopped()) {} } int main() { // 创建示例点云 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); cloud->width = 500; cloud->height = 1; cloud->points.resize(cloud->width * cloud->height); // 填充随机点数据 for (auto& point : *cloud) { point.x = 1024 * rand() / (RAND_MAX + 1.0f); point.y = 1024 * rand() / (RAND_MAX + 1.0f); point.z = 1024 * rand() / (RAND_MAX + 1.0f); } simpleVis(cloud); return 0; } ``` #### 2. 直通滤波示例(引用[3]实现) ```cpp #include <pcl/filters/passthrough.h> pcl::PointCloud<pcl::PointXYZ>::Ptr applyPassThrough( const pcl::PointCloud<pcl::PointXYZ>::Ptr& input, const std::string& field, float min, float max) { pcl::PassThrough<pcl::PointXYZ> pass; pass.setInputCloud(input); pass.setFilterFieldName(field); // 设置过滤轴向(x/y/z) pass.setFilterLimits(min, max); // 设置阈值范围 pass.setFilterLimitsNegative(false); // 是否保留范围外数据 pcl::PointCloud<pcl::PointXYZ>::Ptr output(new pcl::PointCloud<pcl::PointXYZ>); pass.filter(*output); return output; } ``` ### 四、学习路径建议 1. **初级阶段**:掌握点云I/O操作与可视化(PCLVisualizer类) 2. **中级阶段**:理解点云特征描述子(PFH/FPFH/VFH) 3. **高级阶段**:研究点云配准算法(ICP及其变种实现) 4. **工程实践**:结合OpenCV实现多传感器数据融合 ### 五、推荐资源 1. 官方文档:[PCL Documentation](https://pointclouds.org/documentation/) 2. GitHub资源库:[PCL官方示例代码](https://github.com/PointCloudLibrary/pcl/tree/master/examples) 3. 书籍:《点云PCL学习教程(第二版)》(机械工业出版社)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Mr.Q

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

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

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

打赏作者

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

抵扣说明:

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

余额充值