1、下载kinect SDK (
Kinect for Windows SDK 2.0
): https://www.microsoft.com/en-us/download/details.aspx?id=44561
下载以后直接安装,注意,必须是win8或者win10系统,由于是微软的产品,所以SDK安装过程非常简单,点击下一步就行,安装完成以后在:C:\Program Files\Microsoft SDKs 目录下
2、安装Visual Studio2015
3、安装Opencv3
4、新建项目KinectTest,在源文件创建test.cpp文件,然后配置
设置为 Debug模式 x64
4.1 配置包含目录
打开包含目录配置窗口,分别配置Kinect和opencv
C:\Program Files\Microsoft SDKs\Kinect\v2.0_1409\inc (默认)
E:\Opencv\opencv3_2_1\opencv\mybuild\install\include (自己opencv的目录)E:\Opencv\opencv3_2_1\opencv\mybuild\install\include\opencv (自己opencv的目录)E:\Opencv\opencv3_2_1\opencv\mybuild\install\include\opencv2 (自己opencv的目录)
4.2 配置库目录
C:\Program Files\Microsoft SDKs\Kinect\v2.0_1409\Lib\x64 (默认)
E:\Opencv\opencv3_2_1\opencv\mybuild\install\x64\vc14\lib (自己opencv的目录)
4.3 配置连接器
属性页 -> 链接器 -> 输入->附加依赖项
输入:
opencv_world300.lib
Kinect20.lib
参考代码:https://blog.csdn.net/ktigerhero3/article/details/49934745
===============================================代码
#include <stdio.h>
#include <iostream>#include <Kinect.h>#include <windows.h>#include <opencv/highgui.h>#include <cv.h>using namespace cv;
using namespace std; int main(){ IKinectSensor* m_pKinectSensor; IColorFrameSource* pColorFrameSource; IColorFrameReader* m_pColorFrameReader; IFrameDescription* pFrameDescription = NULL; ColorImageFormat imageFormat = ColorImageFormat_None; int nWidth, nHeight; uchar *pBuffer = NULL;UINT nBufferSize = 0;
HRESULT hr = GetDefaultKinectSensor(&m_pKinectSensor);//获取默认传感器
assert(hr >= 0); hr = m_pKinectSensor->Open();//打开传感器 assert(hr >= 0); hr = m_pKinectSensor->get_ColorFrameSource(&pColorFrameSource);//打开颜色信息源 assert(hr >= 0); pColorFrameSource->OpenReader(&m_pColorFrameReader);//打开颜色帧读取器 assert(hr >= 0); while (1){ //获取最近的彩色帧 IColorFrame* pColorFrame = NULL; while ((hr < 0) || (NULL == pColorFrame))//循环直到获取到最近的一帧 { hr = m_pColorFrameReader->AcquireLatestFrame(&pColorFrame); } //获取彩色图片信息包括宽,高,格式 assert(hr >= 0); pColorFrame->get_FrameDescription(&pFrameDescription);//获取图片描述信息 pFrameDescription->get_Width(&nWidth); pFrameDescription->get_Height(&nHeight); cout << "width=" << nWidth << endl;//我的kinect摄像头宽为1920 cout << "Height=" << nHeight << endl;//我的kinect摄像头高为1080 pColorFrame->get_RawColorImageFormat(&imageFormat);//输出结果为 ColorImageFormat_Yuy2 = 5,为Yuy2格式 /*YUY2格式,以4:2:2方式打包 YUV 4:2:2 每个色差信道的抽样率是亮度信道的一半,所以水平方向的色度抽样率只是4:4 : 4的一半。对非压缩的8比特量化的图像来说, 每个由两个水平方向相邻的像素组成的宏像素需要占用4字节内存。 下面的四个像素为:[Y0 U0 V0][Y1 U1 V1][Y2 U2 V2][Y3 U3 V3] 存放的码流为:Y0 U0 Y1 V1 Y2 U2 Y3 V3 映射出像素点为:[Y0 U0 V1][Y1 U0 V1][Y2 U2 V3][Y3 U2 V3]*/ cout << "imageformat is " << imageFormat << endl;Mat colorImg(nHeight, nWidth, CV_8UC4);//新建一个mat对象,用于保存读入的图像,注意参数的高在前,宽在后
pBuffer = colorImg.data; nBufferSize = colorImg.rows*colorImg.step;/*调用CopyConvertedFrameDataToArray,此函数的作用是从pColorFrame对象中拷贝nBufferSize个字节到pBuffer所指的Mat矩阵中,按
ColorImageFormat_Bgra格式保存*/ hr = pColorFrame->CopyConvertedFrameDataToArray(nBufferSize, reinterpret_cast<BYTE*>(pBuffer), ColorImageFormat_Bgra); pColorFrame->Release();namedWindow("display");
imshow("display", colorImg); if (27 == waitKey(50)) break; } return 0;}==============================================
或者在 C:\Program Files\Microsoft SDKs\Kinect\v2.0_1409\Samples 下面找一份项目,启动项目