构造点云数据
pcl::PointCloud<pcl::PointXYZ> pointcloud;
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.0 - y;
point.y = y;
point.z = z;
pointcloud.points.push_back(point);
}
}
pointcloud.width = (uint32_t)pointcloud.points.size();
pointcloud.height = 1;
创建深度图像
//定义参数
float angularResolution = (float)(1.0f * (M_PI / 180.0f));//角分辨率是1度,也就是说由相邻像素表示的光束相差一弧度
float maxAngleWidth = (float)(360.0f * (M_PI / 180.0f));
float maxAngleHeight = (float)(180.0f * (M_PI / 180.0f));//模拟的范围传感器有一个完整的360度全景图
Eigen::Affine3f sensorPose = (Eigen::Affine3f) Eigen::Translation3f(0.0f, 0.0f, 0.0f);//传感器的位置定义了虚拟传感器的6 DOF位置,它的原点是滚动=俯仰=偏航=0。
pcl::RangeImage::CoordinateFrame coordinate_frame = pcl::RangeImage::CAMERA_FRAME;//x朝向右,y向下,z轴是向前的,另一种选择是激光框架,x面向前方,y向左,z向上。
float noiseLevel = 0.00;//对于噪声,噪声是0,范围图像是使用普通的z缓冲区创建的
float minRange = 0.0f;// minRange >0 ,所有更近的点都将被忽略
int borderSize = 1;//边界大小>0,边界将会在裁剪时留下一个未被观察到的点的边界。
pcl::RangeImage rangeImage;
rangeImage.createFromPointCloud(pointcloud, angularResolution, maxAngleWidth, maxAngleHeight, sensorPose,
coordinate_frame, noiseLevel, minRange, borderSize);
可视化
//以图像的形式显示深度图像,深度值作为颜色显示
pcl::visualization::RangeImageVisualizer range_image_widget("Range image");
range_image_widget.showRangeImage(rangeImage);
std::cout << rangeImage << "\n";
std::cout << pointcloud << "\n";
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("viewer"));
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> colorhandler(pointcloud.makeShared(), 255, 0, 0);
viewer->addPointCloud(pointcloud.makeShared(), colorhandler,"cloud");
viewer->setBackgroundColor(1, 1, 1);
viewer->setCameraPosition(
0, 0, 0,
0, 0, -1,
0, 0, 0);
viewer->addCoordinateSystem(1, "cloud");
可视化结果
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/e985b1394ada555b4d2dc9f41de8e42c.png)
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/4f1904e13468011f7276f905797d8ef6.png)
完整代码
#include "stdafx.h"
#include <iostream>
#include <pcl\visualization\pcl_plotter.h>
#include <pcl\visualization\pcl_visualizer.h>
#include <pcl\point_types.h>
#include <pcl\point_cloud.h>
#include <pcl\range_image\range_image.h>
#include <pcl\visualization\range_image_visualizer.h>
#include <vector>
int _tmain(int argc, _TCHAR* argv[])
{
pcl::PointCloud<pcl::PointXYZ> pointcloud;
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.0 - y;
point.y = y;
point.z = z;
pointcloud.points.push_back(point);
}
}
pointcloud.width = (uint32_t)pointcloud.points.size();
pointcloud.height = 1;
//定义参数
float angularResolution = (float)(1.0f * (M_PI / 180.0f));//角分辨率是1度,也就是说由相邻像素表示的光束相差一弧度
float maxAngleWidth = (float)(360.0f * (M_PI / 180.0f));
float maxAngleHeight = (float)(180.0f * (M_PI / 180.0f));//模拟的范围传感器有一个完整的360度全景图
Eigen::Affine3f sensorPose = (Eigen::Affine3f) Eigen::Translation3f(0.0f, 0.0f, 0.0f);//传感器的位置定义了虚拟传感器的6 DOF位置,它的原点是滚动=俯仰=偏航=0。
pcl::RangeImage::CoordinateFrame coordinate_frame = pcl::RangeImage::CAMERA_FRAME;//x朝向右,y向下,z轴是向前的,另一种选择是激光框架,x面向前方,y向左,z向上。
float noiseLevel = 0.00;//对于噪声,噪声是0,范围图像是使用普通的z缓冲区创建的
float minRange = 0.0f;// minRange >0 ,所有更近的点都将被忽略
int borderSize = 1;//边界大小>0,边界将会在裁剪时留下一个未被观察到的点的边界。
pcl::RangeImage rangeImage;
rangeImage.createFromPointCloud(pointcloud, angularResolution, maxAngleWidth, maxAngleHeight, sensorPose,
coordinate_frame, noiseLevel, minRange, borderSize);
//以图像的形式显示深度图像,深度值作为颜色显示
pcl::visualization::RangeImageVisualizer range_image_widget("Range image");
range_image_widget.showRangeImage(rangeImage);
std::cout << rangeImage << "\n";
std::cout << pointcloud << "\n";
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("viewer"));
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> colorhandler(pointcloud.makeShared(), 255, 0, 0);
viewer->addPointCloud(pointcloud.makeShared(), colorhandler,"cloud");
viewer->setBackgroundColor(1, 1, 1);
viewer->setCameraPosition(
0, 0, 0,
0, 0, -1,
0, 0, 0);
viewer->addCoordinateSystem(1, "cloud");
while (!viewer->wasStopped())
{
viewer->spinOnce();
}
system("pause");
return 0;
}
参考文献
[1]. http://pointclouds.org/documentation/tutorials/range_image_creation.php#range-image-creation