RealSenseR200版是当今最小的3D传感器。
这是一个利用RealSenseR200的小程序。
使用OpenCV2的Mat装载深度数据,在imshow时,鼠标左键单击窗口的深度图中一点,即可cout输出该点的深度。单位为mm。
#include "pxcsensemanager.h"
#include "pxcmetadata.h"#include "util_cmdline.h"
#include "util_render.h"
#include <opencv2\opencv.hpp>
using namespace cv;
using namespace std;
Mat depth;
Mat color;
//鼠标点击回调函数
void on_mouse(int event,int x, int y,int flags,void *ustc);
int wmain(int argc, WCHAR* argv[])
{
PXCSenseManager *psm = PXCSenseManager::CreateInstance();
if (!psm)
{
wprintf_s(L"Unabel to create the PXCSenseManager\n");
return 1;
}
psm->EnableStream(PXCCapture::STREAM_TYPE_COLOR);
psm->EnableStream(PXCCapture::STREAM_TYPE_DEPTH);
if (psm->Init() != PXC_STATUS_NO_ERROR)
{
wprintf_s(L"Unable to Init the PXCSenseManager\n");
return 2;
}
PXCImage *colorIm, *depthIm;
PXCImage::ImageData depth_data;
PXCImage::ImageData color_data;
PXCImage::ImageInfo depth_information;
PXCImage::ImageInfo color_information;
namedWindow("depth");
setMouseCallback("depth",on_mouse,0);
while (1)
{
if (psm->AcquireFrame(true) < PXC_STATUS_NO_ERROR) break;
PXCCapture::Sample *sample = psm->QuerySample();
colorIm = sample->color;
depthIm = sample->depth;
//获取深度和彩色帧
colorIm->AcquireAccess(PXCImage::ACCESS_READ,PXCImage::PIXEL_FORMAT_RGB24, &color_data);
depthIm->AcquireAccess(PXCImage::ACCESS_READ, &depth_data);//深度帧获取的格式也可调节
depth_information = sample->depth->QueryInfo();
color_information = sample->color->QueryInfo();
//利用构造函数,载入深度数据和彩色数据。最后步进不要忘了除以uchar的大小
depth=Mat(Size(depth_information.width,depth_information.height),CV_16UC1,(void*)depth_data.planes[0],depth_data.pitches[0]/sizeof(uchar));
color=Mat(Size(color_information.width,color_information.height),CV_8UC3, (void*)color_data.planes[0],color_data.pitches[0]/sizeof(uchar));
depthIm->ReleaseAccess(&depth_data);
colorIm->ReleaseAccess(&color_data);
psm->ReleaseFrame();
//imshow("color", color);
//CV_16UC1的图片在imshow时,原始数据将除以256,将最远探测距离设为z,那么imshow时可以乘以255*256/z,此处乘以15
imshow("depth",depth*15);
waitKey(1);
}
psm->Release();
}
void on_mouse(int event,int x, int y,int flags,void *ustc)
{
if(event==CV_EVENT_LBUTTONDOWN)
{
cout<<depth.at<ushort>(y,x)<<"mm"<<endl;
}
}