版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SCUT_Arucee/article/details/50956654
前面提到过一种利用realsense提取彩色和深度视频流并实现pxcimage到mat类型转化的方法,具体可见Realsense(一)提取彩色和深度视频流并实现pxcimage到mat。
下面的代码只是在实现pxcimage到mat类型转化上有所不同,利用realsense本身提取彩色和深度视频流的方法不变。
#include <pxcsensemanager.h>
#include <pxcsession.h>
#include "util_render.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <opencv2\opencv.hpp>
#include <windows.h>
#define WIDTH 640
#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;
}
Mat img = Mat(HEIGHT, WIDTH, CV_16UC1);
ushort *p;
PXCImage *colorIm, *depthIm;
PXCImage::ImageData depth_data, color_data;
PXCImage::ImageInfo depth_info, color_info;
while (psm->AcquireFrame(true) >= PXC_STATUS_NO_ERROR)
{
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"未正常获取彩色图\n");
if (depthIm->AcquireAccess(PXCImage::ACCESS_READ, &depth_data) < PXC_STATUS_NO_ERROR)
wprintf_s(L"未正常获取深度图\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));
ushort *dpixels = (ushort*)depth_data.planes[0];
int dpitch = depth_data.pitches[0] / sizeof(ushort);
for (int y = 0; y < (int)depth_info.height; y++)
{
for (int x = 0; x < (int)depth_info.width; x++)
{
ushort d = dpixels[y*dpitch + x];
p = img.ptr<ushort>(y);
//距离在0.2m至1.2m之间
if (d>0)
p[x] = 255 - 0.255*(d - 200);
else
p[x] = 0;
}
}
Mat output;
img.convertTo(output, CV_8UC1, 1);
depthIm->ReleaseAccess(&depth_data);
colorIm->ReleaseAccess(&color_data);
if (!renderColor->RenderFrame(colorIm)) break;
if (!renderDepth->RenderFrame(depthIm)) break;
psm->ReleaseFrame();
imshow("depth picture", output);
waitKey(1);
imshow("color", color);
waitKey(1);
}
psm->Release();
system("pause");
}
更具体的说,彩色图的转化没有变,只是深度图的转化代码方案不同。
下面是效果图:
上一篇博客的效果图对比如下:
可以看到,新方案在深度图的pxcimage到mat的转化上似乎表现更好一些,但无论是哪一种,转化为mat类型之后,深度图的直观表现都没有realsense本身的pxcimage好,图中可明显看出鼻子区域的深度表现。