win10 + visual studio 2015 + kinect2.0 + opencv3.2
#include <Kinect.h>
#include <iostream>
#include <opencv2\highgui.hpp>
using namespace std;
using namespace cv;
char file_name[20];
int a = 0;
int main(void)
{
IKinectSensor * mySensor = nullptr;
GetDefaultKinectSensor(&mySensor); //獲取感應器
mySensor->Open(); //打開感應器
IDepthFrameSource * mySource = nullptr; //取得深度數據
mySensor->get_DepthFrameSource(&mySource);
int height = 0, width = 0;
IFrameDescription * myDescription = nullptr; //取得深度數據的分辨率
mySource->get_FrameDescription(&myDescription);
myDescription->get_Height(&height);
myDescription->get_Width(&width);
myDescription->Release();
IDepthFrameReader * myReader = nullptr;
mySource->OpenReader(&myReader); //打開深度數據的Reader
IDepthFrame * myFrame = nullptr;
Mat temp(height, width, CV_16UC1); //建立圖像矩陣
Mat depthImg(height, width, CV_8UC1);
while (1)
{
if (myReader->AcquireLatestFrame(&myFrame) == S_OK) //通過Reader嘗試獲取最新的一幀深度數據,放入深度幀中,並判斷是否成功獲取
{
myFrame->CopyFrameDataToArray(height * width, (UINT16 *)temp.data); //先把數據存入16位的圖像矩陣中
temp.convertTo(depthImg, CV_8UC1, 255.0 / 4500); //再把16位轉換爲8位
imshow("TEST", depthImg);
sprintf(file_name, "depthImg + %d.jpg", a++);
imwrite(file_name, depthImg);
myFrame->Release();
}
if (waitKey(30) == VK_ESCAPE)
break;
}
myReader->Release(); //釋放不用的變量並且關閉感應器
mySource->Release();
mySensor->Close();
mySensor->Release();
return 0;
from: kinect2.0 opencv3.2 深度圖像提取(方式二)