小言_互联网的博客

pcl 基本操作汇总

432人阅读  评论(0)

目录

PCLVisualizer简单的点云可视化

createViewPort创建视窗

代码 

效果

点云视窗上打印文本信息

使用addText

合并多个点云

xyz+xyz

xyz + nxnynz

新建自己的Point类型

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


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

PCLVisualizer简单的点云可视化


  
  1. #include <iostream>
  2. #include <pcl\io\pcd_io.h>
  3. #include <pcl\visualization\pcl_visualizer.h>
  4. using namespace std;
  5. int main()
  6. {
  7. // 加载数据
  8. pcl::PointCloud<pcl::PointXYZ>:: Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
  9. pcl::PCDReader reader;
  10. if (reader. read( ".\\models\\horse.pcd", *cloud) < 0)
  11. {
  12. PCL_ERROR( "not exists!\n");
  13. system( "pause");
  14. return -1;
  15. }
  16. // 可视化点云
  17. boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("viewer_window_name"));
  18. viewer-> addPointCloud<pcl::PointXYZ>(cloud, "horse_cloud"); // 将点云附加到视窗中,并对该点云定义唯一的ID“horse_cloud”
  19. while (!viewer-> wasStopped())
  20. {
  21. viewer-> spinOnce( 100); // 100 ms 刷新一次
  22. }
  23. return 0;
  24. }

createViewPort创建视窗

代码 


  
  1. #include <pcl/visualization/cloud_viewer.h>
  2. int main()
  3. {
  4. boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("PCL Viewer"));
  5. viewer-> initCameraParameters(); // 初始化相机默认参数
  6. // 一个视窗
  7. //int v1(0);
  8. //viewer->createViewPort(0.0, 0.0, 1.0, 1.0, v1); // x[0,1], y[0,1]
  9. //viewer->setBackgroundColor(255, 0, 255, v1);
  10. // 两个视窗
  11. //int v1(0), v2(1);
  12. //viewer->createViewPort(0.0, 0.0, 0.5, 1.0, v1); // x1[0,0.5], y1[0,1]
  13. //viewer->createViewPort(0.5, 0.0, 1.0, 1.0, v2); // x2[0.5,1], y2[0,1]
  14. //viewer->setBackgroundColor(255, 0, 255, v1);
  15. //viewer->setBackgroundColor(0, 255, 255, v2);
  16. // 三个视窗
  17. //int v1(0), v2(1), v3(2);
  18. //viewer->createViewPort(0.0, 0.0, 0.33, 1.0, v1); // x1[0,0.33], y1[0,1]
  19. //viewer->createViewPort(0.33, 0.0, 0.66, 1.0, v2); // x2[0.33,0.66], y2[0,1]
  20. //viewer->createViewPort(0.66, 0.0, 1.0, 1.0, v3); // x3[0.66,1], y3[0,1]
  21. //viewer->setBackgroundColor(255, 0, 255, v1);
  22. //viewer->setBackgroundColor(0, 255, 255, v2);
  23. //viewer->setBackgroundColor(255, 255, 0, v3);
  24. // 四个视窗
  25. int v1(0), v2(1), v3(2), v4(3);
  26. viewer-> createViewPort( 0.0, 0.0, 0.5, 0.5, v1); // x1[0,0.5], y1[0,0.5]
  27. viewer-> createViewPort( 0.5, 0.0, 1.0, 0.5, v2); // x2[0.5,1], y2[0,0.5]
  28. viewer-> createViewPort( 0.0, 0.5, 0.5, 1.0, v3); // x3[0.,0.5], y3[0.5,1]
  29. viewer-> createViewPort( 0.5, 0.5, 1.0, 1.0, v4); // x3[0.5,1], y3[0.5,1]
  30. viewer-> setBackgroundColor( 255, 0, 255, v1);
  31. viewer-> setBackgroundColor( 0, 255, 255, v2);
  32. viewer-> setBackgroundColor( 255, 255, 0, v3);
  33. viewer-> setBackgroundColor( 255, 0, 0, v4);
  34. viewer-> addCoordinateSystem(); // 在viewer对象下所有的视窗都可视化坐标系
  35. viewer-> spin(); // 显示交互界面
  36. }

效果

