本教程演示如何从点云和给定传感器位置创建距离图像。该代码创建了漂浮在观察者前方的矩形示例点云。
#代码
首先,在你最喜欢的编辑器中创建一个叫做range_image_creation.cpp的文件,并在其中放置下面的代码:
#include <pcl/range_image/range_image.h>
int main (int argc, char** argv) {
pcl::PointCloud<pcl::PointXYZ> pointCloud;
// Generate the data
for (float y=-0.5f; y<=0.5f; y+=0.01f) {
for (float z=-0.5f; z<=0.5f; z+=0.01f) {
pcl::PointXYZ point;
point.x = 2.0f - y;
point.y = y;
point.z = z;
pointCloud.points.push_back(point);
}
}
pointCloud.width = (uint32_t) pointCloud.points.size();
pointCloud.height = 1;
// We now want to create a range image from the above point cloud, with a 1deg angular resolution
float angularResolution = (float) ( 1.0f * (M_PI/180.0f)); // 1.0 degree in radians
float maxAngleWidth = (float) (360.0f * (M_PI/180.0f)); // 360.0 degree in radians
float maxAngleHeight = (float) (180.0f * (M_PI/180.0f)); // 180.0 degree in radians
Eigen::Affine3f sensorPose = (Eigen::Affine3f)Eigen::Translation3f(0.0f, 0.0f, 0.0f);
pcl::RangeImage::CoordinateFrame coordinate_frame = pcl::RangeImage::CAMERA_FRAME;
float noiseLevel=0.00;
float minRange = 0.0f;
int borderSize = 1;
pcl::RangeImage rangeImage;
rangeImage.createFromPointCloud(pointCloud, angularResolution, maxAngleWidth, maxAngleHeight,
sensorPose, coordinate_frame, noiseLevel, minRange, borderSize);
std::cout << rangeImage << "\n";
}
#说明
让我们看看这个部分:
#include <pcl/range_image/range_image.h>
int main (int argc, char** argv) {
pcl::PointCloud<pcl::PointXYZ> pointCloud;
// Generate the data
for (float y=-0.5f; y<=0.5f; y+=0.01f) {
for (float z=-0.5f; z<=0.5f; z+=0.01f) {
pcl::PointXYZ point;
point.x = 2.0f - y;
point.y = y;
point.z = z;
pointCloud.points.push_back(point);
}
}
pointCloud.width = (uint32_t) pointCloud.points.size();
pointCloud.height = 1;
This includes the necessary range image header, starts the main and generates a point cloud that represents a rectangle.
float angularResolution = (float) ( 1.0f * (M_PI/180.0f)); // 1.0 degree in radians
float maxAngleWidth = (float) (360.0f * (M_PI/180.0f)); // 360.0 degree in radians
float maxAngleHeight = (float) (180.0f * (M_PI/180.0f)); // 180.0 degree in radians
Eigen::Affine3f sensorPose = (Eigen::Affine3f)Eigen::Translation3f(0.0f, 0.0f, 0.0f);
pcl::RangeImage::CoordinateFrame coordinate_frame = pcl::RangeImage::CAMERA_FRAME;
float noiseLevel=0.00;
float minRange = 0.0f;
int borderSize = 1;
这部分定义了我们想要创建的距离图像的参数。
角分辨率假定为1度,这意味着由相邻像素表示的光束相差一度。
maxAngleWidth = 360和maxAngleHeight = 180意味着我们模拟的距离传感器具有完整的周围360度视图。您始终可以使用此设置,因为范围图像只会被裁剪为只有自动观察到某些区域的区域。但是,您可以通过减少值来节省一些计算。例如,对于面向前方的180度视角的激光扫描仪,在传感器后面不能观察到任何点,maxAngleWidth = 180就足够了。
sensorPose将虚拟传感器的6DOF位置定义为roll = pitch = yaw = 0的原点。
coordinate_frame = CAMERA_FRAME告诉系统x向右,y向下,z轴向前。另一种选择是LASER_FRAME,x向前,y向左,z向上。
对于noiseLevel = 0,范围图像是使用正常的z缓冲区创建的。然而,如果你想平均落在同一个单元格中的点数,你可以使用更高的值。0.05意味着,所有与最近点距离5cm的点都用于计算范围。
如果minRange大于0,所有靠近的点都将被忽略。
borderSize大于0会在裁剪图像时在图像周围留下未观察点的边框。
pcl::RangeImage rangeImage;
rangeImage.createFromPointCloud(pointCloud, angularResolution, maxAngleWidth, maxAngleHeight,sensorPose, coordinate_frame, noiseLevel, minRange, borderSize);
std::cout << rangeImage << "\n";
剩下的代码使用给定的参数从点云中创建距离图像,并在终端上输出一些信息。
范围图像来自PointCloud类,其点包含成员x,y,z和范围。有三种观点。有效点的实际范围大于零。未观察点具有x = y = z = NAN且range= -INFINITY。远程点具有x = y = z = NAN且range= INFINITY(无穷大)。
#编译和运行程序
将以下行添加到您的CMakeLists.txt文件中:
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(range_image_creation)
find_package(PCL 1.2 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable (range_image_creation range_image_creation.cpp)
target_link_libraries (range_image_creation ${PCL_LIBRARIES})
在创建可执行文件后,您可以运行它。简单地做:
./range_image_creation
你应该看到以下内容:
range image of size 42x36 with angular resolution 1deg/pixel and 1512 points