转换点云
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_cloud.h>
#include <pcl/common/transforms.h> //allows us to use pcl::transformPointCloud function
#include <pcl/visualization/pcl_visualizer.h>
int main (int argc, char** argv)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr source_cloud (new pcl::PointCloud<pcl::PointXYZ> ());
if (pcl::io::loadPCDFile<pcl::PointXYZ> ("Marketplace_downsampled.pcd", *source_cloud) == -1)
{
PCL_ERROR ("Couldn't read file sample.pcd \n");
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)
// Here we defined a 45° (PI/4) rotation around the Z axis and a translation on the X axis.
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;
// Executing the transformation
pcl::PointCloud<pcl::PointXYZ>::Ptr transformed_cloud (new pcl::PointCloud<pcl::PointXYZ> ());
/*
void pcl::transformPointCloud(const pcl::PointCloud< PointT > & cloud_in,
pcl::PointCloud< PointT > & cloud_out,
const Eigen::Matrix4f & transform )
*/
// Apply an affine transform defined by an Eigen Transform.
pcl::transformPointCloud (*source_cloud, *transformed_cloud, transform_1);
//其实这个打开和保存的方式可以用pcl::PCDReader 和pcl::PCDWritwer
pcl::io::savePCDFileASCII ("Marketplace_downsampled_transformed.pcd", *transformed_cloud);
return 0;
}
然后再写cmakelist文件
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(transform_cloud)
find_package(PCL 1.2 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable (transform_cloud transform_cloud.cpp)
target_link_libraries (transform_cloud ${PCL_LIBRARIES})
本文介绍了一种使用PCL库进行点云数据转换的方法,包括读取点云数据、定义旋转和平移矩阵、应用仿射变换以及保存转换后的点云。通过具体的C++代码示例,展示了如何实现点云的旋转和平移。
458

被折叠的 条评论
为什么被折叠?



