《PCL点云库学习&VS2010(X64)》Part 34 旋转平移矩阵用法

1、变换与投影矩阵讲解:

https://en.wikipedia.org/wiki/Transformation_matrix

http://web.iitd.ac.in/~hegde/cad/lecture/L6_3dtrans.pdf

点云变换主要涉及平移、旋转、缩放、反射、剪切、视角转换、投影等,这里主要介绍平移与旋转。

2、使用Eigen::Matrix4f 进行变换

#include <pcl/io/pcd_io.h>
#include <pcl/common/transforms.h>
#include <pcl/visualization/pcl_visualizer.h>int
main(int argc, char** argv)
{// Objects for storing the point clouds.pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);pcl::PointCloud<pcl::PointXYZ>::Ptr transformed(new pcl::PointCloud<pcl::PointXYZ>);// Read a PCD file from disk.if (pcl::io::loadPCDFile<pcl::PointXYZ>(argv[1], *cloud) != 0){return -1;}// Transformation matrix object, initialized to the identity matrix// (a null transformation).Eigen::Matrix4f transformation = Eigen::Matrix4f::Identity();// Set a rotation around the Z axis (right hand rule).float theta = 90.0f * (M_PI / 180.0f); // 90 degrees.transformation(0, 0) = cos(theta);transformation(0, 1) = -sin(theta);transformation(1, 0) = sin(theta);transformation(1, 1) = cos(theta);// Set a translation on the X axis.transformation(0, 3) = 1.0f; // 1 meter (positive direction).pcl::transformPointCloud(*cloud, *transformed, transformation);// Visualize both the original and the result.pcl::visualization::PCLVisualizer viewer(argv[1]);viewer.addPointCloud(cloud, "original");// The transformed one's points will be red in color.pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> colorHandler(transformed, 255, 0, 0);viewer.addPointCloud(transformed, colorHandler, "transformed");// Add 3D colored axes to help see the transformation.viewer.addCoordinateSystem(1.0, 0);while (!viewer.wasStopped()){viewer.spinOnce();}
}

2、使用Eigen::Affine3进行变换

#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 help
void
showHelp(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 function
int
main (int argc, char** argv)
{// Show helpif (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 filesstd::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 filespcl::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 Matrix4fThis 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 radianstransform_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 transformationprintf ("Method #1: using a Matrix4f\n");std::cout << transform_1 << std::endl;/*  METHOD #2: Using a Affine3fThis 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 axistransform_2.rotate (Eigen::AngleAxisf (theta, Eigen::Vector3f::UnitZ()));// Print the transformationprintf ("\nMethod #2: using an Affine3f\n");std::cout << transform_2.matrix() << std::endl;// Executing the transformationpcl::PointCloud<pcl::PointXYZ>::Ptr transformed_cloud (new pcl::PointCloud<pcl::PointXYZ> ());// You can either apply transform_1 or transform_2; they are the samepcl::transformPointCloud (*source_cloud, *transformed_cloud, transform_2);// Visualizationprintf(  "\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 cloudpcl::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 handlerviewer.addPointCloud (source_cloud, source_cloud_color_handler, "original_cloud");pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> transformed_cloud_color_handler (transformed_cloud, 230, 20, 20); // Redviewer.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 greyviewer.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 positionwhile (!viewer.wasStopped ()) { // Display the visualiser until 'q' key is pressedviewer.spinOnce ();}return 0;
}

3、使用Eigen::Quaternion

4、总结:

1)使最小二乘方程值最小,从而求得c,R,T三个参数,分别表示局部放大系数、旋转系数和平移系数。

齐次变换后得到一个4x4矩阵:

返回一个矩阵,使上述的最小二乘方程最小。

见文章“Least-squares estimation of transformation parameters between two point patterns”Shinji Umeyama, PAMI 1991, DOI: 10.1109/34.88573

2)matrix4f的具体结构及意义。

  /* 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 Matrix4fThis is the "manual" method, perfect to understand but error prone !*/

由上述的提示段代码可知,左上角的三行三列主要是旋转矩阵参数,最后一列的上三行是平移矩阵。

实际上该4x4矩阵肯定不止这些,具体的如下图所示:

其中p,q,r对应的透视变换参数,左上角三行三列包括局部缩放、剪切、旋转和反射等参数。具体的设置见文章中的第二个pdf链接。最后一行的l,m,n表示沿着x,y,z轴进行平移。s表示全局缩放参数。

