第1回Intel Realsense D 435 iの赤外線両目画像+深さ図を記録する

5499 ワード

初めてのIntel Realsenseの開発の道を記録する
         D435i  ,        ,  D435i D435    ,        imu(D435i imu),      (        ),        ,  1999(   ).
        ,           ,       ,**                  include      lib   **,       。
                ,      Viewer                  ,              Opencv     IR      。        。(      ,           ,           ,             ,               ,      )

    :
#include 
#include 
#include 
#include 
#include 
#include
#include
#include
#include
#include
#include

using namespace cv;
using namespace std;
using namespace rs2;

const int width = 1280;
const int height = 720;
const int fps = 30;
//const int fps = 60;



int main()
{
	//Initialization
	//Depth 
	const char* depth_win = "depth_Image";
	namedWindow(depth_win, WINDOW_AUTOSIZE);
	//IR Left & Right
	const char* left_win = "left_Image";
	namedWindow(left_win, WINDOW_AUTOSIZE);
	const char* right_win = "right_Image";
	namedWindow(right_win, WINDOW_AUTOSIZE);
	//Color
	const char* color_win = "color_Image";
	namedWindow(color_win, WINDOW_AUTOSIZE); 

	char LName[100];//left 
	char RName[100];//right 
	char DName[100];//depth
	char CName[100];//color
	long long  i = 0;//counter

	//Pipeline
	rs2::pipeline pipe;
	rs2::config pipe_config;
	pipe_config.enable_stream(RS2_STREAM_DEPTH, width, height, RS2_FORMAT_Z16, fps);
	pipe_config.enable_stream(RS2_STREAM_INFRARED, 1, width, height, RS2_FORMAT_Y8, fps);
	pipe_config.enable_stream(RS2_STREAM_INFRARED, 2, width, height, RS2_FORMAT_Y8, fps);
	pipe_config.enable_stream(RS2_STREAM_COLOR , width, height, RS2_FORMAT_BGR8, fps);

	rs2::pipeline_profile profile = pipe.start(pipe_config);

	//stream
	auto depth_stream = profile.get_stream(RS2_STREAM_DEPTH).as<:video_stream_profile>();

	while (cvGetWindowHandle(depth_win)&& cvGetWindowHandle(right_win)&& cvGetWindowHandle(left_win)&& cvGetWindowHandle(color_win)) // Application still alive?
	{
		//            
		rs2::frameset frameset = pipe.wait_for_frames();
		//        
		frame depth_frame = frameset.get_depth_frame();
		video_frame ir_frame_left = frameset.get_infrared_frame(1);
		video_frame ir_frame_right = frameset.get_infrared_frame(2);
		frame color_frame = frameset.get_color_frame();

		Mat dMat_left(Size(width, height), CV_8UC1, (void*)ir_frame_left.get_data());
		Mat dMat_right(Size(width, height), CV_8UC1, (void*)ir_frame_right.get_data());
		Mat depth_image(Size(width, height),CV_16U, (void*)depth_frame.get_data(), Mat::AUTO_STEP);
		Mat color_image(Size(width, height), CV_8UC3, (void*)color_frame.get_data(), Mat::AUTO_STEP);

		imshow(left_win,dMat_left);
		imshow(right_win,dMat_right);
		imshow(depth_win, depth_image);
		imshow(color_win, color_image);
		/*waitKey(1);*/
		char c = waitKey(1);
		if (c == 'p')
		{
			sprintf_s(LName, "F:\\cpppractice\\D435iimshow\\x64\\Debug\\left_eye\\%d.png", i);
			imwrite(LName, dMat_left);
			sprintf_s(RName, "F:\\cpppractice\\D435iimshow\\x64\\Debug\\right_eye\\%d.png", i);
			imwrite(RName, dMat_right);
			sprintf_s(DName, "F:\\cpppractice\\D435iimshow\\x64\\Debug\\depth\\%d.png", i);
			imwrite(DName, depth_image);
			sprintf_s(CName, "F:\\cpppractice\\D435iimshow\\x64\\Debug\\color\\%d.png", i);
			imwrite(CName, color_image);
			i++;
		}
		else if (c == 'q')
			break;
		/*sprintf_s(LName, "F:\\cpppractice\\D435iimshow\\x64\\Debug\\left_eye\\%d.png", i);
		imwrite(LName, dMat_left);
		sprintf_s(RName, "F:\\cpppractice\\D435iimshow\\x64\\Debug\\right_eye\\%d.png", i);
		imwrite(RName, dMat_right);
		sprintf_s(DName, "F:\\cpppractice\\D435iimshow\\x64\\Debug\\depth\\%d.png", i);
		imwrite(DName, depth_image);
		sprintf_s(CName, "F:\\cpppractice\\D435iimshow\\x64\\Debug\\color\\%d.png", i);
		imwrite(CName, color_image);
		i++;*/
	}
	return 0;
}



ここでlinuxを使った穴を補足します.私はlinuxを使っている間にopencvとrealsenseはいろいろなことができません.それから少しだけ調整するしかありません.実は主にいろいろなライブラリがリンクされていないので、Cmakelistsで工夫すればいいのです.Cmakelistsは以下のようにします.
cmake_minimum_required(VERSION 3.1)
project(pic_cap)
add_definitions(-std=c++11)
add_executable(pic_cap main.cpp)
aux_source_directory(. SRC_LIST)
#add_executable(${PROJECT_NAME} ${SRC_LIST})
find_package( OpenCV 3 REQUIRED )
#find_package( REALSENSE 2 REQUIRED )
install(TARGETS pic_cap RUNTIME DESTINATION bin)
target_link_libraries(pic_cap ${OpenCV_LIBS})
target_link_libraries(pic_cap ${DEPENDENCIES})
#target_link_libraries(pic_cap ${REALSENSE2_LIBS})
include_directories(/home/librealsense/include)
set(DEPENDENCIES realsense2 )
target_link_libraries(pic_cap ${DEPENDENCIES})

注意:Realsense D 435 iは16ビットの画像を直接読み出しますが、私たちが使用しているopencvのMatは8ビットでよく使われています.そのため、画像変換の面で(Mat depthimageconvert(Mat input)という関数)C++言語では、2:<1>直接depth_からstreamで読み出した深さマップの画素値範囲は0-65535であり、最終的に使用する画像画素の階調値範囲は0-255であるため、I(i,j)/255またはI(i,j)/65535*255を直接使用すると(実は両者の原理は同じである)、結果は黒であり、係数を乗じなければならない.画像全体の階調値を上げてこそ、可視化の結果が得られる.しかし、そうすれば元のデータが真実ではない.<2>realsense D 435 iデフォルトのdepth画像は遠白近黒ですが、私たちがよく使うのは遠黒近白で、両者には良い変換方法はありません(まだ見つかっていませんが、255-I(i,j)を直接使うことはできませんね)参考文献:[1]https://blog.csdn.net/xuyuhua1985/article/details/80108597