一个视窗。 

 两个视窗。

 三个视窗。

 四个视窗。

点云视窗上打印文本信息

使用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)
    */


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

合并多个点云

xyz+xyz

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


  
  1. #include <pcl/point_types.h>
  2. #include <pcl/io/file_io.h>
  3. #include <pcl/io/pcd_io.h>
  4. #include <pcl/point_cloud.h>
  5. #include <iostream>
  6. int
  7. main (int argc, char** argv)
  8. {
  9. pcl::PointCloud<pcl::PointXYZ> cloud_a, cloud_b;
  10. pcl::PointCloud<pcl::PointXYZ> cloud_c;
  11. // 创建点云数据
  12. cloud_a.width = 3; // 点数3
  13. cloud_a.height = cloud_b.height = 1; // 设置为无序点云
  14. cloud_a.points. resize(cloud_a.width * cloud_a.height);
  15. cloud_b.width = 2;
  16. cloud_b.points. resize(cloud_b.width * cloud_b.height);
  17. // 赋值
  18. for ( size_t i = 0; i < cloud_a.points. size(); ++i)
  19. {
  20. cloud_a.points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
  21. cloud_a.points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
  22. cloud_a.points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
  23. }
  24. for ( size_t i = 0; i < cloud_b.points. size(); ++i)
  25. {
  26. cloud_b.points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
  27. cloud_b.points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
  28. cloud_b.points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
  29. }
  30. cloud_c = cloud_a;
  31. cloud_c += cloud_b;
  32. pcl::io:: savePCDFile( "test.pcd", cloud_c);
  33. }

结果:


  
  1. # .PCD v0 .7 - Point Cloud Data file format
  2. VERSION 0.7
  3. FIELDS x y z
  4. SIZE 4 4 4
  5. TYPE F F F
  6. COUNT 1 1 1
  7. WIDTH 5
  8. HEIGHT 1
  9. VIEWPOINT 0 0 0 1 0 0 0
  10. POINTS 5
  11. DATA ascii
  12. 1.28125 577.09375 197.9375
  13. 828.125 599.03125 491.375
  14. 358.6875 917.4375 842.5625
  15. 764.5 178.28125 879.53125
  16. 727.53125 525.84375 311.28125

xyz + nxnynz


  
  1. #include <pcl/point_types.h>
  2. #include <pcl/io/file_io.h>
  3. #include <pcl/io/pcd_io.h>
  4. #include <pcl/point_cloud.h>
  5. #include <pcl/visualization/pcl_visualizer.h>
  6. int
  7. main (int argc, char** argv)
  8. {
  9. pcl::PointCloud<pcl::PointXYZ> cloud_a; // 向量a
  10. pcl::PointCloud<pcl::Normal> n_cloud_b; // 法向量
  11. pcl::PointCloud<pcl::PointNormal> p_n_cloud_c; // xyz + nxnynz = (x,y,z,nx,ny,nz)
  12. // 创建点云数据
  13. cloud_a.width = 3; // 点数3
  14. cloud_a.height = 1; // 设置为无序点云
  15. cloud_a.points. resize(cloud_a.width * cloud_a.height);
  16. n_cloud_b.width = 3;
  17. n_cloud_b.height = 1;
  18. n_cloud_b.points. resize(n_cloud_b.width * n_cloud_b.height);
  19. // 赋值
  20. for ( size_t i = 0; i < cloud_a.points. size(); ++i)
  21. {
  22. cloud_a.points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
  23. cloud_a.points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
  24. cloud_a.points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
  25. }
  26. for ( size_t i = 0; i < n_cloud_b.points. size(); ++i)
  27. {
  28. n_cloud_b.points[i].normal[ 0] = 1024 * rand() / (RAND_MAX + 1.0f);
  29. n_cloud_b.points[i].normal[ 1] = 1024 * rand() / (RAND_MAX + 1.0f);
  30. n_cloud_b.points[i].normal[ 2] = 1024 * rand() / (RAND_MAX + 1.0f);
  31. }
  32. pcl:: concatenateFields(cloud_a, n_cloud_b, p_n_cloud_c);
  33. pcl::io:: savePCDFile( "test.pcd", p_n_cloud_c);
  34. }

结果:


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

