使用矩陣變換點雲


嘗試用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