c_cpp 从Kinect v2中检索原始颜色数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 从Kinect v2中检索原始颜色数据相关的知识,希望对你有一定的参考价值。

#include "stdafx.h"
#include <Windows.h>
#include <Kinect.h>
#include <opencv2/opencv.hpp>


template<class Interface>
inline void SafeRelease( Interface *& pInterfaceToRelease )
{
	if( pInterfaceToRelease != NULL ){
		pInterfaceToRelease->Release();
		pInterfaceToRelease = NULL;
	}
}

int _tmain( int argc, _TCHAR* argv[] )
{
	cv::setUseOptimized( true );

	// Sensor
	IKinectSensor* pSensor;
	HRESULT hResult = S_OK;
	hResult = GetDefaultKinectSensor( &pSensor );
	if( FAILED( hResult ) ){
		std::cerr << "Error : GetDefaultKinectSensor" << std::endl;
		return -1;
	}

	hResult = pSensor->Open();
	if( FAILED( hResult ) ){
		std::cerr << "Error : IKinectSensor::Open()" << std::endl;
		return -1;
	}

	// Source
	IColorFrameSource* pColorSource;
	hResult = pSensor->get_ColorFrameSource( &pColorSource );
	if( FAILED( hResult ) ){
		std::cerr << "Error : IKinectSensor::get_ColorFrameSource()" << std::endl;
		return -1;
	}

	// Reader
	IColorFrameReader* pColorReader;
	hResult = pColorSource->OpenReader( &pColorReader );
	if( FAILED( hResult ) ){
		std::cerr << "Error : IColorFrameSource::OpenReader()" << std::endl;
		return -1;
	}

	// Description
	IFrameDescription* pDescription;
	hResult = pColorSource->get_FrameDescription( &pDescription );
	if( FAILED( hResult ) ){
		std::cerr << "Error : IColorFrameSource::get_FrameDescription()" << std::endl;
		return -1;
	}

	int width = 0;
	int height = 0;
	pDescription->get_Width( &width ); // 1920
	pDescription->get_Height( &height ); // 1080

	cv::Mat colorMat( height, width, CV_8UC3 );
	cv::namedWindow( "Color" );

	while( 1 ){
		// Frame
		IColorFrame* pColorFrame = nullptr;
		hResult = pColorReader->AcquireLatestFrame( &pColorFrame );
		if( SUCCEEDED( hResult ) ){
			UINT bufferSize = 0;
			BYTE* pBuffer = nullptr;
			hResult = pColorFrame->AccessRawUnderlyingBuffer( &bufferSize, &pBuffer ); // YUY2
			if( SUCCEEDED( hResult ) ){
				cv::Mat bufferMat( height, width, CV_8UC2, pBuffer );
				cv::cvtColor( bufferMat, colorMat, CV_YUV2BGR_YUYV );
			}
		}
		SafeRelease( pColorFrame );

		// Draw
		cv::imshow( "Color", colorMat );
		if( cv::waitKey( 30 ) == VK_ESCAPE ){
			break;
		}
	}

	// Release
	SafeRelease( pColorSource );
	SafeRelease( pColorReader );
	SafeRelease( pDescription );
	if( pSensor ){
		pSensor->Close();
	}
	SafeRelease( pSensor );
	cv::destroyAllWindows();

	return 0;
}

以上是关于c_cpp 从Kinect v2中检索原始颜色数据的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 使用KinectGrabber / Kinect2Grabber从Kinect v1 / v2传感器检索的pcl :: PointCloud <pcl :: PointXYZRGBA

c_cpp 使用不带Grabber的Point Cloud Library绘制从Kinect v2检索到的Point Cloud

Kinect V2 中原始数据的单位是啥?

c_cpp 为Kinect SDK v2正文数据序列化Boost.Serialization(XML)的定义

c_cpp Kinect v2坐标系统映射

c_cpp 使用PCLVisualizer和Kinect v2 Grabber绘制点云