本部落格适用于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圖像。