使用奥比中光Astra Pro捕获深度图与彩色图代码

#include <astra/astra.hpp>
#include <opencv2/opencv.hpp>

int main(int argc, char** argv)
{
	//初始化Astra SDK
	astra::initialize();
	astra::StreamSet streamset;
	astra::StreamReader reader = streamset.create_reader();

	//获取深度流
	auto depthStream = reader.stream<astra::DepthStream>();

	//配置深度流分辨率、格式和帧率
	astra::ImageStreamMode depthMode;
	depthMode.set_width(640);
	depthMode.set_height(480);
	depthMode.set_pixel_format(astra_pixel_formats::ASTRA_PIXEL_FORMAT_DEPTH_MM);
	depthMode.set_fps(30);

	//设置深度流模式并打开深度流
	depthStream.set_mode(depthMode);
	depthStream.start();

	//获取彩色流
	auto colorStream = reader.stream<astra::ColorStream>();

	//配置彩色流的分辨率、格式和帧率
	astra::ImageStreamMode colorMode;
	colorMode.set_width(640);
	colorMode.set_height(480);
	colorMode.set_pixel_format(astra_pixel_formats::ASTRA_PIXEL_FORMAT_RGB888);
	colorMode.set_fps(30);

	//设置彩色流模式并打开彩色流

	colorStream.set_mode(colorMode);
	colorStream.start();

	while (true)
	{
		//更新数据流
		astra_update();
		if (!reader.has_new_frame())
		{
			continue;
		}
		//获取最后一帧
		auto frame = reader.get_latest_frame(100);

		//获取深度帧数据
		auto depthFrame = frame.get<astra::DepthFrame>();
		auto depth = depthFrame.data();

		//获取深度帧的宽高
		auto depthWidth = depthFrame.width();
		auto depthHeight = depthFrame.height();

		//处理并渲染深度帧数据
		if (depthFrame.is_valid())
		{
			cv::Mat rawMat(depthHeight, depthWidth, CV_16UC1, (void*)depth);
			cv::Mat depthMat;
			rawMat.convertTo(depthMat, CV_8UC1);

			cv::imshow("Depth Viewer", depthMat);
		}

		//获取彩色帧数据
		auto colorFrame = frame.get<astra::ColorFrame>();
		auto color = colorFrame.data();

		//获取彩色帧的宽高
		auto colorWidth = colorFrame.width();
		auto colorHeight = colorFrame.height();

		//处理并渲染彩色帧数据
		if (colorFrame.is_valid())
		{
			cv::Mat rawMat(colorHeight, colorWidth, CV_8UC3, (void*)color);
			cv::Mat colorMat;
			cv::cvtColor(rawMat, colorMat, cv::COLOR_BGR2RGB);
			cv::imshow("Color Viewer", colorMat);
		}

		//按ESC退出
		int key = cv::waitKey(100);
		if (key == 27)
			break;
	}
	//释放Astra SDK资源
	astra::terminate();
}

在这里插入图片描述

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