博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PCL显示点云-ICP(PCL1.9.1)
阅读量:2050 次
发布时间:2019-04-28

本文共 9071 字,大约阅读时间需要 30 分钟。

参考:

//https://www.cnblogs.com/ghjnwk/p/10305796.html//鼠标左键,鼠标右键,鼠标滚轮,按住滚轮不放
cmake_minimum_required(VERSION 3.5.1)project(PCL_demo1)set(CMAKE_CXX_STANDARD 14)#find_package(PCL CONFIG REQUIRED)set(PCL_DIR "/media/spple/新加卷/Dataset/pcl-master/install/share/pcl-1.9/")find_package(PCL REQUIRED)include_directories(${PCL_INCLUDE_DIRS})link_directories(${PCL_LIBRARY_DIRS})add_definitions(${PCL_DEFINITIONS})list(REMOVE_ITEM PCL_LIBRARIES "vtkproj4")add_executable(PCL_demo1 interactive_icp.cpp)target_link_libraries (PCL_demo1 ${PCL_LIBRARIES})#-DCMAKE_TOOLCHAIN_FILE=/home/spple/paddle/vcpkg/scripts/buildsystems/vcpkg.cmake
#include 
#include
#include
#include
#include
#include
#include
// TicToc#include
//PointXYZRGBNormaltypedef pcl::PointXYZRGBNormal PointT;typedef pcl::PointCloud
PointCloudT;bool next_iteration = false;voidprint4x4Matrix (const Eigen::Matrix4d & matrix){ printf ("Rotation matrix :\n"); printf (" | %6.3f %6.3f %6.3f | \n", matrix (0, 0), matrix (0, 1), matrix (0, 2)); printf ("R = | %6.3f %6.3f %6.3f | \n", matrix (1, 0), matrix (1, 1), matrix (1, 2)); printf (" | %6.3f %6.3f %6.3f | \n", matrix (2, 0), matrix (2, 1), matrix (2, 2)); printf ("Translation vector :\n"); printf ("t = < %6.3f, %6.3f, %6.3f >\n\n", matrix (0, 3), matrix (1, 3), matrix (2, 3));}voidkeyboardEventOccurred (const pcl::visualization::KeyboardEvent& event, void* nothing){ if (event.getKeySym () == "space" && event.keyDown ()) next_iteration = true;}// Downsamplevoid voxel_grid_filter(PointCloudT::Ptr cloud_in, float leafsize){ pcl::VoxelGrid
sor; sor.setInputCloud(cloud_in); sor.setLeafSize(leafsize, leafsize, leafsize); sor.filter(*cloud_in); // std::cout << "after filter has " << cloud_in->points.size () << " points\n";}intmain (int argc, char* argv[]){ // The point clouds we will be using PointCloudT::Ptr cloud_in (new PointCloudT); // Original point cloud PointCloudT::Ptr cloud_in_48 (new PointCloudT); // Original point cloud PointCloudT::Ptr cloud_tr (new PointCloudT); // Transformed point cloud PointCloudT::Ptr cloud_icp (new PointCloudT); // ICP output point cloud float leafsize = 2; int iterations = 1; // Default number of ICP iterations if (argc > 4) { PCL_ERROR ("Error.\n"); return (-1); } pcl::console::TicToc time; time.tic (); if (pcl::io::loadPLYFile (argv[1], *cloud_in) < 0) { PCL_ERROR ("Error loading cloud %s.\n", argv[1]); return (-1); } std::cout << "\nLoaded file " << argv[1] << " (" << cloud_in->size () << " points) in " << time.toc () << " ms\n" << std::endl; time.tic (); if (pcl::io::loadPLYFile (argv[2], *cloud_in_48) < 0) { PCL_ERROR ("Error loading cloud %s.\n", argv[1]); return (-1); } std::cout << "\nLoaded file " << argv[2] << " (" << cloud_in_48->size () << " points) in " << time.toc () << " ms\n" << std::endl; if (leafsize > 0) { voxel_grid_filter(cloud_in, leafsize); voxel_grid_filter(cloud_in_48, leafsize); } time.tic (); std::cout << "\nLoaded file " << argv[2] << " (" << cloud_in_48->size () << " points) in " << time.toc () << " ms\n" << std::endl; float numMatrix[12] = {0}; ifstream in(argv[3]); char ch[200]; for(int i=0;i<12;i++) { in.getline(ch,200,'\n'); numMatrix[i] = atof(ch); //std::cout << numMatrix[i]; } // Defining a rotation matrix and translation vector Eigen::Matrix4d transformation_matrix = Eigen::Matrix4d::Identity (); // A rotation matrix (see https://en.wikipedia.org/wiki/Rotation_matrix) transformation_matrix (0, 0) = numMatrix[0]; transformation_matrix (0, 1) = numMatrix[1]; transformation_matrix (0, 2) = numMatrix[2]; transformation_matrix (1, 0) = numMatrix[3]; transformation_matrix (1, 1) = numMatrix[4]; transformation_matrix (1, 2) = numMatrix[5]; transformation_matrix (2, 0) = numMatrix[6]; transformation_matrix (2, 1) = numMatrix[7]; transformation_matrix (2, 2) = numMatrix[8]; transformation_matrix (0, 3) = numMatrix[9]; transformation_matrix (1, 3) = numMatrix[10]; transformation_matrix (2, 3) = numMatrix[11]; // Display in terminal the transformation matrix std::cout << "Applying this rigid transformation to: cloud_in -> cloud_icp" << std::endl; print4x4Matrix (transformation_matrix); //把48(cloud_in_48)通过初始Rt矩阵进行变换,变换后的48‘(cloud_in)为原点云,去匹配目标点云52(cloud_in) // Executing the transformation pcl::transformPointCloud (*cloud_in_48, *cloud_icp, transformation_matrix); *cloud_tr = *cloud_icp; // We backup cloud_icp into cloud_tr for later use // The Iterative Closest Point algorithm time.tic (); pcl::IterativeClosestPointWithNormals
icp; icp.setMaximumIterations (iterations); icp.setMaxCorrespondenceDistance(5); icp.setInputSource (cloud_icp); icp.setInputTarget (cloud_in); icp.align (*cloud_icp); icp.setMaximumIterations (1); // We set this variable to 1 for the next time we will call .align () function std::cout << "Applied " << iterations << " ICP iteration(s) in " << time.toc () << " ms" << std::endl; if (icp.hasConverged ()) { std::cout << "\nICP has converged, score is " << icp.getFitnessScore () << std::endl; std::cout << "\nICP transformation " << iterations << " : cloud_icp -> cloud_in" << std::endl; transformation_matrix = icp.getFinalTransformation ().cast
(); print4x4Matrix (transformation_matrix); } else { PCL_ERROR ("\nICP has not converged.\n"); return (-1); } // Visualization boost::shared_ptr< pcl::visualization::PCLVisualizer > viewer(new pcl::visualization::PCLVisualizer("ICP demo")); //初始化相机参数 viewer->initCameraParameters (); // Create two vertically separated viewports int v1 (0); int v2 (1); viewer->createViewPort (0.0, 0.0, 0.5, 1.0, v1); viewer->createViewPort (0.5, 0.0, 1.0, 1.0, v2); // The color we will be using float bckgr_gray_level = 0.0; // Black float txt_gray_lvl = 1.0 - bckgr_gray_level; // Original point cloud is white pcl::visualization::PointCloudColorHandlerCustom
cloud_in_color_h (cloud_in, (int) 255 * txt_gray_lvl, (int) 255 * txt_gray_lvl, (int) 255 * txt_gray_lvl); viewer->addPointCloud (cloud_in, cloud_in_color_h, "cloud_in_v1", v1); viewer->addPointCloud (cloud_in, cloud_in_color_h, "cloud_in_v2", v2); // Transformed point cloud is green pcl::visualization::PointCloudColorHandlerCustom
cloud_tr_color_h (cloud_tr, 20, 180, 20); viewer->addPointCloud (cloud_tr, cloud_tr_color_h, "cloud_tr_v1", v1); // Transformed point cloud is blue pcl::visualization::PointCloudColorHandlerCustom
cloud_in48_color_h (cloud_in_48, 20, 20, 180); viewer->addPointCloud (cloud_in_48, cloud_in48_color_h, "cloud_in48_v1", v1); // ICP aligned point cloud is red pcl::visualization::PointCloudColorHandlerCustom
cloud_icp_color_h (cloud_icp, 180, 20, 20); viewer->addPointCloud (cloud_icp, cloud_icp_color_h, "cloud_icp_v2", v2); // Adding text descriptions in each viewport 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); 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); std::stringstream ss; ss << iterations; std::string iterations_cnt = "ICP iterations = " + ss.str (); viewer->addText (iterations_cnt, 10, 60, 16, txt_gray_lvl, txt_gray_lvl, txt_gray_lvl, "iterations_cnt", v2); // Set background color viewer->setBackgroundColor (bckgr_gray_level, bckgr_gray_level, bckgr_gray_level, v1); viewer->setBackgroundColor (bckgr_gray_level, bckgr_gray_level, bckgr_gray_level, v2); //添加坐标系 viewer->addCoordinateSystem(1.0, "reference"); // Set camera position and orientation //设置坐标原点 //https://www.cnblogs.com/ghjnwk/p/10305796.html //鼠标左键,鼠标右键,鼠标滚轮,按住滚轮不放 viewer->setCameraPosition (0, 0, 0, 0, 0, 0, 0); viewer->setSize (2048, 2048); // Visualiser window size // Register keyboard callback : viewer->registerKeyboardCallback (&keyboardEventOccurred, (void*) NULL); // Display the visualiser while (!viewer->wasStopped ()) { viewer->spinOnce (); // The user pressed "space" : if (next_iteration) { // The Iterative Closest Point algorithm time.tic (); icp.align (*cloud_icp); std::cout << "Applied 1 ICP iteration in " << time.toc () << " ms" << std::endl; if (icp.hasConverged ()) { printf ("\033[11A"); // Go up 11 lines in terminal output. printf ("\nICP has converged, score is %+.0e\n", icp.getFitnessScore ()); std::cout << "\nICP transformation " << ++iterations << " : cloud_icp -> cloud_in" << std::endl; transformation_matrix *= icp.getFinalTransformation ().cast
(); // WARNING /!\ This is not accurate! For "educational" purpose only! print4x4Matrix (transformation_matrix); // Print the transformation between original pose and current pose ss.str (""); ss << iterations; std::string iterations_cnt = "ICP iterations = " + ss.str (); viewer->updateText (iterations_cnt, 10, 60, 16, txt_gray_lvl, txt_gray_lvl, txt_gray_lvl, "iterations_cnt"); viewer->updatePointCloud (cloud_icp, cloud_icp_color_h, "cloud_icp_v2"); } else { PCL_ERROR ("\nICP has not converged.\n"); return (-1); } } next_iteration = false; } return (0);}

PCL_demo1 SampleFrame_52.ply SampleFrame_48.ply Rt_48_52.txt

 

你可能感兴趣的文章
启航 —— 记 —— 第二次自考的反思:自考与自我改造的困境
查看>>
数据结构与算法(三)——线性表
查看>>
Java8学习笔记(一)—— 函数式编程的四个基本接口
查看>>
Java8学习笔记(二)—— Lambda表达式
查看>>
Java8学习笔记(三)—— Optional类的使用
查看>>
Java8学习笔记(四) —— Stream流式编程
查看>>
Java8学习笔记(五)—— 方法引用(::双冒号操作符)
查看>>
数据结构与算法(四)—— 栈与队列
查看>>
数据结构与算法(五)—— 广义表
查看>>
微服务简介
查看>>
CAP定理
查看>>
Docker初探
查看>>
Docker镜像常用命令
查看>>
使用Dockerfile定制镜像
查看>>
Docker容器数据持久化
查看>>
Docker Compose
查看>>
GitLab克隆项目出现 “git未能顺利结束(退出码128)”问题的解决
查看>>
SpringBoot整合FastDFS(附源码)
查看>>
在RoboWare Studio下利用python语言实现话题
查看>>
科学计算库——NumPy库
查看>>