pcl 学习2 利用矩阵转移一个点云
来源:互联网 发布:网络语弹棉花什么意思 编辑:程序博客网 时间:2024/05/16 11:54
点云处理中,我们有可能需要将点云旋转或者平移到某个位置。pcl提供了相应的函数。假如转移矩阵是[R|T],那么目标点dst和原始点src的关系是:
dst=[R|t]*src.
转移矩阵的格式是
|——-> This column is the translation
| R R R x | \
| R R R y | }-> The identity 3x3 matrix (no rotation) on the left
| R R R z | /
| 0 0 0 1 | -> We do not use this line (and it has to stay 0,0,0,1)
不懂的移步到http://blog.sina.com.cn/s/blog_620bf89501011fl8.html
官方教程
http://pointclouds.org/documentation/tutorials/matrix_transform.php#matrix-transform
代码
#include <iostream>#include <pcl/io/pcd_io.h>#include <pcl/io/ply_io.h>#include <pcl/point_cloud.h>#include <pcl/console/parse.h>#include <pcl/common/transforms.h>#include <pcl/visualization/pcl_visualizer.h>// This function displays the helpvoidshowHelp(char * program_name){ std::cout << std::endl; std::cout << "Usage: " << program_name << " cloud_filename.[pcd|ply]" << std::endl; std::cout << "-h: Show this help." << std::endl;}// This is the main functionintmain(int argc, char** argv){ // Show help if (pcl::console::find_switch(argc, argv, "-h") || pcl::console::find_switch(argc, argv, "--help")) { showHelp(argv[0]); return 0; } // Fetch point cloud filename in arguments | Works with PCD and PLY files std::vector<int> filenames; bool file_is_pcd = false; filenames = pcl::console::parse_file_extension_argument(argc, argv, ".ply"); if (filenames.size() != 1) { filenames = pcl::console::parse_file_extension_argument(argc, argv, ".pcd"); if (filenames.size() != 1) { showHelp(argv[0]); return -1; } else { file_is_pcd = true; } } // Load file | Works with PCD and PLY files pcl::PointCloud<pcl::PointXYZ>::Ptr source_cloud(new pcl::PointCloud<pcl::PointXYZ>()); if (file_is_pcd) { if (pcl::io::loadPCDFile(argv[filenames[0]], *source_cloud) < 0) { std::cout << "Error loading point cloud " << argv[filenames[0]] << std::endl << std::endl; showHelp(argv[0]); return -1; } } else { if (pcl::io::loadPLYFile(argv[filenames[0]], *source_cloud) < 0) { std::cout << "Error loading point cloud " << argv[filenames[0]] << std::endl << std::endl; showHelp(argv[0]); return -1; } } /* Reminder: how transformation matrices work : |-------> This column is the translation | 1 0 0 x | \ | 0 1 0 y | }-> The identity 3x3 matrix (no rotation) on the left | 0 0 1 z | / | 0 0 0 1 | -> We do not use this line (and it has to stay 0,0,0,1) METHOD #1: Using a Matrix4f This is the "manual" method, perfect to understand but error prone ! */ Eigen::Matrix4f transform_1 = Eigen::Matrix4f::Identity(); // Define a rotation matrix (see https://en.wikipedia.org/wiki/Rotation_matrix) float theta = M_PI / 4; // The angle of rotation in radians transform_1(0, 0) = cos(theta); transform_1(0, 1) = -sin(theta); transform_1(1, 0) = sin(theta); transform_1(1, 1) = cos(theta); // (row, column) // Define a translation of 2.5 meters on the x axis. transform_1(0, 3) = 2.5; // Print the transformation printf("Method #1: using a Matrix4f\n"); std::cout << transform_1 << std::endl; /* METHOD #2: Using a Affine3f This method is easier and less error prone */ Eigen::Affine3f transform_2 = Eigen::Affine3f::Identity(); // Define a translation of 2.5 meters on the x axis. transform_2.translation() << 2.5, 0.0, 0.0; // The same rotation matrix as before; theta radians arround Z axis transform_2.rotate(Eigen::AngleAxisf(theta, Eigen::Vector3f::UnitZ())); // Print the transformation printf("\nMethod #2: using an Affine3f\n"); std::cout << transform_2.matrix() << std::endl; // Executing the transformation pcl::PointCloud<pcl::PointXYZ>::Ptr transformed_cloud(new pcl::PointCloud<pcl::PointXYZ>()); // You can either apply transform_1 or transform_2; they are the same pcl::transformPointCloud(*source_cloud, *transformed_cloud, transform_2); // Visualization printf("\nPoint cloud colors : white = original point cloud\n" " red = transformed point cloud\n"); pcl::visualization::PCLVisualizer viewer("Matrix transformation example"); // Define R,G,B colors for the point cloud pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> source_cloud_color_handler(source_cloud, 255, 255, 255); // We add the point cloud to the viewer and pass the color handler viewer.addPointCloud(source_cloud, source_cloud_color_handler, "original_cloud"); pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> transformed_cloud_color_handler(transformed_cloud, 230, 20, 20); // Red viewer.addPointCloud(transformed_cloud, transformed_cloud_color_handler, "transformed_cloud"); viewer.addCoordinateSystem(1.0, "cloud", 0); viewer.setBackgroundColor(0.05, 0.05, 0.05, 0); // Setting background to a dark grey viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "original_cloud"); viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "transformed_cloud"); //viewer.setPosition(800, 400); // Setting visualiser window position while (!viewer.wasStopped()) { // Display the visualiser until 'q' key is pressed viewer.spinOnce(); } return 0;}
源代码中用了两种方式构造转移矩阵,一种是直接赋值,另一种用eigen自动生成。
效果如下
点云文件来源请参考上一个博客。运行代码时在工程 属性 配置属性 调试 命令参数中输入点云文件路径(Vs用户)
0 0
- pcl 学习2 利用矩阵转移一个点云
- PCL 利用空间同名点求取旋转和平移矩阵
- PCL点云程序学习
- PCL 学习笔记-使用 PCL 来显示点云
- 点云学习--PCl 1.7.2 FLANN bug解决
- pcl学习笔记(一)点云IO
- PCL学习之获取点云
- 点云PCL估计一个点云的表面法线
- 利用PCL(Point Cloud Library)进行点云拼接
- PCL学习笔记(2):点云视窗类CloudViewer的介绍以及PCL程序:代码解析
- PCL点云分割(2)
- PCL点云滤波
- 点云python-pcl
- 调试一个MFC的点云可视化(PCL)程序
- pcl从一个点云里面导出下标
- 利用PCL处理Realsense点云数据-在vs2015里用PCL获取Realsense点云数据
- pcl 学习 离群点删除
- 65.[PCL]点云及PCL基础
- Codeforces Gym 100623I Problem I. Important Wires
- java_匿名内部类
- View的工作原理
- React Native 从入门到原理
- 《Android开发艺术探索》读书笔记----第一章:Activity的启动模式
- pcl 学习2 利用矩阵转移一个点云
- Q&A——物理
- 理解RemoteViews
- EMP界面开发规范(2)
- 修改linux 系统编码为utf-8
- 推荐一个好用的免费空间
- Linux context_switch函数实现分析
- Java 中protected详解
- Android的Drawable