年末了,才想起导师交代的任务还没开始做。所以网上资料一顿找,将Kinect2.0在win10 64位 VS2013以及opencv2.4.9 的环境配置整理一下。
首先是你要装Kinect2.0的驱动,需要下载微软提供的SDK,这里给出链接:
https://www.microsoft.com/en-us/download/details.aspx?id=44561
接下来,我们打开VS2013新建C++控制台应用程序
配置opencv2.4.9以及Kinect2.0
在属性中
VC++目录中
包含目录:(找到opencv安装的目录中),点前面是你们自己的安装路径
.\opencv\build\include\opencv
.\opencv\build\include\opencv2
.\opencv\build\include
$(KINECTSDK20_DIR)\inc
-------KINECTSDK20_DIR是你安装了SDK以后,在系统环境变量中的值,默认安装应该都会是这个路径,如果没有,自己手动添加环境变量即可
库目录:
.\opencv\build\x86\vc12\lib
$(KINECTSDK20_DIR)\lib\x86
这里x86和x64 视你的编译环境而定,x64不行的话就换成x86
在链接器——>输入——>附加依赖项中添加
opencv2.4.9 debug库
opencv_calib3d249d.lib
opencv_contrib249d.lib
opencv_core249d.lib
opencv_features2d249d.lib
opencv_flann249d.lib
opencv_gpu249d.lib
opencv_highgui249d.lib
opencv_imgproc249d.lib
opencv_legacy249d.lib
opencv_ml249d.lib
opencv_nonfree249d.lib
opencv_objdetect249d.lib
opencv_ocl249d.lib
opencv_photo249d.lib
opencv_stitching249d.lib
opencv_superres249d.lib
opencv_ts249d.lib
opencv_video249d.lib
opencv_videostab249d.lib
以及Kinect2.0 库
Kinect20.VisualGestureBuilder.lib
Kinect20.lib
Kinect20.Face.lib
Kinect20.Fusion.lib
环境配置就是这样
这里放一个小demo,进行测试,效果是获取Kinect彩色图 深度图和红外图
原始代码来自(http://blog.csdn.net/jiaojialulu/article/details/53087988),我做了些许改动,编译通过运行
#include
#include
#include
#include
using namespace cv;
using namespace std;
// 安全释放指针
template
inline void SafeRelease(Interface *& pInterfaceToRelease)
{
if (pInterfaceToRelease != NULL)
{
pInterfaceToRelease->Release();
pInterfaceToRelease = NULL;
}
}
int main()
{
// 获取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(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(depthData[i] % 256);
reinterpret_cast(i_depth.data)[i] = intensity;
}
// 实际是16位unsigned int数据
//hr = m_pDepthFrame->CopyFrameDataToArray(424 * 512, reinterpret_cast(i_depth.data));
}
// infrared拷贝到图片中
if (SUCCEEDED(hr))
{
hr = m_pInfraredFrame->CopyFrameDataToArray(424 * 512, reinterpret_cast(i_ir.data));
}
// 显示
imshow("rgb", i_rgb);
if (waitKey(1) == VK_ESCAPE)
break;
imshow("depth", i_depth);
if (waitKey(1) == VK_ESCAPE)
break;
imshow("ir", i_ir);
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;
}