1. 程式人生 > >kinect2.0 opencv3.2深度影象提取(方式一)

kinect2.0 opencv3.2深度影象提取(方式一)

轉載請註明出處:http://blog.csdn.net/mystylee/article/details/77992723

本人的配置:win10 + visual studio 2015 + kinect2.0 + opencv3.2

對於Kinect、opencv的配置不再贅述,網上很多資料。下面是實現調取kinect 深度影象的程式碼:

#include <Kinect.h>
#include <iostream>
#include <opencv2/highgui.hpp>

using   namespace   std;
using   namespace   cv;

char file_name[20];
int a = 0;
int main(void)
{
IKinectSensor   * m_pKinectSensor = nullptr;
GetDefaultKinectSensor(&m_pKinectSensor);  
m_pKinectSensor->Open();          


IDepthFrameSource   * m_pDepthSource = nullptr;   
m_pKinectSensor->get_DepthFrameSource(&m_pDepthSource);


int depth_height = 0, depth_width = 0;
IFrameDescription   * m_pDepthDescription = nullptr;  
m_pDepthSource->get_FrameDescription(&m_pDepthDescription);
m_pDepthDescription->get_Height(&depth_height);
m_pDepthDescription->get_Width(&depth_width);
m_pDepthDescription->Release();

IDepthFrameReader   *m_pDepthReader = nullptr;
m_pDepthSource->OpenReader(&m_pDepthReader);    

IDepthFrame * m_pDepthFrame = nullptr;
UINT16* depthArray = new UINT16[depth_height*depth_width];
Mat depthImg(depth_height, depth_width, CV_8UC1);
while (1)
{
if (m_pDepthReader->AcquireLatestFrame(&m_pDepthFrame) == S_OK) 
{
m_pDepthFrame->CopyFrameDataToArray(depth_height * depth_width, depthArray); 
for (int i = 0; i < depth_height; i++)//遍歷影象
{
for (int j = 0; j < depth_width; j++)
{
depthImg.at<uchar>(i, j) = static_cast<uchar>(depthArray[i * depth_width + j]);
}
}
imshow("depthImg", depthImg);
sprintf(file_name, "depth + %d.jpg", a++);//也可以是其他型別的圖片檔案。比如.bmp
imwrite(file_name, depthImg);//儲存每一幀的深度圖片
waitKey(1);


m_pDepthFrame->Release();
}
if (waitKey(30) == VK_ESCAPE)
break;
}
m_pDepthReader->Release();        
m_pDepthSource->Release();
m_pKinectSensor->Close();
m_pKinectSensor->Release();


return  0;
}