rs-imshow Sample

211 阅读1分钟

本次实例用于Intel RealSense 摄像头与OpenCV集成的"hello-world"代码段。该示例将打开一个OpenCV UI窗口,并向其渲染彩色深度流。

#include<librealsense2/rs.hpp>
#include<iostream>
#include<opencv2/opencv.hpp>
#include<string>

int main(int aargc, char * argc[])try
{
	//声明深度着色器,用于深度数据的漂亮可视化 
	//rs2:	RealSense父类
	//colorizer:	着色器,用于实现深度数据的漂亮可视化
	//color_map:	着色器实例化对象
	rs2::colorizer color_map;

	//pipeline:	封装实际设备和传感器的类
	//pipe:实例化的对象
	rs2::pipeline pipe;
	//使用默认的配置启动管道
	pipe.start();

	//
	using namespace cv;
	//定义新建的窗口名称
	const auto window_name = "Display Image";
	//新建窗口
	namedWindow(window_name, WINDOW_AUTOSIZE);

	//getWindowProperty():获取窗口属性方法
	while (waitKey(1)<0 && getWindowProperty(window_name,WND_PROP_AUTOSIZE)>= 0)
	{
		//
		rs2::frameset data = pipe.wait_for_frames();

		rs2::frame depth = data.get_depth_frame().apply_filter(color_map);

		//查询框架大小(宽度和高度) 
		const int w = depth.as<rs2::video_frame>().get_width();
		const int h = depth.as<rs2::video_frame>().get_height();

		//根据着色深度数据创建大小为(w,h)的OpenCV矩阵 
		Mat image(Size(w, h), CV_8UC3, (void*)depth.get_data(), Mat::AUTO_STEP);

		//用新数据更新窗口
		imshow(window_name, image);
	}

	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;
}