KinectV2(一)---获取彩色图像

KinectV2(一)—获取彩色图像

V2彩色分辨率:1920x1080

#include 
#include 	//opencv头文件
#include 
#include 
#include 	//Kinect头文件

using namespace std;
using namespace cv;

int main(void)
{
	//初始化Kinect
	IKinectSensor* mySensor;
	HRESULT hResult = S_OK;
	hResult = GetDefaultKinectSensor(&mySensor);
	if (FAILED(hResult)) {
		cerr << "Error : GetDefaultKinectSensor" << std::endl;
		return -1;
	}
	hResult = mySensor->Open();
	if (FAILED(hResult)) {
		cerr << "Error : IKinectSensor::Open()" << std::endl;
		return -1;
	}

	//彩色帧源
	IColorFrameSource *myColorSource = nullptr;    
	hResult = mySensor->get_ColorFrameSource(&myColorSource);
	if (FAILED(hResult)) {
		cerr << "Error : IKinectSensor::get_ColorFrameSource()" << std::endl;
		return -1;
	}

	//彩色帧读取
	IColorFrameReader *myColorReader = nullptr;    
	hResult = myColorSource->OpenReader(&myColorReader);
	if (FAILED(hResult)) {
		cerr << "Error : IColorFrameSource::OpenReader()" << std::endl;
		return -1;
	}

	//Description
	IFrameDescription * myDescription = nullptr;
	hResult = myColorSource->get_FrameDescription(&myDescription);
	if (FAILED(hResult)) {
		cerr << "Error : IColorFrameSource::get_FrameDescription()" << std::endl;
		return -1;
	}
	int colorHeight = 0, colorWidth = 0;
	myDescription->get_Height(&colorHeight);
	myDescription->get_Width(&colorWidth);

	
	Mat original(colorHeight, colorWidth, CV_8UC4);
	while (1)
	{
		IColorFrame * myColorFrame = nullptr;
		while(myColorReader->AcquireLatestFrame(&myColorFrame) != NULL);
		myColorFrame->CopyConvertedFrameDataToArray(colorHeight * colorWidth * 4,
							original.data, ColorImageFormat_Bgra);
		Mat copy = original.clone();
		myColorFrame->Release();   //释放彩色帧
		resize(copy, copy, cv::Size(), 0.5, 0.5);
		imshow("test", copy);
		if (waitKey(30) == VK_ESCAPE)
			break;
		//Sleep(1000);
	}
	myColorSource->Release();
	myColorReader->Release();
	mySensor->Close();
	mySensor->Release();
	return 0;
}

KinectV2(一)---获取彩色图像_第1张图片

你可能感兴趣的:(KinectV2学习)