函数原型
RangeImage::createFromPointCloud (const PointCloudType& point_cloud,
float angular_resolution,
float max_angle_width,float max_angle_height,
const Eigen::Affine3f& sensor_pose, RangeImage::CoordinateFrame coordinate_frame,
float noise_level,float min_range,
int border_size)
参数说明:
CAMERA_FRAM
E说明系统的X轴是向右的、Y轴是向下的、Z轴是向前的。如果参数值是LASER_FRAME
,其X轴向前、Y轴向左、Z轴向上。创建 矩形点云
#include
#include
#include
#include
int main(int argc, char** argv) {
pcl::PointCloud::Ptr pointCloudPtr(new pcl::PointCloud);
pcl::PointCloud& pointCloud = *pointCloudPtr;
// 创建一个矩形形状的点云
// 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
// 根据之前得到的点云图,通过1deg的分辨率生成深度图。
float angularResolution = (float)(1.0f * (M_PI / 180.0f));// 弧度1°
float maxAngleWidth = (float)(360.0f * (M_PI / 180.0f)); // 弧度360°
float maxAngleHeight = (float)(180.0f * (M_PI / 180.0f)); // 弧度180°
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::Ptr rangeImage_ptr(new pcl::RangeImage);
pcl::RangeImage& rangeImage = *rangeImage_ptr;
rangeImage.createFromPointCloud(pointCloud, angularResolution, maxAngleWidth, maxAngleHeight,
sensorPose, coordinate_frame, noiseLevel, minRange, borderSize);
std::cout << rangeImage << "\n";
// 添加原始点云
pcl::visualization::PCLVisualizer viewer("3D Viewer");
viewer.setBackgroundColor(1, 1, 1);
pcl::visualization::PointCloudColorHandlerCustom org_image_color_handler(pointCloudPtr, 255, 100, 0);
viewer.addPointCloud(pointCloudPtr, org_image_color_handler, "orginal image");
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "orginal image");
viewer.initCameraParameters();
viewer.addCoordinateSystem(1.0);
// 添加深度图点云
boost::shared_ptr viewer1(new pcl::visualization::PCLVisualizer("RangeImage Viewer"));
viewer1->setBackgroundColor(0, 0, 0); //设置背景颜色为黑色
pcl::visualization::PointCloudColorHandlerGenericField range_image_color_handler(rangeImage_ptr, "z");
viewer1->addPointCloud(rangeImage_ptr, range_image_color_handler, "range image");
viewer1->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, "range image");
viewer1->initCameraParameters();
while (!viewer.wasStopped())
{
viewer.spinOnce();
}
return (0);
}
运行效果:
Explanation :
pcl::PointCloud::Ptr pointCloudPtr(new pcl::PointCloud);
pcl::PointCloud& pointCloud = *pointCloudPtr;
这段代码创建了一个指向pcl::PointCloudpcl::PointXYZ类型的指针pointCloudPtr,并通过关键字new实例化了一个新的PointCloud对象。然后,通过将指针解引用并赋值给pointCloud变量,将其引用指向了pointCloudPtr所指向的PointCloud对象。
pcl::RangeImage::Ptr rangeImage_ptr(new pcl::RangeImage);
pcl::RangeImage& rangeImage = *rangeImage_ptr;
这段代码创建了一个指向pcl::RangeImage类型的指针rangeImage_ptr,并通过关键字new实例化了一个新的RangeImage对象。然后,通过将指针解引用并赋值给rangeImage变量,将其引用指向了rangeImage_ptr所指向的RangeImage对象。
viewer.initCameraParameters();
通过调用viewer.initCameraParameters()方法,初始化了相机参数,即设置了默认相机姿态和投影参数,以便在可视化中正确显示点云或三维对象。
加载已有的点云数据
#include
#include
#include
#include
#include
#include
#include //保存深度图像
#include //深度图像可视化
#include //点云可视化
int main(int argc, char** argv) {
pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
// 从PCD文件中加载点云数据
if (pcl::io::loadPCDFile("../data/DKdata2.pcd", *cloud) == -1) {
PCL_ERROR("无法读取 PCD 文件!\n");
return -1;
}
// 创建深度图参数
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(135.75f, -99.18f, 52.64f);
pcl::RangeImage::CoordinateFrame coordinate_frame = pcl::RangeImage::CAMERA_FRAME;
float noiseLevel = 0.00;
float minRange = 0.0f;
int borderSize = 1;
pcl::RangeImage::Ptr rangeImage_ptr(new pcl::RangeImage);
pcl::RangeImage& rangeImage = *rangeImage_ptr;
//pcl::RangeImage rangeImage;
rangeImage.createFromPointCloud(*cloud, angularResolution, maxAngleWidth, maxAngleHeight,
sensorPose, coordinate_frame, noiseLevel, minRange, borderSize);
boost::shared_ptrviewer(new pcl::visualization::PCLVisualizer("RangeImage Viewer"));
viewer->setBackgroundColor(0, 0, 0); //设置背景颜色为黑色
pcl::visualization::PointCloudColorHandlerGenericField range_image_color_handler(rangeImage_ptr, "z");
viewer->addPointCloud(rangeImage_ptr, range_image_color_handler, "range image");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, "range image");
viewer->initCameraParameters();
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
}
return 0;
}
运行效果:
参考:点云转深度图:转换,保存,可视化