PCL点云库:对点云进行变换(Using a matrix to transform a point cloud)
点云数据可以用ASCII码的形式存储在PCD文件中(关于该格式的描述可以参考链接:The PCD (Point Cloud Data) file format)。为了生成三维点云数据,在excel中用rand()函数生成200行0-1的小数,ABC三列分别代表空间点的xyz坐标。

# .PCD v.7 - Point Cloud Data file format
VERSION .7
FIELDS x y z
SIZE 4 4 4
TYPE F F F
COUNT 1 1 1
WIDTH 200
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS 200
DATA ascii
0.88071666 0.369209703 0.062937221
0.06418104 0.579762553 0.221359779
...
...
0.640053058 0.480279041 0.843647334
0.245554712 0.825770496 0.626442137
进行点云的变换主要用到的函数是pcl::transformPointCloud,函数原型为:
void pcl::transformPointCloud(const pcl::PointCloud< PointT > & cloud_in,
pcl::PointCloud< PointT > & cloud_out,
const Eigen::Matrix4f & transform )
参数中cloud_in为源点云,cloud_out为变换后的点云,transform为变换矩阵。下面的代码对源点云绕Z轴旋转45°,然后沿X轴平移了2.5个单位:
#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> // This is the main function
int main (int argc, char** argv)
{ //creates a PointCloud<PointXYZ> boost shared pointer and initializes it.
pcl::PointCloud<pcl::PointXYZ>::Ptr source_cloud (new pcl::PointCloud<pcl::PointXYZ> ()); // Load PCD file
if (pcl::io::loadPCDFile<pcl::PointXYZ> ("sample.pcd", *source_cloud) == -)
{
PCL_ERROR ("Couldn't read file sample.pcd \n");
return (-);
} /* 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/; // The angle of rotation in radians
transform_1 (,) = cos (theta);
transform_1 (,) = -sin(theta);
transform_1 (,) = sin (theta);
transform_1 (,) = cos (theta);
// (row, column) // Define a translation of 2.5 meters on the x axis.
transform_1 (,) = 2.5; // Print the transformation
printf ("Method #1: using a Matrix4f\n");
std::cout << transform_1 << std::endl; // 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); // 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, , , );
// 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, , , ); // Red
viewer.addPointCloud (transformed_cloud, transformed_cloud_color_handler, "transformed_cloud"); viewer.addCoordinateSystem (1.0, ); //Adds 3D axes describing a coordinate system to screen at 0,0,0.
viewer.setBackgroundColor(0.05, 0.05, 0.05, ); // Setting background to a dark grey
viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, , "original_cloud");
viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, , "transformed_cloud");
//viewer.setPosition(800, 400); // Setting visualiser window position while (!viewer.wasStopped ()) { // Display the visualiser until 'q' key is pressed
viewer.spinOnce ();
} return ;
}
在PCL官网下载All-in-one installers,由于使用的是Win7 32位系统,因此选择了Windows MSVC 2010 (32bit)进行安装,这个All-in-one的安装程序会同时安装除QT外的一些第三方依赖库,比如boost、Eigen、VTK、OpenNI等。

安装好之后如果是自己在VS2010中配置工程属性,将会很麻烦,可以参考Using PCL in your own project。下面通过Cmake来自动生成VS2010的项目。首先创建一个CMakeLists.txt文件(注意其中PCL的版本)。如果编译软件使用了外部库,事先并不知道它的头文件和链接库的位置。得在编译命令中加上包含它们的查找路径。CMake使用find_package命令来解决这个问题。
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(pcl-matrix_transform)
find_package(PCL 1.6 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})
然后使用CMake的GUI程序来生成工程文件。Where is the source code栏选择源代码所在路径,这里代码放在桌面上;Where to build the binaries栏选择生成文件所在的路径,默认在桌面上的build文件夹中。点击Configure之后会弹出对话框,选择Visual Studio 2010,最后点击生成按钮,将会在build文件夹中生成VS2010的工程文件。

在编译之前需要注意一点,由于All-in-one的安装程序没有安装QT(需要单独安装),需要在工程属性的附加依赖项中删掉QT的lib(这个程序中暂时也没有用到QT相关的东西),否则会出现编译错误。最后运行程序,结果如下图所示。其中红色的点云为变换后的。

参考:
Using a matrix to transform a point cloud
http://pointclouds.org/documentation/tutorials/matrix_transform.php#matrix-transform
The PCD (Point Cloud Data) file format
http://pointclouds.org/documentation/tutorials/pcd_file_format.php#pcd-file-format
Reading Point Cloud data from PCD files
http://pointclouds.org/documentation/tutorials/reading_pcd.php#reading-pcd
PCL学习笔记(1):pcl1.6.0+vs2010环境配置以及第一个pcl程序
http://www.voidcn.com/blog/chentravelling/article/p-3487308.html
PCL点云库:对点云进行变换(Using a matrix to transform a point cloud)的更多相关文章
- 配置点云库PCL时遇到的问题
配置PCL基本参照PCL中国官网教程 http://www.pclcn.org/study/shownews.php?lang=cn&id=34 配置点云库时遇到的问题(基于win8 64位, ...
- PCL点云库:ICP算法
ICP(Iterative Closest Point迭代最近点)算法是一种点集对点集配准方法.在VTK.PCL.MRPT.MeshLab等C++库或软件中都有实现,可以参见维基百科中的ICP Alg ...
- 点云库PCL学习
1. 点云的提取 点云的获取:RGBD获取 点云的获取:图像匹配获取(通过摄影测量提取点云数据) 点云的获取:三维激光扫描仪 2. PCL简介 PCL是Point Cloud Library的简称,是 ...
- PCL点云库中的坐标系(CoordinateSystem)
博客转载自:https://blog.csdn.net/qq_33624918/article/details/80488590 引言 世上本没有坐标系,用的人多了,便定义了坐标系统用来定位.地理坐标 ...
- Windows下安装PCL点云库
原文链接:http://blog.csdn.net/u012337034/article/details/38270109 简介: 在Windows下安装PCL点云库的方法大概有两种: ...
- Windows 8 64位系统 在VS2010 32位软件上 搭建 PCL点云库 开发环境
Windows 8 64位系统 在VS2010 32位软件上 搭建 PCL点云库 开发环境 下载PCL For windows 软件包 到这个网站下载PCL-All-In-One Installer: ...
- ViCANdo新版本发布(PART1) | 点云库(PCL)集成
激光雷达 随着智能驾驶技术的发展,激光雷达迅速的进入工程师的视野,不管是机械式.MEMS还是纯固态激光雷达,本质上都是以一定的速度扫描照射区域,在此过程中激光雷达不断的发出激光并接收反 ...
- python利用pybind11调用PCL点云库
2019年7月9日14:31:13 完成了一个简单的小例子,python生成点云数据,利用pybind11传给PCL显示. ubuntu 16.04 + Anaconda3 python3.6 + ...
- PCL点云库(Point Cloud Library)简介
博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=29 什么是PCL PCL(Point Cloud Library)是在吸收了 ...
随机推荐
- oracle的面试问题
1. Oracle跟SQL Server 2005的区别? 宏观上: 1). 最大的区别在于平台,oracle可以运行在不同的平台上,sql server只能运行在windows平台上,由于windo ...
- Ceph的集群全部换IP
由于要对物理机器要做IP规划,所有物理机统一做到35网段,对于ceph集群来说,是有一定工作量的. 前提条件,ceph集群正常.原来的所有集群在44网段.mon地址是172.17.44.22 在44网 ...
- android 学习随笔十(网络:get、post提交数据)
1.get public class Tools { public static String getTextFromStream(InputStream is){ byte[] b = new by ...
- iOS 学习笔记 十三 (2015.04.15)采用第三方库,实现ios录音转为amr
1.第三方开源库地址 https://github.com/guange2015/ios-amr 2.参考博客地址 http://blog.csdn.net/windsoul85/article/de ...
- ubunu下用命令设置壁纸
ubunu下用命令设置壁纸: gsettings set org.gnome.desktop.background picture-uri “file:[fileName]” eg:gsettings ...
- linux下配置环境变量【原创】
用户目录下的.profile, 你只需要在用户目录下,创建bin目录,在里面放入你想要执行的可执行文件,就可以在命令行下就可以使用了 # ~/.profile: executed by the com ...
- Centos7下用命令下载jdk7及jboss-eap-6
计划在南非的一台云主机上搭建一个web环境,首先需要在云主机上搭建我们指定版本的JDK和JBOSS. 在云上搭特定版本的环境,软件包传输是一件十分艰巨的任务.我先后尝试了:公司电信专线.公司联通专线. ...
- golang的helloworld
新建源码文件hello.go mkdir -p /work/goTest/ cd /work/goTest/ vim hello.go 编码hello.go文件: package main impor ...
- VS 6.00 工程项目文件详解
*.dsp(DeveloperStudio Project):是VC++的工程配置文件,比如说你的工程包含哪个文件,你的编译选项是什么等等,编译的时候是按照.dsp的配置来的.*.dsw(Develo ...
- c#之双色球
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...