VS 2013 配置Kinect 2.0 :
- 首先,新建个项目,然后在【解决方案资源管理器】中右键项目名,选择【属性】
- 在【C/C++】的【常规】里,【附加包含目录】中加入【$(KINECTSDK20_DIR)\inc】
- 在【链接器】的【常规】里,【附加库目录】中加入【$(KINECTSDK20_DIR)\Lib\x86】
- 在【链接器】的【输入】里,【附加依赖项】中加入【kinect20.lib】
上面做完之后应该就可以编译运行了,但是我发现写代码时不会对【Kinect.h】中出现的函数这些进行自动补全,而且语法检查时提示【Kinect.h】找不到,如果你也出现此问题,那就在【解决方案资源管理器】中的【头文件】这里右键添加【Kinect.h】,它位于【C:\Program Files\Microsoft SDKs\Kinect\v2.0_1409\inc】中。
// Standard Library
#include <iostream>
// OpenCV Header
#include<opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
// Kinect for Windows SDK Header
#include <Kinect.h>
#include<fstream>
using namespace cv;
using namespace std;
vector<Point> src;
int i = 0;
ofstream fout("01.txt");
void writeMatToFile(cv::Mat& m, Point p, const char* filename)
{
//ofstream fout(filename);
if (!fout)
{
cout << "File Not Opened" << endl; return;
}
fout <<p.x<<" "<<p.y<<" "<< m.at<unsigned short>(p.x, p.y) << endl;
fout.close();
}
//点击两下获取部分三维坐标
Point p;
void onMouse(int event, int x, int y, int flags, void *param)
{
Mat *img = reinterpret_cast<Mat*>(param);
if (event == CV_EVENT_LBUTTONDOWN)//左键按下,读取初始坐标,并在图像上该点处划圆
{
i++;//统计点击的次数
p.x = x;
p.y = y;
src.push_back(p);
cout << p << static_cast<int>(img->at<unsigned short>(cv::Point(x, y))) << endl;
cout << i << endl;
//cout << image.at<unsigned short> << endl;
//cout << p << static_cast<int>(img->at<uchar>(cv::Point(x, y))) << endl;
//cout << img << endl;
fout << p.x << " " << p.y << " " << static_cast<int>(img->at<unsigned short>(cv::Point(x, y))) << endl;
//writeMatToFile(*img, p, "01.txt");
}
}
//using namespace std;
int main(int argc, char** argv)
{
// 1a. Get default Sensor
IKinectSensor* pSensor = nullptr;
GetDefaultKinectSensor(&pSensor);
// 1b. Open sensor
pSensor->Open();
// 2a. Get frame source
IDepthFrameSource* pFrameSource = nullptr;
pSensor->get_DepthFrameSource(&pFrameSource);
// 2b. Get frame description
int iWidth = 0;
int iHeight = 0;
IFrameDescription* pFrameDescription = nullptr;
pFrameSource->get_FrameDescription(&pFrameDescription);
pFrameDescription->get_Width(&iWidth);
pFrameDescription->get_Height(&iHeight);
pFrameDescription->Release();
pFrameDescription = nullptr;
// 2c. get some dpeth only meta
UINT16 uDepthMin = 0, uDepthMax = 0;
pFrameSource->get_DepthMinReliableDistance(&uDepthMin);
pFrameSource->get_DepthMaxReliableDistance(&uDepthMax);
cout << "Reliable Distance: "
<< uDepthMin << " – " << uDepthMax << endl;
// perpare OpenCV
cv::Mat mDepthImg(iHeight, iWidth, CV_16UC1);
cv::Mat mImg8bit(iHeight, iWidth, CV_8UC1);
cv::namedWindow("Depth Map");
// 3a. get frame reader
IDepthFrameReader* pFrameReader = nullptr;
pFrameSource->OpenReader(&pFrameReader);
// Enter main loop
while (true)
{
// 4a. Get last frame
IDepthFrame* pFrame = nullptr;
if (pFrameReader->AcquireLatestFrame(&pFrame) == S_OK)
{
// 4c. copy the depth map to image
pFrame->CopyFrameDataToArray(iWidth * iHeight,
reinterpret_cast<UINT16*>(mDepthImg.data));
// 4d. convert from 16bit to 8bit
mDepthImg.convertTo(mImg8bit, CV_8U, 255.0f / uDepthMax);
namedWindow("image", CV_WINDOW_AUTOSIZE);
setMouseCallback("image", onMouse, &mDepthImg);
imshow("image", mImg8bit);
//cv::imshow("Depth Map", mImg8bit);
// 4e. release frame
pFrame->Release();
}
// 4f. check keyboard input
if (cv::waitKey(30) == VK_ESCAPE){
break;
}
}
// 3b. release frame reader
pFrameReader->Release();
pFrameReader = nullptr;
// 2d. release Frame source
pFrameSource->Release();
pFrameSource = nullptr;
// 1c. Close Sensor
pSensor->Close();
// 1d. Release Sensor
pSensor->Release();
pSensor = nullptr;
return 0;
}