Opencv+Kinect2.0获取景深图

来源:互联网 发布:淘宝人像摄影教程 编辑:程序博客网 时间:2024/04/30 02:11

一、景深图
和之前获取彩色图的差不多,就多了一个类型转换

#include "opencv2/core.hpp"#include "opencv2/imgproc.hpp"#include "opencv2/highgui.hpp"#include "opencv2/videoio.hpp"#include <iostream>#include <Kinect.h>  #pragma comment ( lib, "kinect20.lib" )  using namespace cv;using namespace std;int main(){    HRESULT hResult = S_OK;         IKinectSensor *kinect;              GetDefaultKinectSensor(&kinect);    kinect->Open();         IDepthFrameSource *depthrsource;          IDepthFrameReader *depthreader;    IFrameDescription *depthde;    kinect->get_DepthFrameSource(&depthrsource);    depthrsource->OpenReader(&depthreader);    depthrsource->get_FrameDescription(&depthde);    int width = 0;          int height = 0;    depthde->get_Height(&height);    depthde->get_Width(&width);    Mat a(height, width, CV_8UC1);          namedWindow("aaa");    UINT16 *data = new UINT16[width*height];    while (1)    {        IDepthFrame*frame;        hResult=depthreader->AcquireLatestFrame(&frame);        if (SUCCEEDED(hResult))        {            frame->CopyFrameDataToArray(height*width, data);            for (int i = 0; i < width*height; i++)            {                byte intensity = (byte)(data[i]>>5);        //类型的转换,data内的值即为距离                reinterpret_cast<BYTE*>(a.data)[i] = intensity;            }        }        if (frame != NULL)          {            frame->Release();            frame = NULL;        }        if (waitKey(30) == VK_ESCAPE)                 break;        imshow("aaa", a);    }    if (depthrsource != NULL)        {        depthrsource->Release();        depthrsource = NULL;    }    if (depthreader != NULL)    {        depthreader->Release();        depthreader = NULL;    }    if (depthde != NULL)    {        depthde->Release();        depthde = NULL;    }    if (kinect)    {        kinect->Close();    }    if (kinect != NULL)    {        kinect->Release();        kinect = NULL;    }    destroyAllWindows();}

运行图:
这里写图片描述

二、红外图
同样,只是修改下类型就可以了

#include "opencv2/core.hpp"#include "opencv2/imgproc.hpp"#include "opencv2/highgui.hpp"#include "opencv2/videoio.hpp"#include <iostream>#include <Kinect.h>  #pragma comment ( lib, "kinect20.lib" )  using namespace cv;using namespace std;int main(){    HRESULT hResult = S_OK;         IKinectSensor *kinect;              GetDefaultKinectSensor(&kinect);    kinect->Open();         IInfraredFrameSource *irsource;          IInfraredFrameReader *irreader;    IFrameDescription *irde;    kinect->get_InfraredFrameSource(&irsource);    irsource->OpenReader(&irreader);    irsource->get_FrameDescription(&irde);    int width = 0;          int height = 0;    irde->get_Height(&height);    irde->get_Width(&width);    Mat a(height, width, CV_8UC1);          namedWindow("aaa");    UINT16 *data = new UINT16[width*height];    while (1)    {        IInfraredFrame*frame;        hResult=irreader->AcquireLatestFrame(&frame);        if (SUCCEEDED(hResult))        {            frame->CopyFrameDataToArray(height*width, data);            for (int i = 0; i < width*height; i++)            {                byte intensity = (byte)(data[i]>>8);        //类型的转换                reinterpret_cast<BYTE*>(a.data)[i] = intensity;            }        }        if (frame != NULL)          {            frame->Release();            frame = NULL;        }        if (waitKey(30) == VK_ESCAPE)                 break;        imshow("aaa", a);    }    if (irsource != NULL)        {        irsource->Release();        irsource = NULL;    }    if (irreader != NULL)    {        irreader->Release();        irreader = NULL;    }    if (irde != NULL)    {        irde->Release();        irde = NULL;    }    if (kinect)    {        kinect->Close();    }    if (kinect != NULL)    {        kinect->Release();        kinect = NULL;    }    destroyAllWindows();}

运行图:
这里写图片描述

0 0
原创粉丝点击