kinect2.0 opencv3.2深度图像提取(方式一)

转载请注明出处:http://blog.csdn.net/mystylee/article/details/77992723

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

对于Kinect、opencv的配置不再赘述,网上很多资料。下面是实现调取kinect 深度图像的代码:

#include
#include
#include

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(i, j) = static_cast(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;
}kinect2.0 opencv3.2深度图像提取(方式一)_第1张图片

你可能感兴趣的:(kinect)