新建自己的Point类型

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


  
  1. #include <pcl/point_types.h>
  2. #include <pcl/point_cloud.h>
  3. #include <pcl/io/pcd_io.h>
  4. struct MyPointType // 定义点类型说明
  5. {
  6. PCL_ADD_POINT4D; // 该点类型有4个元素
  7. float val;
  8. EIGEN_MAKE_ALIGNED_OPERATOR_NEW // 确保new操作符对齐操作
  9. }EIGEN_ALIGN16; // 强制SSE对齐
  10. POINT_CLOUD_REGISTER_POINT_STRUCT(
  11. MyPointType, // 注册点类型宏
  12. ( float, x, x)
  13. ( float, y, y)
  14. ( float, z, z)
  15. ( float, val, val)
  16. )
  17. int
  18. main (int argc, char** argv)
  19. {
  20. // 创建1行2列的点云
  21. pcl::PointCloud<MyPointType> cloud;
  22. cloud.points. resize( 2);
  23. cloud.width = 2;
  24. cloud.height = 1;
  25. // 对每个点赋值
  26. cloud.points[ 0].val = 1;
  27. cloud.points[ 0].x = cloud.points[ 0].y = cloud.points[ 0].z = 0;
  28. cloud.points[ 1].val = 2;
  29. cloud.points[ 1].x = cloud.points[ 1].y = cloud.points[ 1].z = 3;
  30. pcl::io:: savePCDFile( "test.pcd", cloud);
  31. }

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


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

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


  
  1. #include <pcl/point_types.h>
  2. #include <pcl/io/file_io.h>
  3. #include <pcl/io/pcd_io.h>
  4. #include <pcl/point_cloud.h>
  5. #include <pcl/visualization/pcl_visualizer.h>
  6. #include <pcl/kdtree/kdtree_flann.h>
  7. #include <pcl/filters/voxel_grid.h>
  8. #include <iostream>
  9. #include <pcl/registration/ndt.h>
  10. #include <pcl/registration/icp.h>
  11. #include <iostream>
  12. int main()
  13. {
  14. pcl::PointCloud<pcl::PointXYZ>:: Ptr cloud_in(new pcl::PointCloud<pcl::PointXYZ>); // 原始点云
  15. pcl::PointCloud<pcl::PointXYZ>:: Ptr cloud_rotate(new pcl::PointCloud<pcl::PointXYZ>); // 只是旋转
  16. pcl::PointCloud<pcl::PointXYZ>:: Ptr cloud_rotate_shift(new pcl::PointCloud<pcl::PointXYZ>); // 旋转加平移
  17. if (pcl::io:: loadPCDFile( "E:\\code\\c++\\PCL_demo\\PCL_demo\\models\\rabbit.pcd", *cloud_in) < 0)
  18. {
  19. PCL_ERROR( "Error loading cloud %s.\n");
  20. return ( -1);
  21. }
  22. // 旋转
  23. rigid_transformation(cloud_in, cloud_rotate, M_PI / 4, "x", 0, 0, 0);
  24. rigid_transformation(cloud_in, cloud_rotate_shift, M_PI / 4, "x", 0, 5, 0);
  25. // 可视化
  26. boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("Trans viewer"));
  27. viewer-> initCameraParameters();
  28. int v1(0), v2(1);
  29. viewer-> createViewPort( 0.0, 0.0, 0.5, 1.0, v1);
  30. viewer-> createViewPort( 0.5, 0.0, 1.0, 1.0, v2);
  31. viewer-> addCoordinateSystem( 1);
  32. pcl:: visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_in_color_r(cloud_in, 128, 128, 0);
  33. pcl:: visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_in_color_b(cloud_rotate, 128, 0, 128);
  34. viewer-> addPointCloud(cloud_in, cloud_in_color_r, "orig1", v1);
  35. viewer-> addPointCloud(cloud_rotate, cloud_in_color_b, "trans1", v1);
  36. viewer-> addPointCloud(cloud_in, cloud_in_color_r, "orig2", v2);
  37. viewer-> addPointCloud(cloud_rotate_shift, cloud_in_color_b, "trans2", v2);
  38. viewer-> spin();
  39. }

 


转载:https://blog.csdn.net/jizhidexiaoming/article/details/128399020
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场