Opencv+Kinect2.0获取景深图

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

#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();
}

运行图:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cccccc1212

这是c币不是人民币,不要充值

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值