https://blog.csdn.net/qq_33933704/article/details/78652633

在逆向工程,计算机视觉,文物数字化等领域中,由于点云的不完整,旋转错位,平移错位等,使得要得到的完整的点云就需要对局部点云进行配准,为了得到被测物体的完整数据模型,需要确定一个合适的坐标系,将从各个视角得到的点集合并到统一的坐标系下形成一个完整的点云,然后就可以方便进行可视化的操作,这就是点云数据的配准。点云的配准有手动配准依赖仪器的配准,和自动配准,点云的自动配准技术是通过一定的算法或者统计学规律利用计算机计算两块点云之间错位,从而达到两块点云自动配准的效果,其实质就是把不同的坐标系中测得到的数据点云进行坐标系的变换,以得到整体的数据模型,问题的关键是如何让得到坐标变换的参数R(旋转矩阵)和T(平移向量),使得两视角下测得的三维数据经坐标变换后的距离最小,,目前配准算法按照过程可以分为整体配准和局部配准,。PCL中有单独的配准模块,实现了配准相关的基础数据结构,和经典的配准算法如ICP。

     ICP算法对待拼接的2片点云,首先根据一定的准则确立对应点集P与Q,其中对应点对的个数,然后通过最小乘法迭代计算最优的坐标变换,即旋转矩阵R和平移矢量t,使得误差函数最小,ICP处理流程分为四个主要的步骤:

     1. 对原始点云数据进行采样
     2.确定初始对应点集
     3.去除错误对应点对

     4.坐标变换求解

     以下代码读取两个pcd点云配准,用空格控制迭代次数,并用visualization可视化出来,其中白色用来显示原始点云数据,经过一定旋转平移的点云显示为绿色,红色用来显示icp匹配后的点云数据。每次迭代都会打出此时的选择矩阵和平移向量。

 

 

 
  1. #include<iostream>

  2. #include<pcl/io/pcd_io.h>

  3. #include<pcl/point_types.h>

  4. #include<pcl/registration/icp.h>

  5. #include <pcl/visualization/pcl_visualizer.h>//可视化头文件

  6.  
  7. typedef pcl::PointXYZ PointT;

  8. typedef pcl::PointCloud<PointT> PointCloudT;

  9.  
  10. bool next_iteration = false;

  11.  
  12. void print4x4Matrix(const Eigen::Matrix4d & matrix) //打印旋转矩阵和平移矩阵

  13. {

  14. printf("Rotation matrix :\n");

  15. printf(" | %6.3f %6.3f %6.3f | \n", matrix(0, 0), matrix(0, 1), matrix(0, 2));

  16. printf("R = | %6.3f %6.3f %6.3f | \n", matrix(1, 0), matrix(1, 1), matrix(1, 2));

  17. printf(" | %6.3f %6.3f %6.3f | \n", matrix(2, 0), matrix(2, 1), matrix(2, 2));

  18. printf("Translation vector :\n");

  19. printf("t = < %6.3f, %6.3f, %6.3f >\n\n", matrix(0, 3), matrix(1, 3), matrix(2, 3));

  20. }

  21.  
  22. void KeyboardEventOccurred(const pcl::visualization::KeyboardEvent& event,void* nothing)

  23. { //使用空格键来增加迭代次数,并更新显示

  24. if (event.getKeySym() == "space" && event.keyDown())

  25. next_iteration = true;

  26. }

  27.  
  28.  
  29. int main(int argc, char** argv)

  30. {

  31. // 创建点云指针

  32. PointCloudT::Ptr cloud_in(new PointCloudT); // 原始点云

  33. PointCloudT::Ptr cloud_tr(new PointCloudT); // 转换后的点云

  34. PointCloudT::Ptr cloud_icp(new PointCloudT); // ICP 输出点云

  35.  
  36. //读取pcd文件

  37. if (pcl::io::loadPCDFile<pcl::PointXYZ>("you.pcd", *cloud_in) == -1)

  38. {

  39. PCL_ERROR("Couldn't read file1 \n");

  40. return (-1);

  41. }

  42. std::cout << "Loaded " << cloud_in->size() << " data points from file1" << std::endl;

  43.  
  44. if (pcl::io::loadPCDFile<pcl::PointXYZ>("zuo.pcd", *cloud_icp) == -1)

  45. {

  46. PCL_ERROR("Couldn't read file2 \n");

  47. return (-1);

  48. }

  49. std::cout << "Loaded " << cloud_icp->size() << " data points from file2" << std::endl;

  50.  
  51.  
  52. int iterations = 1; // 默认的ICP迭代次数

  53.  
  54.  
  55. //定义旋转矩阵和平移向量Matrix4d是为4*4的矩阵

  56. Eigen::Matrix4d transformation_matrix = Eigen::Matrix4d::Identity();

  57.  
  58.  
  59. //初始化

  60. double theta = M_PI / 8; // 旋转的角度用弧度的表示方法

  61. transformation_matrix(0, 0) = cos(theta);

  62. transformation_matrix(0, 1) = -sin(theta);

  63. transformation_matrix(1, 0) = sin(theta);

  64. transformation_matrix(1, 1) = cos(theta);

  65.  
  66. // Z轴的平移向量 (0.4 meters)

  67. transformation_matrix(2, 3) = 0.4;

  68.  
  69. //打印转换矩阵

  70. std::cout << "Applying this rigid transformation to: cloud_in -> cloud_icp" << std::endl;

  71. print4x4Matrix(transformation_matrix);

  72.  
  73. // 执行点云转换

  74. pcl::transformPointCloud(*cloud_in, *cloud_icp, transformation_matrix);

  75. *cloud_tr = *cloud_icp; // 备份cloud_icp赋值给cloud_tr为后期使用

  76.  
  77. //icp配准

  78. pcl::IterativeClosestPoint<pcl::PointXYZ, pcl::PointXYZ> icp; //创建ICP对象,用于ICP配准

  79. icp.setMaximumIterations(iterations); //设置最大迭代次数iterations=true

  80. icp.setInputCloud(cloud_icp); //设置输入点云

  81. icp.setInputTarget(cloud_in); //设置目标点云(输入点云进行仿射变换,得到目标点云)

  82. icp.align(*cloud_icp); //匹配后源点云

  83. icp.setMaximumIterations(1); // 设置为1以便下次调用

  84. std::cout << "Applied " << iterations << " ICP iteration(s)"<< std::endl;

  85. if (icp.hasConverged())//icp.hasConverged ()=1(true)输出变换矩阵的适合性评估

  86. {

  87. std::cout << "\nICP has converged, score is " << icp.getFitnessScore() << std::endl;

  88. std::cout << "\nICP transformation " << iterations << " : cloud_icp -> cloud_in" << std::endl;

  89. transformation_matrix = icp.getFinalTransformation().cast<double>();

  90. print4x4Matrix(transformation_matrix);

  91. }

  92. else

  93. {

  94. PCL_ERROR("\nICP has not converged.\n");

  95. return (-1);

  96. }

  97.  
  98.  
  99. //可视化

  100. pcl::visualization::PCLVisualizer viewer("ICP demo");

  101. // 创建两个观察视点

  102. int v1(0);

  103. int v2(1);

  104. viewer.createViewPort(0.0, 0.0, 0.5, 1.0, v1);

  105. viewer.createViewPort(0.5, 0.0, 1.0, 1.0, v2);

  106. // 定义显示的颜色信息

  107. float bckgr_gray_level = 0.0; // Black

  108. float txt_gray_lvl = 1.0 - bckgr_gray_level;

  109. // 原始的点云设置为白色的

  110. pcl::visualization::PointCloudColorHandlerCustom<PointT> cloud_in_color_h(cloud_in, (int)255 * txt_gray_lvl, (int)255 * txt_gray_lvl,

  111. (int)255 * txt_gray_lvl);

  112.  
  113. viewer.addPointCloud(cloud_in, cloud_in_color_h, "cloud_in_v1", v1);//设置原始的点云都是显示为白色

  114. viewer.addPointCloud(cloud_in, cloud_in_color_h, "cloud_in_v2", v2);

  115.  
  116. // 转换后的点云显示为绿色

  117. pcl::visualization::PointCloudColorHandlerCustom<PointT> cloud_tr_color_h(cloud_tr, 20, 180, 20);

  118. viewer.addPointCloud(cloud_tr, cloud_tr_color_h, "cloud_tr_v1", v1);

  119.  
  120. // ICP配准后的点云为红色

  121. pcl::visualization::PointCloudColorHandlerCustom<PointT> cloud_icp_color_h(cloud_icp, 180, 20, 20);

  122. viewer.addPointCloud(cloud_icp, cloud_icp_color_h, "cloud_icp_v2", v2);

  123.  
  124. // 加入文本的描述在各自的视口界面

  125. //在指定视口viewport=v1添加字符串“white 。。。”其中"icp_info_1"是添加字符串的ID标志,(10,15)为坐标16为字符大小 后面分别是RGB值

  126. viewer.addText("White: Original point cloud\nGreen: Matrix transformed point cloud", 10, 15, 16, txt_gray_lvl, txt_gray_lvl, txt_gray_lvl, "icp_info_1", v1);

  127. viewer.addText("White: Original point cloud\nRed: ICP aligned point cloud", 10, 15, 16, txt_gray_lvl, txt_gray_lvl, txt_gray_lvl, "icp_info_2", v2);

  128.  
  129. std::stringstream ss;

  130. ss << iterations; //输入的迭代的次数

  131. std::string iterations_cnt = "ICP iterations = " + ss.str();

  132. viewer.addText(iterations_cnt, 10, 60, 16, txt_gray_lvl, txt_gray_lvl, txt_gray_lvl, "iterations_cnt", v2);

  133.  
  134. // 设置背景颜色

  135. viewer.setBackgroundColor(bckgr_gray_level, bckgr_gray_level, bckgr_gray_level, v1);

  136. viewer.setBackgroundColor(bckgr_gray_level, bckgr_gray_level, bckgr_gray_level, v2);

  137.  
  138. // 设置相机的坐标和方向

  139. viewer.setCameraPosition(-3.68332, 2.94092, 5.71266, 0.289847, 0.921947, -0.256907, 0);

  140. viewer.setSize(1280, 1024); // 可视化窗口的大小

  141.  
  142. // 注册按键回调函数

  143. viewer.registerKeyboardCallback(&KeyboardEventOccurred, (void*)NULL);

  144.  
  145. //显示

  146. while (!viewer.wasStopped())

  147. {

  148. viewer.spinOnce();

  149.  
  150. //按下空格键的函数

  151. if (next_iteration)

  152. {

  153. // 最近点迭代算法

  154. icp.align(*cloud_icp);

  155. if (icp.hasConverged())

  156. {

  157. printf("\033[11A"); // Go up 11 lines in terminal output.

  158. printf("\nICP has converged, score is %+.0e\n", icp.getFitnessScore());

  159. std::cout << "\nICP transformation " << ++iterations << " : cloud_icp -> cloud_in" << std::endl;

  160. transformation_matrix *= icp.getFinalTransformation().cast<double>(); // WARNING /!\ This is not accurate!

  161. print4x4Matrix(transformation_matrix); // 打印矩阵变换

  162.  
  163. ss.str("");

  164. ss << iterations;

  165. std::string iterations_cnt = "ICP iterations = " + ss.str();

  166. viewer.updateText(iterations_cnt, 10, 60, 16, txt_gray_lvl, txt_gray_lvl, txt_gray_lvl, "iterations_cnt");

  167. viewer.updatePointCloud(cloud_icp, cloud_icp_color_h, "cloud_icp_v2");

  168. }

  169. else

  170. {

  171. PCL_ERROR("\nICP has not converged.\n");

  172. return (-1);

  173. }

  174. }

  175. next_iteration = false;

  176. }

  177.  
  178.  
  179. return 0;

  180. }


 

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