接上一篇:Kinect2.0獲取數據
http://blog.csdn.net/jiaojialulu/article/details/53087988
博主好細心,代碼基本上帖過來就可以用,注釋掉的部分改成文件輸出就可以了!
#include "stdafx.h"
#include "kinect.h"
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
// 安全釋放指針
template<class Interface>
inline void SafeRelease(Interface *& pInterfaceToRelease)
{
if (pInterfaceToRelease != NULL)
{
pInterfaceToRelease->Release();
pInterfaceToRelease = NULL;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
// 獲取Kinect設備
IKinectSensor* m_pKinectSensor;
HRESULT hr;
hr = GetDefaultKinectSensor(&m_pKinectSensor);
if (FAILED(hr))
{
return hr;
}
IMultiSourceFrameReader* m_pMultiFrameReader=NULL;
if (m_pKinectSensor)
{
hr = m_pKinectSensor->Open();
if (SUCCEEDED(hr))
{
// 獲取多數據源到讀取器
hr = m_pKinectSensor->OpenMultiSourceFrameReader(
FrameSourceTypes::FrameSourceTypes_Color |
FrameSourceTypes::FrameSourceTypes_Infrared |
FrameSourceTypes::FrameSourceTypes_Depth,
&m_pMultiFrameReader);
}
}
if (!m_pKinectSensor || FAILED(hr))
{
return E_FAIL;
}
// 三個數據幀及引用
IDepthFrameReference* m_pDepthFrameReference = NULL;
IColorFrameReference* m_pColorFrameReference = NULL;
IInfraredFrameReference* m_pInfraredFrameReference = NULL;
IInfraredFrame* m_pInfraredFrame = NULL;
IDepthFrame* m_pDepthFrame = NULL;
IColorFrame* m_pColorFrame = NULL;
// 三個圖片格式
Mat i_rgb(1080, 1920, CV_8UC4); //注意:這里必須為4通道的圖,Kinect的數據只能以Bgra格式傳出
Mat i_depth(424, 512, CV_8UC1);
Mat i_ir(424, 512, CV_16UC1);
UINT16 *depthData = new UINT16[424 * 512];
IMultiSourceFrame* m_pMultiFrame = nullptr;
while (true)
{
// 獲取新的一個多源數據幀
hr = m_pMultiFrameReader->AcquireLatestFrame(&m_pMultiFrame);
if (FAILED(hr) || !m_pMultiFrame)
{
//cout << "!!!" << endl;
continue;
}
// 從多源數據幀中分離出彩色數據,深度數據和紅外數據
if (SUCCEEDED(hr))
hr = m_pMultiFrame->get_ColorFrameReference(&m_pColorFrameReference);
if (SUCCEEDED(hr))
hr = m_pColorFrameReference->AcquireFrame(&m_pColorFrame);
if (SUCCEEDED(hr))
hr = m_pMultiFrame->get_DepthFrameReference(&m_pDepthFrameReference);
if (SUCCEEDED(hr))
hr = m_pDepthFrameReference->AcquireFrame(&m_pDepthFrame);
if (SUCCEEDED(hr))
hr = m_pMultiFrame->get_InfraredFrameReference(&m_pInfraredFrameReference);
if (SUCCEEDED(hr))
hr = m_pInfraredFrameReference->AcquireFrame(&m_pInfraredFrame);
// color拷貝到圖片中
UINT nColorBufferSize = 1920 * 1080 * 4;
if (SUCCEEDED(hr))
hr = m_pColorFrame->CopyConvertedFrameDataToArray(nColorBufferSize, reinterpret_cast<BYTE*>(i_rgb.data), ColorImageFormat::ColorImageFormat_Bgra);
// depth拷貝到圖片中
if (SUCCEEDED(hr))
{
hr = m_pDepthFrame->CopyFrameDataToArray(424 * 512, depthData);
for (int i = 0; i < 512 * 424; i++)
{
// 0-255深度圖,為了顯示明顯,只取深度數據的低8位
BYTE intensity = static_cast<BYTE>(depthData[i] % 256);
reinterpret_cast<BYTE*>(i_depth.data)[i] = intensity;
}
ICoordinateMapper* m_pCoordinateMapper=NULL;
hr = m_pKinectSensor->get_CoordinateMapper(&m_pCoordinateMapper);
ColorSpacePoint* m_pColorCoordinates = new ColorSpacePoint[512 * 424];
HRESULT hr = m_pCoordinateMapper->MapDepthFrameToColorSpace(512 * 424, depthData, 512 * 424, m_pColorCoordinates);
Mat i_depthToRgb(424, 512, CV_8UC4);
if (SUCCEEDED(hr))
{
for (int i = 0; i < 424 * 512; i++)
{
ColorSpacePoint p = m_pColorCoordinates[i];
if (p.X != -std::numeric_limits<float>::infinity() && p.Y != -std::numeric_limits<float>::infinity())
{
int colorX = static_cast<int>(p.X + 0.5f);
int colorY = static_cast<int>(p.Y + 0.5f);
if ((colorX >= 0 && colorX < 1920) && (colorY >= 0 && colorY < 1080))
{
i_depthToRgb.data[i * 4] = i_rgb.data[(colorY * 1920 + colorX) * 4];
i_depthToRgb.data[i * 4 + 1] = i_rgb.data[(colorY * 1920 + colorX) * 4 + 1];
i_depthToRgb.data[i * 4 + 2] = i_rgb.data[(colorY * 1920 + colorX) * 4 + 2];
i_depthToRgb.data[i * 4 + 3] = i_rgb.data[(colorY * 1920 + colorX) * 4 + 3];
}
}
}
}
imshow("rgb2depth", i_depthToRgb);
if (waitKey(1) == VK_ESCAPE)
break;
CameraSpacePoint* m_pCameraCoordinates = new CameraSpacePoint[512 * 424];
if (SUCCEEDED(hr))
{
HRESULT hr = m_pCoordinateMapper->MapDepthFrameToCameraSpace(512 * 424, depthData, 512 * 424, m_pCameraCoordinates);
}
if (SUCCEEDED(hr))
{
for (int i = 0; i < 512 * 424; i++)
{
CameraSpacePoint p = m_pCameraCoordinates[i];
if (p.X != -std::numeric_limits<float>::infinity() && p.Y != -std::numeric_limits<float>::infinity() && p.Z != -std::numeric_limits<float>::infinity())
{
float cameraX = static_cast<float>(p.X);
float cameraY = static_cast<float>(p.Y);
float cameraZ = static_cast<float>(p.Z);
//cout << "x: " << cameraX << "y: " << cameraY << "z: " << cameraZ << endl;
//GLubyte *rgb = new GLubyte();
//rgb[2] = i_depthToRgb.data[i * 4 + 0];
//rgb[1] = i_depthToRgb.data[i * 4 + 1];
//rgb[0] = i_depthToRgb.data[i * 4 + 2];
//// 顯示點
//glColor3ubv(rgb);
//glVertex3f(cameraX, -cameraY, cameraZ);
}
}
}
}
// 顯示
/*imshow("rgb", i_rgb);
if (waitKey(1) == VK_ESCAPE)
break;*/
imshow("depth", i_depth);
if (waitKey(1) == VK_ESCAPE)
break;
// 釋放資源
SafeRelease(m_pColorFrame);
SafeRelease(m_pDepthFrame);
SafeRelease(m_pInfraredFrame);
SafeRelease(m_pColorFrameReference);
SafeRelease(m_pDepthFrameReference);
SafeRelease(m_pInfraredFrameReference);
SafeRelease(m_pMultiFrame);
}
// 關閉窗口,設備
cv::destroyAllWindows();
m_pKinectSensor->Close();
std::system("pause");
return 0;
}
我們實驗室的一幀數據,哈哈!

上面的代碼有內存泄露,程序運行一段時間把我的機器物理內存都占滿了,下面代碼更新一下!
