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