使用矩阵变换点云


尝试用4*4矩阵变换点云,将对加载的点云应用旋转和平移,并显示原始和变换后的点云。

1、首先,创建cpp文件,命名为matrix_transform.cpp并将如下代码放入其中:

#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>    //使用pcl::transformPointCloud函数
#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 help
if (pcl::console::find_switch (argc, argv, "-h") || pcl::console::find_switch (argc, argv, "--help")) {    //若输入cpp文件名加-h或--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) {  //表示读取的扩展名不是 .ply文件,立刻运行读取 .pcd文件
filenames = pcl::console::parse_file_extension_argument (argc, argv, ".pcd");

if (filenames.size () != 1) {    //判断读取的是否为pcd文件,若不是,将显示help信息
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> ());  //定义一个PointXYZ类型的点云指针,名称为source_cloud,并分配内存

if (file_is_pcd) {
if (pcl::io::loadPCDFile (argv[filenames[0]], *source_cloud) < 0) {  //将对应文件名的点云存储到source_cloud中,并判断返回值是否小于0
std::cout << "Error loading point cloud " << argv[filenames[0]] << std::endl << std::endl;  //若小于0将报错,显示帮助并退出
showHelp (argv[0]);
return -1;
}
} else {
if (pcl::io::loadPLYFile (argv[filenames[0]], *source_cloud) < 0) {  //ply也是同样的道理
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  //左上角的3*3矩阵为旋转矩阵,最后一列的前三行为平移
| 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();  //初始化一4*4单位阵,名为transform_1,即调用Eigen库中的Matrix4f(4阶float类型)

// Define a rotation matrix (see https://en.wikipedia.org/wiki/Rotation_matrix)
float theta = M_PI/4; // The angle of rotation in radians  //定义了Π/4的旋转角度
transform_1 (0,0) = std::cos (theta);
transform_1 (0,1) = -sin(theta);
transform_1 (1,0) = sin (theta);
transform_1 (1,1) = std::cos (theta);
// (row, column)

// Define a translation of 2.5 meters on the x axis.
transform_1 (0,3) = 2.5;  //也就是对X轴平移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();  //定义一个3阶float类型仿射变换矩阵(单位阵)

// Define a translation of 2.5 meters on the x axis.
transform_2.translation() << 2.5, 0.0, 0.0;  //X轴正向平移2.5

// The same rotation matrix as before; theta radians around Z axis
transform_2.rotate (Eigen::AngleAxisf (theta, Eigen::Vector3f::UnitZ()));  //同时绕Z轴旋转theta

// 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> ());  //定义一个PointXYZ类型的点云指针,名称为transformed_cloud
// You can either apply transform_1 or transform_2; they are the same
pcl::transformPointCloud (*source_cloud, *transformed_cloud, transform_2);  //使用 transform_2将原点云转化到transformed_cloud中

// 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); // 红色
viewer.addPointCloud (transformed_cloud, transformed_cloud_color_handler, "transformed_cloud");

viewer.addCoordinateSystem (1.0, "cloud", 0);  //添加坐标系轴长为1
viewer.setBackgroundColor(0.05, 0.05, 0.05, 0);   // 设置背景色为暗灰色,0指不透明度
viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "original_cloud");  //设置点云渲染属性点体积为2
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;
}

2、新建CMakeLists.txt文件,填入一下指令:

cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

project(pcl-matrix_transform)

find_package(PCL 1.7 REQUIRED)

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

add_executable (matrix_transform matrix_transform.cpp)
target_link_libraries (matrix_transform ${PCL_LIBRARIES})

3、创建名为build的文件夹,通过终端在cpp文件夹里输入如下指令:

mkdir build 在build文件夹下终端中输入:cmake .. 完成后输入:make 生成可执行文件。

4、我们可以使用官网教程中的ism_train_wolf.pcd文件展示效果,将pcd文件复制到build文件夹中,运行代码./matrix_transform ism_train_wolf.pcd 可以看到点云狼已经被旋转45°  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM