realsense 获取深度、彩色相机内参外参

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

#include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
#include <opencv2/opencv.hpp>   // Include OpenCV API

#include <fstream>              // File IO
#include <iostream>             // Terminal IO
#include <sstream>              // Stringstreams
#include<algorithm>
#include <direct.h>
using namespace std;
using namespace cv;

#define RS_WIDTH 640//1280
#define RS_HEIGHT 480//720
#define RS_FPS 30
bool lButtonDown = false;
static Point p;
static Rect selection;
bool selected = false;
void onMouse(int event, int x, int y, int flags, void* depth)
{
	if (lButtonDown)
	{
		selection.x = MIN(x, p.x);
		selection.y = MIN(y, p.y);
		selection.width = std::abs(x - p.x);
		selection.height = std::abs(y - p.y);
	}
	if (event == EVENT_LBUTTONDOWN)//左键按下,读取初始坐标 
	{
		lButtonDown = true;
		p.x = x;
		p.y = y;
		selection = Rect(x, y, 0, 0);
	}
	if (event == EVENT_LBUTTONUP)
	{
		lButtonDown = false;
		if (selection.width > 0 && selection.height > 0)
		{
			selected = true;
		}
	}
}
int main(int argc, char* argv[]) try
{
	// judge whether devices is exist or not
	rs2::context ctx;
	auto list = ctx.query_devices(); // Get a snapshot of currently connected devices
	if (list.size() == 0)
		throw std::runtime_error("No device detected. Is it plugged in?");
	rs2::device dev = list.front();

	// Declare RealSense pipeline, encapsulating the actual device and sensors
	rs2::pipeline pipe;

	rs2::config cfg;//创建一个以非默认配置的配置用来配置管道
	 //Add desired streams to configuration
	//彩色和深度可以不配置,按默认值输出;红外图必须进行配置,否则无法显示
	cfg.enable_stream(RS2_STREAM_COLOR, RS_WIDTH, RS_HEIGHT, RS2_FORMAT_BGR8, RS_FPS);//向配置添加所需的流
	cfg.enable_stream(RS2_STREAM_DEPTH, RS_WIDTH, RS_HEIGHT, RS2_FORMAT_Z16, RS_FPS);

	pipe.start(cfg);//指示管道使用所请求的配置启动流

	rs2::frame depth;
	rs2::stream_profile depth_profile;
	rs2::stream_profile color_profile;

	while (!depth_profile || !color_profile)
	{
		rs2::frameset frames = pipe.wait_for_frames(); // Wait for next set of frames from the camera
		
		if (frames.size() == 1)
		{
			if (frames.get_profile().stream_type() == RS2_STREAM_DEPTH)
			{
				depth = frames.get_depth_frame();
				depth_profile = depth.get_profile();
			}
			else
			{
				color_profile = frames.get_color_frame().get_profile();
			}
		}
		else
		{
			depth = frames.get_depth_frame();
			depth_profile = depth.get_profile();
			color_profile = frames.get_color_frame().get_profile();
		}
	}

	rs2_intrinsics depth_intrin = depth_profile.as<rs2::video_stream_profile>().get_intrinsics();
	rs2_intrinsics color_intrin = color_profile.as<rs2::video_stream_profile>().get_intrinsics();
	rs2_extrinsics depth_extrin_to_color = depth_profile.as<rs2::video_stream_profile>().get_extrinsics_to(color_profile);
	rs2_extrinsics color_extrin_to_depth = color_profile.as<rs2::video_stream_profile>().get_extrinsics_to(depth_profile);

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值