点云realsense
时间: 2024-12-26 11:22:15 浏览: 8
### 使用 Realsense 获取并处理点云
为了利用 Intel RealSense 深度相机(如D435i型号)来获取和保存点云数据,可以采用 librealsense 库中的 `rs2::pointcloud` 类创建点云对象,并通过调用相应的方法计算实际的点云数据[^1]。
下面是一个简单的 C++ 示例程序用于展示如何从 RealSense 设备读取深度帧并转换成点云:
```cpp
#include <librealsense2/rs.hpp> // Include RealSense Cross Platform API Headers
#include <iostream>
#include "pcl/visualization/cloud_viewer.h"
int main() try {
rs2::pipeline pipe;
auto config = rs2::config();
// Configure streams to enable
config.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);
pipe.start(config);
using namespace std;
cout << "Press ESC key to stop capturing..." << endl;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
rs2::pointcloud pc; // Create pointcloud object
while (true) {
// Wait for next set of frames from the camera
rs2::frameset data = pipe.wait_for_frames();
// Generate the pointcloud and texture mappings
rs2::points points = pc.calculate(data.get_depth_frame());
// Get vertices as vector of float triples
const auto verts = points.get_vertices();
// Clear previous content before adding new points.
cloud->clear();
// Add each vertex into PCL PointCloud structure
for(auto& vert : verts){
if (!std::isnan(vert.x)){
cloud->push_back(pcl::PointXYZ(vert.x, vert.y, vert.z));
}
}
// Visualization with PCL viewer
pcl::visualization::CloudViewer viewer("RealSense Point Cloud Viewer");
viewer.showCloud(cloud);
int k = cv::waitKey(1); // Use OpenCV's waitKey function here just for keyboard input handling.
// Break loop on escape key press
if(k == 27)
break;
}
}
catch (const rs2::error & e){
std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
return EXIT_FAILURE;
}
catch (const std::exception& e){
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
```
上述代码片段展示了怎样设置管道以接收来自 RealSense 的深度流,并使用 `rs2::pointcloud` 对象将这些深度信息转化为三维空间坐标系下的点集。最后借助 PCL 库实现了基本可视化功能。
阅读全文