以下是hello-realsense代码的详细中文解释。
该代码利用D350进行中心点的距离检测。
代码运行结果:在室内最远检测距离达到7米
最近检测距离为0.169米,被测物体距离更近时,无法测量距离,显示0米。
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2019 Intel Corporation. All Rights Reserved.
#include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
#include <iostream> // for cout
//引入库
// Hello RealSense example demonstrates the basics of connecting to a RealSense device
// and taking advantage of depth data
//演示了连接到realsense设备的基础知识,并利用深度数据
int main(int argc, char * argv[]) try
//代码开始处先定义了main()函数,其中有两个参数:int argc和char* argv[]。这些参数通常用于从命令行接收输入参数。
//try关键字通常与catch一起使用,以捕获并处理异常情况。
{
// Create a Pipeline - this serves as a top-level API for streaming and processing frames
rs2::pipeline p;
//创建了一个rs2::pipeline对象p,它作为一个顶层API用于流式传输和处理帧。
// Configure and start the pipeline
p.start();
//通过调用p.start()方法配置并启动了该流水线。
//以上代码片段是使用Intel RealSense库中的代码示例,用于创建一个实时流处理程序。
while (true)
{
// Block program until frames arrive
rs2::frameset frames = p.wait_for_frames();
//wait_for_frames()是Intel RealSense库中用于等待获取一帧数据的函数。它会暂停程序的执行,直到接收到新的帧数据。
//p.wait_for_frames()被调用并将返回的帧数据存储在rs2::frameset类型的变量frames中。
// 通过使用wait_for_frames()函数,可以获取相机捕获的实时图像或其他传感器数据,并继续处理这些数据以完成应用程序逻辑。
// Try to get a frame of a depth image
rs2::depth_frame depth = frames.get_depth_frame();
//获取到的帧数据中获取深度图像的帧。
//frames.get_depth_frame() 返回一个 rs2::depth_frame 对象,它代表了一帧深度图像的数据。
// 通过将返回的 rs2::depth_frame 对象赋值给 depth 变量,可以在后续的代码中使用该变量来访问深度图像的相关属性和方法,如获取图像的宽度、高度、像素值等。
// Get the depth frame's dimensions
//这两行代码用于获取深度图像的宽度和高度。 通过这两个变量,你可以得到深度图像的尺寸信息
auto width = depth.get_width();
//depth.get_width() 返回深度图像的宽度,将其赋值给变量 width。
auto height = depth.get_height();
// depth.get_height() 返回深度图像的高度,将其赋值给变量 height。
// Query the distance from the camera to the object in the center of the image
float dist_to_center = depth.get_distance(width / 2, height / 2);
//这行代码是用来计算图像中心点的深度值对应的距离。depth是一个表示深度图像的对象,width和height分别是该深度图像的宽度和高度。
//width / 2和height / 2代表图像的中心点坐标。depth.get_distance()是一个函数调用,它接受一个像素坐标作为参数,并返回该像素对应的距离值。
//dist_to_center变量将会被赋值为图像中心点的深度值对应的距离。
// Print the distance 输出举例
std::cout << "The camera is facing an object " << dist_to_center << " meters away \r";
//使用输出语句std::cout来打印一条消息,在控制台输出一段文本,并在其中插入变量dist_to_center的值。
}
//这段代码是一个无限循环,用于获取RealSense相机的深度图像,并计算图像中央物体与相机之间的距离。
//调用p.wait_for_frames()函数来等待获取一帧图像数据。然后,通过调用frames.get_depth_frame()函数,从获取到的帧数据中提取深度图像的数据。
//接下来,获取深度图像的宽度和高度,并使用depth.get_distance()函数查询图像中央像素点处的物体与相机之间的距离。最后,使用std::cout将距离打印输出。
//这个循环将不断重复执行,每次获取新的深度图像,并计算并输出中央物体与相机之间的距离。
//以下是代码的异常处理,这段代码展示了如何捕获和处理异常,在发生异常时输出相应的错误信息,并返回适当的退出状态码。
return EXIT_SUCCESS;
}
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 rs2::error & e)块,它用于捕获特定类型rs2::error的异常。在捕获到该异常后,程序会执行catch语句块内的代码。
//在catch(const rs2::error & e)块中,通过调用e对象的成员函数获取错误信息,并使用std::cerr输出到标准错误流。
//具体来说,e.get_failed_function()返回导致异常的函数名,e.get_failed_args()返回对应的参数,e.what()返回异常的详细描述信息。
//然后,返回值设为EXIT_FAILURE表示程序运行失败。
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
//catch(const std::exception & e)块,它用于捕获所有类型为std::exception及其派生类的异常。在捕获到该异常后,程序同样会执行对应的catch语句块内的代码。
//在catch(const std::exception & e)块中,通过调用e.what()获取异常的详细描述信息,并使用std::cerr输出到标准错误流。然后,返回值同样设为EXIT_FAILURE。