Realsense SR300 和 R200 提取深度图像并保存

本博客适用于SR300和R200的深度图像和RGB图像的提取。

OpenCV的安装

首先要安装opencv,在这里不做多讲,网上有很多教程.

SDK的安装

Realsense SR300 和 R200 使用的SDK版本是相同的,都是2016 R2(版本很重要),我使用的VS版本是2015.

提取深度图的程序代码

下面为提取深度图像的代码.

#include <pxcsensemanager.h>  
#include <pxcsession.h>  
#include "util_render.h"  
#include <iostream>  
#include <string>  
#include <stdio.h>  
#include <opencv2\opencv.hpp>

#define WIDTH 640  //SR300和R200的这两个值是不同的,此处的值是R200
#define HEIGHT 480  

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
	UtilRender *renderColor = new UtilRender(L"COLOR_STREAM");
	UtilRender *renderDepth = new UtilRender(L"DEPTH_STREAM");

	PXCSenseManager *psm = 0;
	psm = PXCSenseManager::CreateInstance();
	if (!psm)
	{
		wprintf_s(L"Unabel to create the PXCSenseManager\n");
		return 1;
	}
	pxcStatus sts;

	psm->EnableStream(PXCCapture::STREAM_TYPE_COLOR, WIDTH, HEIGHT);

	psm->EnableStream(PXCCapture::STREAM_TYPE_DEPTH, WIDTH, HEIGHT);

	sts = psm->Init();
	if (sts != PXC_STATUS_NO_ERROR)
	{
		wprintf_s(L"Unabel to Initializes the pipeline\n");
		return 2;
	}
	PXCImage *colorIm, *depthIm;
	PXCImage::ImageData depth_data, color_data;
	PXCImage::ImageInfo depth_info, color_info;

	for (int cframe = 0; cframe < 1000; cframe++)
	{
		if (psm->AcquireFrame(true) < PXC_STATUS_NO_ERROR) break;
		PXCCapture::Sample *sample = psm->QuerySample();
		colorIm = sample->color;
		depthIm = sample->depth;

		if (colorIm->AcquireAccess(PXCImage::ACCESS_READ, PXCImage::PIXEL_FORMAT_RGB24, &color_data) < PXC_STATUS_NO_ERROR)
			wprintf_s(L"Color maps are not acquired normally.\n");
		if (depthIm->AcquireAccess(PXCImage::ACCESS_READ, &depth_data) < PXC_STATUS_NO_ERROR)
			wprintf_s(L"Depth maps are not acquired normally.\n");

		depth_info = sample->depth->QueryInfo();
		color_info = sample->color->QueryInfo();

		Mat depth(Size(depth_info.width, depth_info.height), CV_16UC1, (void*)depth_data.planes[0], depth_data.pitches[0] / sizeof(uchar));
		Mat color(Size(color_info.width, color_info.height), CV_8UC3, (void*)color_data.planes[0], color_data.pitches[0] / sizeof(uchar));

		if (!renderColor->RenderFrame(colorIm)) break;
		if (!renderDepth->RenderFrame(depthIm)) break;

		psm->ReleaseFrame();

		stringstream ss;
		string str;
		ss << (cframe);
		str = ss.str();

		vector<int> compressiong_params;
		compressiong_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
		compressiong_params.push_back(0);

		imwrite("Your path" + str + ".color.jpg", color, compressiong_params);
		imwrite("Your path" + str + ".depth.png", depth * 15, compressiong_params);
	}
	psm->Release();
}

参考于SCUT_Arucee大神
用VS2015打开上面代码,然后在属性里面将OpenCV目录和SDK的包含目录和库目录添加,运行之后就可以得到相应的扫描的情况。并且在我们设定的path下得到保存的RGB图像和Depth图像。
在这里插入图片描述
Realsense SR300 和 R200 提取深度图像并保存_第1张图片
Realsense SR300 和 R200 提取深度图像并保存_第2张图片
Realsense SR300 和 R200 提取深度图像并保存_第3张图片

你可能感兴趣的:(realsense,Depth)