intel realsense深度相机拍摄的深度图获取的点云怎么转换为pcl点云类型
(暂定深度图获取转化为点云的方法为相机API中的calculate方法,有更好的点云方法可以用其他)
(vs2019+PCL+c++)
该回答引用GPTᴼᴾᴱᴺᴬᴵ
要将Intel RealSense深度相机拍摄的深度图转换为PCL点云类型,需要执行以下步骤:
以下是示例代码:
#include <iostream>
#include <pcl/point_types.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/io/pcd_io.h>
using namespace std;
pcl::PointCloud<pcl::PointXYZ>::Ptr depthToPointCloud(rs2::depth_frame depth, rs2::intrinsics intrin)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());
float depthScale = rs2::context().get_device().first<rs2::depth_sensor>().get_depth_scale();
// 获取深度图像的宽高
int w = depth.get_width();
int h = depth.get_height();
// 遍历深度图像的像素
for (int dy = 0; dy < h; ++dy)
{
for (int dx = 0; dx < w; ++dx)
{
// 获取深度图像的距离值
float depthValue = depth.get_distance(dx, dy);
// 跳过深度值为零的点
if (depthValue == 0)
{
continue;
}
// 计算点云坐标
pcl::PointXYZ p;
p.x = (dx - intrin.ppx) / intrin.fx * depthValue;
p.y = (dy - intrin.ppy) / intrin.fy * depthValue;
p.z = depthValue;
// 将点云坐标添加到点云对象中
cloud->push_back(p);
}
}
return cloud;
}
int main()
{
// 获取实时深度图像和相机内参
rs2::pipeline pipeline;
pipeline.start();
rs2::frameset frames = pipeline.wait_for_frames();
rs2::depth_frame depth = frames.get_depth_frame();
rs2::video_frame color = frames.get_color_frame();
rs2_intrinsics intrin = depth.get_profile().as<rs2::video_stream_profile>().get_intrinsics();
// 将深度图像转换为点云
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud = depthToPointCloud(depth, intrin);
// 将点云保存为PCD文件
pcl::io::savePCDFile("pointcloud.pcd", *cloud);
// 可视化点云
pcl::visualization::CloudViewer viewer("Point Cloud Viewer");
viewer.showCloud(cloud);
while (!viewer.wasStopped())
{
}
return 0;
}