获取深度图像

#include<stdafx.h>
#include <iostream>
#include <opencv2\imgproc.hpp>    //opencv头文件
#include <opencv2\calib3d.hpp>
#include <opencv2\highgui.hpp>
#include <Kinect.h>    //Kinect头文件

using namespace std;
using namespace cv;

int main(void)
{
    //初始化Kinect
    IKinectSensor* mySensor;
    HRESULT hResult = S_OK;
    hResult = GetDefaultKinectSensor(&mySensor);
    if (FAILED(hResult)) {
        cerr << "Error : GetDefaultKinectSensor" << std::endl;
        return -1;
    }
    hResult = mySensor->Open();
    if (FAILED(hResult)) {
        cerr << "Error : IKinectSensor::Open()" << std::endl;
        return -1;
    }

    //深度帧源
    IDepthFrameSource   * myDepthSource = nullptr;
    hResult = mySensor->get_DepthFrameSource(&myDepthSource);
    if (FAILED(hResult)) {
        cerr << "Error : IKinectSensor::get_DepthFrameSource()" << std::endl;
        return -1;
    }

    //深度帧读取
    IDepthFrameReader *myDepthReader = nullptr;
    hResult = myDepthSource->OpenReader(&myDepthReader);
    if (FAILED(hResult)) {
        cerr << "Error : IDepthFrameSource::OpenReader()" << std::endl;
        return -1;
    }

    //Description
    IFrameDescription * myDescription = nullptr;
    hResult = myDepthSource->get_FrameDescription(&myDescription);
    if (FAILED(hResult)) {
        cerr << "Error : IColorFrameSource::get_FrameDescription()" << std::endl;
        return -1;
    }
    int height = 0, width = 0;
    myDescription->get_Height(&height);
    myDescription->get_Width(&width);
    myDescription->Release();

    Mat temp(height, width, CV_16UC1);    //建立图像矩阵
    Mat img(height, width, CV_8UC1);
    IDepthFrame * myDepthFrame = nullptr;

    while (1)
    {

        while (myDepthReader->AcquireLatestFrame(&myDepthFrame) != S_OK);
        myDepthFrame->CopyFrameDataToArray(height*width, (UINT16 *)temp.data);
        temp.convertTo(img, CV_8UC1, 255.0 / 4500);

        myDepthFrame->Release();   //释放彩色帧
        imshow("test", img);
        if (waitKey(30) == VK_ESCAPE)
            break;
        //Sleep(1000);
    }
    myDepthSource->Release();
    myDepthReader->Release();
    mySensor->Close();
    mySensor->Release();
    return 0;
}

 

 

#include<stdafx.h>#include <iostream>#include <opencv2\imgproc.hpp>//opencv头文件#include <opencv2\calib3d.hpp>#include <opencv2\highgui.hpp>#include <Kinect.h>//Kinect头文件
using namespace std;using namespace cv;
int main(void){//初始化KinectIKinectSensor* mySensor;HRESULT hResult = S_OK;hResult = GetDefaultKinectSensor(&mySensor);if (FAILED(hResult)) {cerr << "Error : GetDefaultKinectSensor" << std::endl;return -1;}hResult = mySensor->Open();if (FAILED(hResult)) {cerr << "Error : IKinectSensor::Open()" << std::endl;return -1;}
//深度帧源IDepthFrameSource   * myDepthSource = nullptr;hResult = mySensor->get_DepthFrameSource(&myDepthSource);if (FAILED(hResult)) {cerr << "Error : IKinectSensor::get_DepthFrameSource()" << std::endl;return -1;}
//深度帧读取IDepthFrameReader *myDepthReader = nullptr;hResult = myDepthSource->OpenReader(&myDepthReader);if (FAILED(hResult)) {cerr << "Error : IDepthFrameSource::OpenReader()" << std::endl;return -1;}
//DescriptionIFrameDescription * myDescription = nullptr;hResult = myDepthSource->get_FrameDescription(&myDescription);if (FAILED(hResult)) {cerr << "Error : IColorFrameSource::get_FrameDescription()" << std::endl;return -1;}int height = 0, width = 0;myDescription->get_Height(&height);myDescription->get_Width(&width);myDescription->Release();
Mat temp(height, width, CV_16UC1);    //建立图像矩阵Mat img(height, width, CV_8UC1);IDepthFrame * myDepthFrame = nullptr;
while (1){
while (myDepthReader->AcquireLatestFrame(&myDepthFrame) != S_OK);myDepthFrame->CopyFrameDataToArray(height*width, (UINT16 *)temp.data);temp.convertTo(img, CV_8UC1, 255.0 / 4500);
myDepthFrame->Release();   //释放彩色帧imshow("test", img);if (waitKey(30) == VK_ESCAPE)break;//Sleep(1000);}myDepthSource->Release();myDepthReader->Release();mySensor->Close();mySensor->Release();return 0;}

 

posted @ 2020-03-27 14:07  hsjwu  阅读(600)  评论(0编辑  收藏  举报