win10 Kinect2 Visualstudio2015 opencv3環境搭建
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 下面找一份項目,啟動項目
win10 Kinect2 Visualstudio2015 opencv3環境搭建