当前位置:   article > 正文

点云配准 4-icp 如何逐步匹配多幅点云(win10)_多点云地图配准

多点云地图配准

一、声明

本人作为初学者,才开始接触点云配准这一块,如有错误地方,望大家指出,我将及时修改,共同进步。

二、实验

本例迭代最近点算法的使用,以便逐步地对一系列点云进行两两匹配。它的思想是对所有的点云进行变换,使得都与第一个点云在统一坐标系中。在每个连贯的有重叠的点云之间找到最佳的变换,并累积这些变换到全部的点云。能够进行ICP算法的点云需要粗略的预匹配(如:在一个机器人的量距内或在地图框架内),并且一个点云与另一个点云需要有重叠部分。

首先看一下,在变换之前的数据分布形状,仔细一看,还是存在一定的差异:

数据集链接:链接:https://pan.baidu.com/s/13Y7Za1D8NHrzhsEG5sOdAQ 
提取码:l8fw

 由于,实在win10下,进行了操作,所以,需要提前将项目属性->调式->命令参数中添加如下内容(既不用修改代码),然后点击确认,即可:

 运行代码(左边是没配准之前的图,右边是配准之后的图):

 按下q之后,开始匹配第一幅图像:

 配准结束:

 cmd显示

 再次按下q(可以看到第二幅图像和第三幅之间以及第二幅和第一幅之间点云分布不同):

 第二幅和第三幅配准后:

 然后依次下去,第三幅和第四幅图,配准:

第四幅和第五幅图,配准:

第五附图和第六幅图: 

 在工程文件下,会生成

 然后利用,命令窗口显示:

pcl_viewer -multiview 1.pcd 2.pcd 3.pcd 4.pcd

 三、代码(代码无需修改):

主函数检查用户输入,在点云矢量data中加载了用户输人的所有点云数据,建立两个视口的可视化对象,左边显示未配准的源和目标点云,右边显示配准后的源和目标点云。在为一对点云找到变换矩阵后,将目标点云变换到源点云坐标系下,并将源点云与变换后的目标点云存储到一点云文件,同时用此次找到的变换矩阵更新全局变换,用于将后续的点云都变换到与第一个输入点云同一坐标系下。

