RealSense学习入门:获取距离

277 阅读1分钟
#include "stdafx.h"
#include <iostream>
#include "librealsense2/rs.hpp"
// 等同于在链接器的输入中添加附加依赖项
#pragma comment(lib,"realsense2.lib")

using namespace rs2;
using namespace std;

void main() {
	//创建一个管道,作为流式处理和处理帧的顶级API
	pipeline p;

	//配置并启动管道
	p.start;

	while (true)
	{
		//暂停程序直到帧到达
		frameset frames = p.wait_for_frames();

		//尝试获取一帧深度图像
		depth_frame depth = frames.get_depth_frame();

		//一帧可能不包含深度,如果是,请继续,直到包含为止
		if (!depth)continue;
		
		//获得图像的高度和宽度,以像素为单位
		float width = depth.get_width();
		float heigh = depth.get_height();

		//查询从相机到图像中心对象的距离
		float dist_to_center = depth.get_distance(width / 2, heigh / 2);

		//输出距离
		cout << "The Camera is facing an object" << dist_to_center << "meters away \r";
	}
}