kinect彩色数据的获取以及在opencv中显示

来源:互联网 发布:中医方剂大辞典数据库 编辑:程序博客网 时间:2024/04/26 06:18

一个非常简单的kinect彩色图像获取的示例

  • 一个非常简单的kinect彩色图像获取的示例
    • 说明
    • 其它kinect教程分享
    • 代码

说明

刚开始接触kinect,发现官方的教程写得都比较难,作为小白的我不怎么看得懂,所以就准备写几个非常的示例,方便自己的学习。
平台:

  1. kinect2.0
  2. kinect for windows sdk2.0
  3. opencv2.4.9
  4. win8.1
  5. vs2013

其它kinect教程分享

http://guoming.me/category/technique/kinect
http://blog.csdn.net/column/details/k4w2dn.html

代码

下面的示例属于win32控制台应用代码
base.h

#pragma once#include <iostream>#include "opencv2/opencv.hpp"#include <Kinect.h>#pragma comment(lib,"Kinect20.lib")//分两个模式引入文件不然会出问题#ifdef _DEBUG#pragma comment(lib,"opencv_core249d.lib")#pragma comment(lib,"opencv_highgui249d.lib")#pragma comment(lib,"opencv_imgproc249d.lib")#endif#ifndef _DEBUG#pragma comment(lib,"opencv_core249.lib")#pragma comment(lib,"opencv_highgui249.lib")#pragma comment(lib,"opencv_imgproc249.lib")#endifusing namespace std;using namespace cv;// Safe release for interfacestemplate<class Interface>inline void SafeRelease(Interface *& pInterfaceToRelease){    if (pInterfaceToRelease != NULL)    {        pInterfaceToRelease->Release();        pInterfaceToRelease = NULL;    }}

main.cpp

#include"base.h"HRESULT main(){    /*        定义    */    IKinectSensor*        m_pKinectSensor = NULL;     IColorFrameSource*    m_pColorFrameSource = NULL;    IColorFrameReader*    m_pColorFrameReader = NULL;    IColorFrame*          m_pColorFrame;    IFrameDescription*    m_pFrameDescription = NULL;    RGBQUAD*              m_pColorRGBX = NULL;    static const int      cColorWidth  = 1920;    static const int      cColorHeight = 1080;    ColorImageFormat imageFormat = ColorImageFormat_None;    int nWidth = 0;    int nHeight = 0;    UINT nBufferSize = 0;    RGBQUAD *pBuffer = NULL;    /*        初始化    */    m_pColorRGBX = new RGBQUAD[cColorWidth * cColorHeight];    HRESULT hr;    hr = GetDefaultKinectSensor(&m_pKinectSensor);    if (FAILED(hr))    {        return hr;        cout << "获得默认的Kinect设备失败" << endl;    }    else    {        cout << "获得默认的Kinect设备成功" << endl;    }    hr = m_pKinectSensor->Open();    if (SUCCEEDED(hr))    {        hr = m_pKinectSensor->get_ColorFrameSource(&m_pColorFrameSource);    }    if (SUCCEEDED(hr))    {        hr = m_pColorFrameSource->OpenReader(&m_pColorFrameReader);    }    if (!m_pKinectSensor || FAILED(hr))    {        cout << "没有可用的Kinect" << endl;    }    else    {        cout << "打开Kinect成功" << endl;    }    SafeRelease(m_pColorFrameSource);    /*    读取数据    */    while (true)    {        if (!m_pColorFrameReader)        {            return S_FALSE;        }        //不是每一次都能获取到m_pColorFrame,因为kinect的彩色帧的帧率是固定的30或者15(看灯光) 而且刚开始时候过来的数据是有问题的不能被正常解析        hr = m_pColorFrameReader->AcquireLatestFrame(&m_pColorFrame);        if ( SUCCEEDED(hr) )        {            hr = m_pColorFrame->get_FrameDescription(&m_pFrameDescription);            cout << "获取数据成功" << endl;        }        else        {            cout << "                                      获取数据失败" << endl;        }        if (SUCCEEDED(hr))        {            hr = m_pFrameDescription->get_Width(&nWidth);        }        if (SUCCEEDED(hr))        {            hr = m_pFrameDescription->get_Height(&nHeight);        }        if (SUCCEEDED(hr))        {            hr = m_pColorFrame->get_RawColorImageFormat(&imageFormat);        }        if (SUCCEEDED(hr))        {            if (imageFormat == ColorImageFormat_Bgra)            {                hr = m_pColorFrame->AccessRawUnderlyingBuffer(&nBufferSize, reinterpret_cast<BYTE**>(&pBuffer));            }            else if (m_pColorRGBX)            {                pBuffer = m_pColorRGBX;                nBufferSize = cColorWidth * cColorHeight * sizeof(RGBQUAD);                hr = m_pColorFrame->CopyConvertedFrameDataToArray(nBufferSize,reinterpret_cast<BYTE*>(pBuffer),ColorImageFormat_Bgra);            }            else            {                return E_FAIL;            }        }        SafeRelease(m_pColorFrame); //关键的地方每次要记得释放        //转化成Mat        Mat showImg;        if( pBuffer && ( nWidth == cColorWidth) && (nHeight == cColorHeight))        {            Mat img(cColorHeight, cColorWidth, CV_8UC4, pBuffer);            resize(img, showImg, Size(cColorWidth / 2, cColorHeight / 2));        }        else        {            cout << "pBuffer = " << pBuffer << endl;            cout << "nWidth = " << nWidth << " " << "nHeight = " << cColorWidth<< endl;            cout << "nHeight = " << nHeight << " " << "cColorHeight = " << cColorHeight << endl;            cout << "数据转化有误" << endl;            showImg = Mat(cColorHeight / 2, cColorWidth / 2,CV_8UC4,Scalar(0,0,0,0));        }        imshow("ColorImg", showImg);        waitKey(1000/30);    }    /*        释放 上面申请的Kinect接口都应该释放    */    //waitKey();    return S_OK;}

效果图:
这里写图片描述

图只是截了了一半,环境有点乱,见笑了。。。

0 0
原创粉丝点击