《PCL点云库学习VS2010(X64)》Part 34 旋转平移矩阵用法相关推荐

  1. 《PCL点云库学习VS2010(X64)》Part 45 点云压缩算法—扫描线(DouglasPeuckerAlgorithm)

    <PCL点云库学习&VS2010(X64)>Part 45 点云压缩算法-扫描线(DouglasPeuckerAlgorithm) 道格拉斯-普克算法主要应用有点云滤波.点云压缩. ...

  2. 《PCL点云库学习VS2010(X64)》Part 51 PTDV0.2迭代加密三角网算法V0.2

    <PCL点云库学习&VS2010(X64)>Part 51 PTDV0.2迭代加密三角网算法V0.2 1.利用实际点云测试初级版本的渐进加密三角网算法: 1.获取最低点 2.构建初 ...

  3. 《PCL点云库学习VS2010(X64)》Part 41 图形学领域的关键算法及源码链接

    <PCL点云库学习&VS2010(X64)>Part 41 图形学领域的关键算法及源码链接 原文链接: Conference papers Graphics Conference ...

  4. 《PCL点云库学习VS2010(X64)》Part 37 FLANN——快速最近邻搜索库

    <PCL点云库学习&VS2010(X64)>Part 37 FLANN--快速最近邻搜索库 一.介绍 该算法库在OpenCV和PCL等开源库中应用较广,是PCL的默认安装库之一.与 ...

  5. PCL点云库学习笔记 点云的欧式聚类

    欧式聚类详解(点云数据处理) 欧式聚类是一种基于欧氏距离度量的聚类算法.基于KD-Tree的近邻查询算法是加速欧式聚类算法的重要预处理方法. KD-Tree最近邻搜索 Kd-树是K-dimension ...

  6. PCL点云库学习(1):环境配置(Ubuntu16.04+QT5+VTK8.0)

    方式一:从公共软件源安装 sudo apt-get install libpcl-dev pcl-tools 直接输入上面代码安装,不保证一定能行.有一堆依赖的东西,保不准就报错了. 方式二:编译安装 ...

  7. PCL点云库学习笔记(3):点云的欧式聚类

    初学者笔记: 点云欧式聚类算法流程 (1)点云读入: (2)体素化下采样(方便处理): (3)去离散点: (4)RANSAC算法检测平面,并剔除平面点云: (5)欧式聚类: (6)结果的输出和可视化: ...

  8. PCL点云库安装及学习(2021.7.28)

    PCL点云库学习 2021.7.28 1.PCL简介 2.Win10系统下PCL环境配置 2.1 前提环境(Win10 64位+Visual Studio 2015) 2.2 方式一:源码编译(过程繁 ...

  9. 编译安装PCL点云库,Kinect2驱动,乐视Astra相机驱动

    编译安装PCL点云库 安装方法一 3d点云安装 apt-cache cearch pcl 安装 sudo apt install 上面查出来的 opencv不建议用以上方法因为需要 安装 opencv ...

最新文章

  1. js:appendChild、insertBefore和insertAfter
  2. Oracle忘记用户名和密码的解决方案
  3. 遗传算法(Genetic Algorithm )+C++实现解决TSP问题
  4. DFT实训教程笔记2(bibili版本)- Scan synthesis practice
  5. 强烈推荐的TensorFlow、Pytorch和Keras的样例资源(深度学习初学者必须收藏)
  6. 乒乓球十一分制比赛规则_乒乓球竞赛规则 赛制和比赛规则
  7. postgreSQL源码分析——索引的建立与使用——各种索引类型的管理和操作(2)
  8. An internal error occurred during: Launching web on MyEclipse Tomcat
  9. 计算机专业技术卷,全国计算机技术与软件专业技术资格(水平)考试1990-2009软件设计师历年真题及答案...
  10. Windows ZIP Archive安装和卸载MySQL 8.0
  11. matlab示例程序,matlab示例程序
  12. 使用iToolab UnlockGo 删除iPhone/iPad上的各种锁
  13. java 抽屉效果_[Java教程]抽屉组件的滑动效果_星空网
  14. 轻松了解,顶级域名,二级域名,三级域名
  15. 识别IOS和android方法
  16. csv文件用excel打开乱码问题
  17. Multisim基础 发光二极管 添加元件的位置
  18. 笔记-Codeforces比赛
  19. 【目标检测】小目标检测问题及解决方法
  20. 美的java面试经验

热门文章

  1. vue路由安全守卫 (组件内路由守卫)
  2. ffmpeg 提取 视频,音频,字幕 方法
  3. 人死了后还有来生吗?
  4. PDF如何在线转换成PPT呢?
  5. (阿里offer)春招知识点总结1:java基础+集合+并发+jvm+ssm
  6. 想不想恶搞你的朋友?试试关不掉的弹窗(vbs)
  7. electron打包应用再win7无法启动解决方案
  8. jquery.print 时间_Jquery Jqprint—随着Jquery Jqprint实现网页打印
  9. send函数给FTP服务器发消息,send函数给FTP服务器发消息
  10. U盘插入电脑提示格式化怎么办