D435i 深度摄像头的第一个c++工程,hello world

玩了些时间的D435i 摄像头, c++ 的 rs-example程序,python 的代码图像深度实时显示,我就开始做一个自己的c++工程,hello world。

我是看了一些realsense 代码,想自己测试下,觉得需要建立一个自己的工程。本文主要介绍工程的设置方面。代码呢,基本是复制的rs-example 里的helloworld 代码。代码简单,功能是显示采集图像的中心点的深度。

环境是visual studio 2017 的免费版本,新建一个工程,取名hello-rs,windows 的控制台程序,就有一个hello-rs.cpp 文件,如果编译执行,就是打印一个hello world。

去掉所有的代码内容,只保留#include "pch.h"

然后复制rs-example中 hello world 工程中的代码,当然也可以这里copy,如下:

#include "pch.h"

// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2019 Intel Corporation. All Rights Reserved.

#include  // Include RealSense Cross Platform API
#include              // for cout

// Hello RealSense example demonstrates the basics of connecting to a RealSense device
// and taking advantage of depth data
int main(int argc, char * argv[]) try
{
	// Create a Pipeline - this serves as a top-level API for streaming and processing frames
	rs2::pipeline p;

	// Configure and start the pipeline
	p.start();

	while (true)
	{
		// Block program until frames arrive
		rs2::frameset frames = p.wait_for_frames();

		// Try to get a frame of a depth image
		rs2::depth_frame depth = frames.get_depth_frame();

		// Get the depth frame's dimensions
		float width = depth.get_width();
		float height = depth.get_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);

		// Print the distance
		std::cout << "The camera is facing an object " << dist_to_center << " meters away \r";
	}

	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 std::exception& e)
{
	std::cerr << e.what() << std::endl;
	return EXIT_FAILURE;
}

尝试编译,有很多include文件都没有,需要设置

工程属性->配置属性->VC++ 目录,在include 里添加 realsense 的include 目录,我的是:

C:\Program Files %28x86%29\Intel RealSense SDK 2.0\include

然后是lib 目录,我的是:

C:\Program Files %28x86%29\Intel RealSense SDK 2.0\lib\x86

工程属性->配置属性->链接->输入->:附加依赖项,添加realsense2.lib

这个文件就在lib 目录C:\Program Files %28x86%29\Intel RealSense SDK 2.0\lib\x86下面。

编译链接,就可以运行了,我的显示:The camera is facing an object  0.391 meters away 
 这个0.391 就是摄像机到他图像的中心的距离,位置不一样,当然数据不一样,我们就测量了一个深度。

就是

float dist_to_center = depth.get_distance(width / 2, height / 2);

这一句的结果。

其他代码就不分析了。

 

 

你可能感兴趣的:(c++)