RealSense 开发(一):深度图像获取

RealSense 开发(一):深度图像获取

安装与配置VS工程

新建VS工程,项目属性
C++附加目录添加:
$(RSSDK_DIR)/include;$(RSSDK_DIR)/sample/common/include;
链接器附加库目录添加:
$(RSSDK_DIR)/lib/$(PlatformName);
$(RSSDK_DIR)/sample/common/lib/$(PlatformName)/$(PlatformToolset);

Release模式附加依赖项添加:
libpxc.lib
libpxcutils.lib

Debug模式附加依赖项添加:
libpxc_d.lib;
libpxcutils_d.lib;

至此,配置完成

代码

添加main.cpp,内容如下:

#include "stdio.h"
#include "pxcsensemanager.h"
#include "pxcsession.h"
#include "util_render.h"
#include "opencv2/opencv.hpp"
#include 
using namespace std;
using namespace cv;

//灰度图转伪彩图
void f_gray2color(Mat gray_mat,Mat color_mat)
{
    uchar* p=nullptr;
    Vec3b* q=nullptr;
    int height=gray_mat.rows;
    int width=gray_mat.cols;
    for(int i=0;ifor(int j=0;j(i);
            q=color_mat.ptr(i);
            if(p[j]==0)
            {q[j][0]=0;q[j][1]=0;q[j][2]=0;}
            else
            {
                q[j][0]=255-p[j];
                q[j][1]=128-abs(128-p[j]);
                q[j][2]=p[j];
            }
        }
    }
    return;
}
int main()
{ 
    //Mat image = imread("1.jpg");
    //namedWindow("pic_viewer", CV_WINDOW_NORMAL);
    //setWindowProperty("pic_viewer", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
    //imshow("pic_viewer", image);
    //waitKey(1000); //每隔1秒显示  
    //destroyAllWindows();


    UtilRender   *renderDepth=new UtilRender(L"DEPTH_STREAM");
    Mat framepic(480,640,CV_16UC1);//深度帧
    Mat preFramePic(480,640,CV_16UC1);//深度帧
    int frameCount = 0;

    PXCSenseManager *sm=PXCSenseManager::CreateInstance();

    PXCVideoModule::DataDesc desc={};
    desc.deviceInfo.streams = PXCCapture::STREAM_TYPE_COLOR | PXCCapture::STREAM_TYPE_DEPTH;

    //sm->EnableStream(PXCCapture::STREAM_TYPE_DEPTH,640,480);
    sm->EnableStreams(&desc);

    pxcStatus sts=sm->Init();
    if (sts"Failed to locate any video stream(s)\n");
        sm->Release();
        return sts;
    }
    if(sm->AcquireFrame(true)"未正常打开"<*sample;
    PXCImage *depthpic;

    PXCImage:: ImageData depthImageData;
    PXCImage:: ImageData colorImageData;
    PXCImage::ImageInfo dinfo;

    while (sm->AcquireFrame(true)>=PXC_STATUS_NO_ERROR) 
    {

        sample=sm->QuerySample();
        depthpic=sample->depth;


        if (depthpic->AcquireAccess(PXCImage::ACCESS_READ,&depthImageData)"未正常获取深度图像"<QueryInfo();

        ushort* dpixels=(ushort*)depthImageData.planes[0];
        int dpitch=depthImageData.pitches[0]/sizeof(ushort);


        for (int y=0;y<(int)dinfo.height;y++)
        {
            ushort* p=framepic.ptr(y);
            for(int x=0;x<(int)dinfo.width;x++)
            {
                ushort d=dpixels[y*dpitch+x];
                p[x]=d;
                //距离在0.2m1.2m之间
                //if (d>0)
                //  p[x]=255-0.255*(d-200);
                //else
                //  p[x]=0;    
            }
        }


        //Mat output;

        //framepic.convertTo(output,CV_8UC1,1);
        Mat color(480,640,CV_8UC3,Scalar(255,255,255));
        //f_gray2color(output,color);
        if (frameCount>1)
        {
            for (int y=0;y<(int)dinfo.height;y++)
            {
                ushort* p=framepic.ptr(y);
                ushort* pp=preFramePic.ptr(y);
                Vec3b *q= color.ptr(y);
                for(int x=0;x<(int)dinfo.width;x++)
                {
                    if (abs(p[x]-pp[x])>10)//p[x]!=0 && pp[x]!=0 &&
                    {
                        //printf("%d\n",p[x]);
                        q[x][0]=0;
                        q[x][1]=0;
                        q[x][2]=255;
                    }
                }
            }
        }


        //imshow("depth picture",output);waitKey(1);
        //imwrite("dd.png",output);

        framepic.copyTo(preFramePic);

        frameCount++;
        depthpic->ReleaseAccess(&depthImageData);
        if(!renderDepth->RenderFrame(depthpic)) break;

        imshow("伪彩图",color);
        waitKey(1);

        sm->ReleaseFrame();
    }
    sm->Release();
}

你可能感兴趣的:(体感)