进行实际匹配,由子函数 pairAlign具体实现,其中参数有输入一组需要配准的点云,以及是否进行下采样的设置项,其他参数输出配准后的点云及变换矩阵。

  1. /*
  2. * @Description: 如何逐步匹配多幅点云 https://www.cnblogs.com/li-yao7758258/p/6489585.html
  3. * 编译执行: ./pairwise_incremental_registration ../pairwise/frame_00000.pcd ../pairwise/capture0001.pcd ../pairwise/capture0002.pcd ../pairwise/capture0003.pcd ../pairwise/capture0004.pcd ../pairwise/capture0005.pcd
  4. * @Author: HCQ
  5. * @Company(School): UCAS
  6. * @Email: 1756260160@qq.com
  7. * @Date: 2020-10-22 17:20:55
  8. * @LastEditTime: 2020-10-22 18:23:55
  9. * @FilePath: /pcl-learning/14registration配准/2如何逐步匹配多幅点云/pairwise_incremental_registration.cpp
  10. */
  11. #include <boost/make_shared.hpp> //boost指针相关头文件
  12. #include <pcl/point_types.h> //点类型定义头文件
  13. #include <pcl/point_cloud.h> //点云类定义头文件
  14. #include <pcl/point_representation.h> //点表示相关的头文件
  15. #include <pcl/io/pcd_io.h> //PCD文件打开存储类头文件
  16. #include <pcl/io/ply_io.h> //ply文件打开存储类头文件
  17. #include <pcl/filters/voxel_grid.h> //用于体素网格化的滤波类头文件
  18. #include <pcl/filters/filter.h> //滤波相关头文件
  19. #include <pcl/features/normal_3d.h> //法线特征头文件
  20. #include <pcl/registration/icp.h>
  21. //ICP类相关头文件
  22. #include <pcl/registration/icp_nl.h> //非线性ICP 相关头文件
  23. #include <pcl/registration/transforms.h> //变换矩阵类头文件
  24. #include <pcl/visualization/pcl_visualizer.h> //可视化类头文件
  25. using pcl::visualization::PointCloudColorHandlerCustom;
  26. using pcl::visualization::PointCloudColorHandlerGenericField;
  27. //定义
  28. typedef pcl::PointXYZ PointT;
  29. typedef pcl::PointCloud<PointT> PointCloud; //申明pcl::PointXYZ数据
  30. typedef pcl::PointNormal PointNormalT;
  31. typedef pcl::PointCloud<PointNormalT> PointCloudWithNormals;
  32. // 申明一个全局可视化对象变量,定义左右视点分别显示配准前和配准后的结果点云
  33. pcl::visualization::PCLVisualizer* p; //创建可视化对象
  34. int vp_1, vp_2; //定义存储 左 右视点的ID
  35. //申明一个结构体方便对点云以文件名和点云对象进行成对处理和管理点云,处理过程中可以同时接受多个点云文件的输入
  36. struct PCD
  37. {
  38. PointCloud::Ptr cloud; //点云共享指针
  39. std::string f_name; //文件名称
  40. PCD() : cloud(new PointCloud) {};
  41. };
  42. struct PCDComparator //文件比较处理
  43. {
  44. bool operator()(const PCD& p1, const PCD& p2)
  45. {
  46. return (p1.f_name < p2.f_name);
  47. }
  48. };
  49. // 以< x, y, z, curvature >形式定义一个新的点表示
  50. class MyPointRepresentation : public pcl::PointRepresentation<PointNormalT>
  51. {
  52. using pcl::PointRepresentation<PointNormalT>::nr_dimensions_;
  53. public:
  54. MyPointRepresentation()
  55. {
  56. nr_dimensions_ = 4; //定义点的维度
  57. }
  58. // 重载copyToFloatArray方法将点转化为四维数组
  59. virtual void copyToFloatArray(const PointNormalT& p, float* out) const
  60. {
  61. // < x, y, z, curvature >
  62. out[0] = p.x;
  63. out[1] = p.y;
  64. out[2] = p.z;
  65. out[3] = p.curvature; // 曲率
  66. }
  67. };
  68. /** \左视图用来显示未匹配的源和目标点云*/
  69. void showCloudsLeft(const PointCloud::Ptr cloud_target, const PointCloud::Ptr cloud_source)
  70. {
  71. p->removePointCloud("vp1_target");
  72. p->removePointCloud("vp1_source");
  73. PointCloudColorHandlerCustom<PointT> tgt_h(cloud_target, 0, 255, 0);
  74. PointCloudColorHandlerCustom<PointT> src_h(cloud_source, 255, 0, 0);
  75. p->addPointCloud(cloud_target, tgt_h, "vp1_target", vp_1);
  76. p->addPointCloud(cloud_source, src_h, "vp1_source", vp_1);
  77. PCL_INFO("Press q to begin the registration.\n");
  78. p->spin();
  79. }
  80. /** \右边显示配准后的源和目标点云*/
  81. void showCloudsRight(const PointCloudWithNormals::Ptr cloud_target, const PointCloudWithNormals::Ptr cloud_source)
  82. {
  83. p->removePointCloud("source");
  84. p->removePointCloud("target");
  85. PointCloudColorHandlerGenericField<PointNormalT> tgt_color_handler(cloud_target, "curvature");
  86. if (!tgt_color_handler.isCapable())
  87. PCL_WARN("Cannot create curvature color handler!");
  88. PointCloudColorHandlerGenericField<PointNormalT> src_color_handler(cloud_source, "curvature");
  89. if (!src_color_handler.isCapable())
  90. PCL_WARN("Cannot create curvature color handler!");
  91. p->addPointCloud(cloud_target, tgt_color_handler, "target", vp_2);
  92. p->addPointCloud(cloud_source, src_color_handler, "source", vp_2);
  93. p->spinOnce();
  94. }
  95. /** \brief Load a set of PCD files that we want to register together
  96. * \param argc the number of arguments (pass from main ())
  97. * \param argv the actual command line arguments (pass from main ())
  98. * \param models the resultant vector of point cloud datasets
  99. */
  100. void loadData(int argc, char** argv, std::vector<PCD, Eigen::aligned_allocator<PCD>>& models)
  101. {
  102. std::string extension(".pcd");
  103. // 第一个参数是命令本身,所以要从第二个参数开始解析
  104. for (int i = 1; i < argc; i++)
  105. {
  106. std::string fname = std::string(argv[i]);
  107. // PCD文件名至少为5个字符大小字符串(因为后缀名.pcd就已经占了四个字符位置)
  108. if (fname.size() <= extension.size())
  109. continue;
  110. std::transform(fname.begin(), fname.end(), fname.begin(), (int (*)(int))tolower);
  111. //检查参数是否为一个pcd后缀的文件
  112. if (fname.compare(fname.size() - extension.size(), extension.size(), extension) == 0)
  113. {
  114. //加载点云并保存在总体的点云列表中
  115. PCD m;
  116. m.f_name = argv[i];
  117. pcl::io::loadPCDFile(argv[i], *m.cloud);
  118. //从点云中移除NAN点也就是无效点
  119. std::vector<int> indices;
  120. pcl::removeNaNFromPointCloud(*m.cloud, *m.cloud, indices);
  121. models.push_back(m);
  122. }
  123. }
  124. }
  125. /** \brief Align a pair of PointCloud datasets and return the result
  126. * \param cloud_src the source PointCloud
  127. * \param cloud_tgt the target PointCloud
  128. * \param output the resultant aligned source PointCloud
  129. * \param final_transform the resultant transform between source and target
  130. */
  131. ///实现匹配,其中参数有输入一组需要配准的点云,以及是否需要进行下采样,其他参数输出配准后的点云以及变换矩阵
  132. void pairAlign(const PointCloud::Ptr cloud_src, const PointCloud::Ptr cloud_tgt, PointCloud::Ptr output, Eigen::Matrix4f& final_transform, bool downsample = false)
  133. {
  134. // Downsample for consistency and speed
  135. // \note enable this for large datasets
  136. PointCloud::Ptr src(new PointCloud); //存储滤波后的源点云
  137. PointCloud::Ptr tgt(new PointCloud); //存储滤波后的目标点云
  138. pcl::VoxelGrid<PointT> grid; //滤波处理对象
  139. if (downsample)
  140. {
  141. grid.setLeafSize(0.05, 0.05, 0.05); //设置滤波时采用的体素大小
  142. grid.setInputCloud(cloud_src);
  143. grid.filter(*src);
  144. grid.setInputCloud(cloud_tgt);
  145. grid.filter(*tgt);
  146. }
  147. else
  148. {
  149. src = cloud_src;
  150. tgt = cloud_tgt;
  151. }
  152. // 计算表面的法向量和曲率
  153. PointCloudWithNormals::Ptr points_with_normals_src(new PointCloudWithNormals);
  154. PointCloudWithNormals::Ptr points_with_normals_tgt(new PointCloudWithNormals);
  155. pcl::NormalEstimation<PointT, PointNormalT> norm_est; //点云法线估计对象
  156. pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>());
  157. norm_est.setSearchMethod(tree);
  158. norm_est.setKSearch(30);
  159. norm_est.setInputCloud(src);
  160. norm_est.compute(*points_with_normals_src);
  161. pcl::copyPointCloud(*src, *points_with_normals_src);
  162. norm_est.setInputCloud(tgt);
  163. norm_est.compute(*points_with_normals_tgt);
  164. pcl::copyPointCloud(*tgt, *points_with_normals_tgt);
  165. //
  166. // Instantiate our custom point representation (defined above) ...
  167. MyPointRepresentation point_representation;
  168. // ... and weight the 'curvature' dimension so that it is balanced against x, y, and z
  169. float alpha[4] = { 1.0, 1.0, 1.0, 1.0 };
  170. point_representation.setRescaleValues(alpha);
  171. //
  172. // 配准
  173. pcl::IterativeClosestPointNonLinear<PointNormalT, PointNormalT> reg; // 配准对象
  174. reg.setTransformationEpsilon(1e-6);
  175. //设置收敛判断条件,越小精度越大,收敛也越慢
  176. // Set the maximum distance between two correspondences (src<->tgt) to 10cm大于此值的点对不考虑
  177. // Note: adjust this based on the size of your datasets
  178. reg.setMaxCorrespondenceDistance(0.1);
  179. // 设置点表示
  180. reg.setPointRepresentation(pcl::make_shared<const MyPointRepresentation>(point_representation));
  181. reg.setInputSource(points_with_normals_src); // 设置源点云
  182. reg.setInputTarget(points_with_normals_tgt); // 设置目标点云
  183. //
  184. // Run the same optimization in a loop and visualize the results
  185. Eigen::Matrix4f Ti = Eigen::Matrix4f::Identity(), prev, targetToSource;
  186. PointCloudWithNormals::Ptr reg_result = points_with_normals_src;
  187. reg.setMaximumIterations(2); 设置最大的迭代次数,即每迭代两次就认为收敛,停止内部迭代
  188. std::cout << "下来了" << endl;
  189. for (int i = 0; i < 3000; ++i) //手动迭代,每手动迭代一次,在配准结果视口对迭代的最新结果进行刷新显示
  190. {
  191. PCL_INFO("Iteration Nr. %d.\n", i);
  192. // 存储点云以便可视化
  193. points_with_normals_src = reg_result;
  194. // Estimate
  195. reg.setInputSource(points_with_normals_src);
  196. reg.align(*reg_result);
  197. //accumulate transformation between each Iteration
  198. Ti = reg.getFinalTransformation() * Ti;
  199. //if the difference between this transformation and the previous one
  200. //is smaller than the threshold, refine the process by reducing
  201. //the maximal correspondence distance
  202. if (fabs((reg.getLastIncrementalTransformation() - prev).sum()) < reg.getTransformationEpsilon())
  203. reg.setMaxCorrespondenceDistance(reg.getMaxCorrespondenceDistance() - 0.001);
  204. prev = reg.getLastIncrementalTransformation();
  205. // visualize current state
  206. showCloudsRight(points_with_normals_tgt, points_with_normals_src);
  207. }
  208. //
  209. // Get the transformation from target to source
  210. targetToSource = Ti.inverse(); //deidao
  211. //
  212. // Transform target back in source frame
  213. pcl::transformPointCloud(*cloud_tgt, *output, targetToSource);
  214. p->removePointCloud("source");
  215. p->removePointCloud("target");
  216. PointCloudColorHandlerCustom<PointT> cloud_tgt_h(output, 0, 255, 0);
  217. PointCloudColorHandlerCustom<PointT> cloud_src_h(cloud_src, 255, 0, 0);
  218. p->addPointCloud(output, cloud_tgt_h, "target", vp_2);
  219. p->addPointCloud(cloud_src, cloud_src_h, "source", vp_2);
  220. PCL_INFO("Press q to continue the registration.\n");
  221. p->spin();
  222. p->removePointCloud("source");
  223. p->removePointCloud("target");
  224. //add the source to the transformed target
  225. *output += *cloud_src;
  226. final_transform = targetToSource;
  227. }
  228. int main(int argc, char** argv)
  229. {
  230. // 存储管理所有打开的点云
  231. std::vector<PCD, Eigen::aligned_allocator<PCD>> data;
  232. loadData(argc, argv, data); // 加载所有点云到data
  233. // 检查输入
  234. if (data.empty())
  235. {
  236. PCL_ERROR("Syntax is: %s <source.pcd> <target.pcd> [*]", argv[0]);
  237. PCL_ERROR("[*] - multiple files can be added. The registration results of (i, i+1) will be registered against (i+2), etc");
  238. return (-1);
  239. }
  240. PCL_INFO("Loaded %d datasets.", (int)data.size());
  241. // 创建PCL可视化对象
  242. p = new pcl::visualization::PCLVisualizer(argc, argv, "Pairwise Incremental Registration example");
  243. p->createViewPort(0.0, 0, 0.5, 1.0, vp_1); //用左半窗口创建视口vp_1
  244. p->createViewPort(0.5, 0, 1.0, 1.0, vp_2); //用右半窗口创建视口vp_2
  245. PointCloud::Ptr result(new PointCloud), source, target;
  246. Eigen::Matrix4f GlobalTransform = Eigen::Matrix4f::Identity(), pairTransform;
  247. for (size_t i = 1; i < data.size(); ++i) //循环处理所有点云================重点======================================
  248. {
  249. source = data[i - 1].cloud; //连续配准
  250. target = data[i].cloud; // 相邻两组点云
  251. showCloudsLeft(source, target); //可视化为配准的源和目标点云
  252. //调用子函数完成一组点云的配准,temp返回配准后两组点云在第一组点云坐标下的点云
  253. PointCloud::Ptr temp(new PointCloud);
  254. PCL_INFO("Aligning %s (%d) with %s (%d).\n", data[i - 1].f_name.c_str(), source->points.size(), data[i].f_name.c_str(), target->points.size());
  255. // pairTransform返回从目标点云target到source的变换矩阵
  256. pairAlign(source, target, temp, pairTransform, true); // ===================重点=========================
  257. //把当前两两配准后的点云temp转化到全局坐标系下返回result
  258. pcl::transformPointCloud(*temp, *result, GlobalTransform);
  259. //用当前的两组点云之间的变换更新全局变换
  260. GlobalTransform = GlobalTransform * pairTransform;
  261. //保存转换到第一个点云坐标下的当前配准后的两组点云result到文件i.pcd
  262. std::stringstream ss;
  263. ss << i << ".pcd";
  264. pcl::io::savePCDFile(ss.str(), *result, true);
  265. }
  266. }
  267. /* ]--- */

四、注意事项(关于q):

 你有可能发现在命令窗口怎么按q程序都不往下运行,那么这需要你在生成显示图右边侧,右击一下,按q即可。

 如图所示:

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
  

闽ICP备14008679号