kinect2.0开发(一) 读取深度图像

来源:互联网 发布:php订单管理系统 开源 编辑:程序博客网 时间:2024/06/05 03:01

代码运行环境

本代码为运行在windows8 64位系统上,采用vs2013集成开发环境,编译为win32程序
利用opencv库显示图像

运行前准备

1.安装Microsoft kinect2.0 SDK
2.配置opencv环境变量
参考以下步骤
http://tanghenxin.blog.163.com/blog/static/213511105201421244826743/
3.将kinect.h的库文件Kinect20.lib包含进去
包含目录: C:\Program Files\Microsoft SDKs\Kinect\v2.0_1409\inc
库目录:C:\Program Files\Microsoft SDKs\Kinect\v2.0_1409\Lib\x86

读取深度图像步骤

  1. 获取kinect传感器
  2. 打开传感器
  3. 获取深度信息传感器
  4. 打开深度帧读取器
  5. 获得最近的一帧
  6. 将深度信息转换为MAT格式
  7. 用opencv的imshow显示
  8. 回收和释放内存

代码如下:

// kinectSensorTest.cpp : 定义控制台应用程序的入口点。//#include <stdio.h>#include <Kinect.h>#include <windows.h>#include <highgui.h>#include <cv.h>using namespace cv;// 转换depth图像到cv::MatMat ConvertMat(const UINT16* pBuffer, int nWidth, int nHeight){    Mat img(nHeight, nWidth, CV_8UC1);    uchar* p_mat = img.data;//指向头指针    const UINT16* pBufferEnd = pBuffer + (nWidth * nHeight);//指向最后一个元素的指针    while (pBuffer < pBufferEnd)//16位最大值为65536    {        *p_mat++ = *pBuffer++ /65536.0 * 256;    }    return img;}int main(){    IKinectSensor*          m_pKinectSensor;    IDepthFrameReader*      m_pDepthFrameReader;    IDepthFrame* pDepthFrame = NULL;    IFrameDescription* pFrameDescription = NULL;    IDepthFrameSource* pDepthFrameSource = NULL;    HRESULT hr = GetDefaultKinectSensor(&m_pKinectSensor);//获取默认kinect传感器    assert(hr >= 0);    printf("打开kinect传感器成功\n");    hr = m_pKinectSensor->Open();//打开传感器    assert(hr >= 0);    hr = m_pKinectSensor->get_DepthFrameSource(&pDepthFrameSource);//获得深度信息传感器    assert(hr >= 0);    hr = pDepthFrameSource->OpenReader(&m_pDepthFrameReader);//打开深度信息帧读取器    assert(hr >= 0);    while (hr < 0 || pDepthFrame == NULL)        hr = m_pDepthFrameReader->AcquireLatestFrame(&pDepthFrame);//由于有时候获取不到,因此循环获取最近的帧    assert(hr >= 0);    hr = pDepthFrame->get_FrameDescription(&pFrameDescription);//获取帧的像素信息(宽和高)    int depth_width, depth_height;    pFrameDescription->get_Width(&depth_width);    pFrameDescription->get_Height(&depth_height);    printf("width=%d height=%d\n", depth_width, depth_height);    USHORT nDepthMinReliableDistance = 0;//获取最大、最小深度距离信息    USHORT nDepthMaxReliableDistance = 0;    assert(hr >= 0);    hr = pDepthFrame->get_DepthMinReliableDistance(&nDepthMinReliableDistance);    assert(hr >= 0);    hr = pDepthFrame->get_DepthMaxReliableDistance(&nDepthMaxReliableDistance);    printf("nDepthMinReliableDistance=%d nDepthMaxReliableDistance=%d\n", nDepthMinReliableDistance, nDepthMaxReliableDistance);    UINT nBufferSize_depth = 0;    UINT16 *pBuffer_depth = NULL;    pDepthFrame->AccessUnderlyingBuffer(&nBufferSize_depth, &pBuffer_depth);//获取图像像素个数和指向图像的指针    //转换为MAT格式    Mat depthImg_show = ConvertMat(pBuffer_depth, depth_width, depth_height);//转换为8位的mat    equalizeHist(depthImg_show, depthImg_show);//均衡化,为了提高显示效果    imwrite("MyFirstKinectImg.jpg", depthImg_show);//保存图片    //用opencv显示    namedWindow("display");    imshow("display", depthImg_show);    if (27 == waitKey(0))    return 0;}

实验结果如图:
这里写图片描述

0 0