您的位置:首页 > 运维架构

Kinect sdk 2.0 + Opencv 获取深度图像并保存

2017-09-09 21:44 741 查看
代码是我结合kinect sdk 2.0 里面的例程和其他网友的代码写出来的。

如果你想在你的电脑上实现我的代码,那你的电脑需要满足以下两个条件:

1. 硬件设备:kinect v2;

2. 编译环境:kinect sdk 2.0 + Opencv3.0 配入编译环境(我有另一篇博客讲的是如何配置编译环境,有兴趣可以找找)。

代码如下:

#include <iostream>
#include <opencv2\imgproc.hpp>
#include <opencv2\calib3d.hpp>
#include <opencv2\highgui.hpp>
#include <Kinect.h>

using   namespace   std;
using   namespace   cv;

// Safe release for interfaces
template<class Interface>
inline void SafeRelease(Interface *& pInterfaceToRelease)
{
if (pInterfaceToRelease != NULL)
{
pInterfaceToRelease->Release();
pInterfaceToRelease = NULL;
}
}

Mat ConvertMat(const UINT16* pBuffer, int nWidth, int nHeight, USHORT nMinDepth, USHORT nMaxDepth)
{
cv::Mat img(nHeight, nWidth, CV_8UC3);
uchar* p_mat = img.data;
const UINT16* pBufferEnd = pBuffer + (nWidth * nHeight);
while (pBuffer < pBufferEnd)
{
USHORT depth = *pBuffer;
BYTE intensity = static_cast<BYTE>((depth >= nMinDepth)&&(depth<= nMaxDepth)?(depth%256):0);
*p_mat = intensity;
p_mat++;
*p_mat = intensity;
p_mat++;
*p_mat = intensity;
p_mat++;
++pBuffer;
}
return img;
}

void main()
{
// Get and initialize the default Kinect sensor
IKinectSensor* m_pKinectSensor = NULL;
// Depth reader
IDepthFrameReader* m_pDepthFrameReader = NULL;
HRESULT hr;
hr = GetDefaultKinectSensor(&m_pKinectSensor);
if (FAILED(hr))
{
cout<<"open kinectsensor failed "<<endl;
}
if (m_pKinectSensor)
{
// Initialize the Kinect and get the depth reader
IDepthFrameSource* pDepthFrameSource = NULL;
hr = m_pKinectSensor->Open();
if (SUCCEEDED(hr))
{
hr = m_pKinectSensor->get_DepthFrameSource(&pDepthFrameSource);
}
if (SUCCEEDED(hr))
{
hr = pDepthFrameSource->OpenReader(&m_pDepthFrameReader);
}
SafeRelease(pDepthFrameSource);
}
if (!m_pKinectSensor || FAILED(hr))
{
cout << "No ready Kinect found!" << endl;
}
IplImage qImg;
while (1)
{
if (!m_pDepthFrameReader)
{
return;
}
IDepthFrame* pDepthFrame = NULL;
HRESULT hr = m_pDepthFrameReader->AcquireLatestFrame(&pDepthFrame);
if (SUCCEEDED(hr))
{
INT64 nTime = 0;
IFrameDescription* pFrameDescription = NULL;
int nWidth = 0;
int nHeight = 0;
USHORT nDepthMinReliableDistance = 0;
USHORT nDepthMaxDistance = 0;
UINT nBufferSize = 0;
UINT16 *pBuffer = NULL;
hr = pDepthFrame->get_RelativeTime(&nTime);
if (SUCCEEDED(hr))
{
hr = pDepthFrame->get_FrameDescription(&pFrameDescription);
}
if (SUCCEEDED(hr))
{
hr = pFrameDescription->get_Width(&nWidth);
}
if (SUCCEEDED(hr))
{
hr = pFrameDescription->get_Height(&nHeight);
}
if (SUCCEEDED(hr))
{
hr = pDepthFrame->get_DepthMinReliableDistance(&nDepthMinReliableDistance);
}
if (SUCCEEDED(hr))
{
// In order to see the full range of depth (including the less reliable far field depth)
// we are setting nDepthMaxDistance to the extreme potential depth threshold
nDepthMaxDistance = USHRT_MAX;

// Note:  If you wish to filter by reliable depth distance, uncomment the following line.
//// hr = pDepthFrame->get_DepthMaxReliableDistance(&nDepthMaxDistance);
}
if (SUCCEEDED(hr))
{
hr = pDepthFrame->AccessUnderlyingBuffer(&nBufferSize, &pBuffer);
}
if (SUCCEEDED(hr))
{
Mat depthImg_show = cv::Mat::zeros(nHeight, nWidth, CV_8UC3);
depthImg_show = ConvertMat(pBuffer, nWidth, nHeight,
nDepthMinReliableDistance, nDepthMaxDistance);
qImg = IplImage(depthImg_show);
cvSaveImage("depth.jpg", &qImg);
imshow("depth", depthImg_show);
}
SafeRelease(pFrameDescription);
}
SafeRelease(pDepthFrame);
if (waitKey(20) == VK_ESCAPE)
break;
}
// done with depth frame reader
SafeRelease(m_pDepthFrameReader);
// close the Kinect Sensor
if (m_pKinectSensor)
{
m_pKinectSensor->Close();
}
SafeRelease(m_pKinectSensor);
}


图像效果:



这是我运行上面的代码保存下来的图片。

以上代码,亲测能运行,如果运行不了,可以给我发邮件,或在评论区评论。

邮箱:Interstellar_ai@163.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息