我实际上正在使用Kinect V2(Xbox One的那个),我正在尝试使用深度流。我想看看Kinect的深度传感器看到了什么。但我没有成功打开一个流。我只能用另一段代码打开一个帧,但不是一个视频。通过一些研究,我尝试使用句柄,但是我写的代码并没有在屏幕上打印出“' stream'放在代码的末尾。我正在使用VS2012,代码是用C ++编写的。
我想我有这个因为我不知道如何使用正确的句柄...如果有人可以帮助我并向我解释什么是句柄而不是指向某个东西的指针,那将是大。谢谢
这是我的代码:
HRESULT hr=S_OK;
WAITABLE_HANDLE *stream=nullptr;
IKinectSensor* kinectSensor=nullptr;
if( SUCCEEDED(hr) )
{
std::cout << "Success IKinectSensor::GetDefaultSensor" << std::endl;
}
else
{
std::cout << "Failed IKinectSensor::GetDefaultSensor" << std::endl;
}
std::cout << "Opening sensors" << std::endl;
if(kinectSensor != NULL)
{
hr = kinectSensor->Open();
Sleep(sleeptime*5);
if( SUCCEEDED( hr ) )
{
std::cout << "Success IKinectSensor::Open" << std::endl;
}
else
{
std::cout << "Failed IKinectSensor::Open" << std::endl;
}
}
}
hr = kinectSensor->OpenMultiSourceFrameReader(FrameSourceTypes_Depth | FrameSourceTypes_Color , &multiSourceReader);
if( SUCCEEDED(hr) )
{
std::cout << "reader open" << std::endl;
hr = multiSourceReader->SubscribeMultiSourceFrameArrived(stream);
if( SUCCEEDED(hr) )
{
std::cout << "stream" << std::endl;
}
}
答案 0 :(得分:0)
我没有使用手柄。请参阅以下代码片段,以获取多源帧读取器的深度帧并使用OpenCV显示它。
#include <Kinect.h>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
// helper function
template <class Interface> inline void safe_release(Interface **ppT)
{
if (*ppT)
{
(*ppT)->Release();
*ppT = NULL;
}
}
bool setup_kinect_sensor_and_acquire_frame()
{
IKinectSensor* kinect_sensor
// initialize kinect sensor
HRESULT hr = GetDefaultKinectSensor(&kinect_sensor);
if (FAILED(hr) || !kinect_sensor)
{
safe_release(&p_multisource_frame);
return false;
}
kinect_sensor->Open();
if (FAILED(hr))
{
return false;
}
// initialize kinect multisource frame reader
IMultiSourceFrameReader* kinect_multisource_reader;
hr = kinect_sensor->OpenMultiSourceFrameReader(FrameSourceTypes_Depth, &kinect_multisource_reader;);
if (FAILED(hr))
{
safe_release(&kinect_multisource_reader);
return false;
}
// acquire multisource frame
IMultiSourceFrame* p_multisource_frame = NULL;
HRESULT hr = kinect_multisource_reader->AcquireLatestFrame(&p_multisource_frame);
if (FAILED(hr))
{
safe_release(&p_multisource_frame);
return false;
}
// get depth frame
IDepthFrameReference* p_depth_frame_ref = NULL;
hr = p_multisource_frame->get_DepthFrameReference(&p_depth_frame_ref);
if (FAILED(hr))
{
safe_release(&p_depth_frame_ref);
return false;
}
IDepthFrame* p_depth_frame = NULL;
IFrameDescription* p_frame_description = NULL;
int width = 0;
int height = 0;
unsigned short depth_min_distance = 0;
unsigned short depth_max_distance = 0;
unsigned short* p_depth_buffer = NULL;
hr = p_depth_frame_ref->AcquireFrame(&p_depth_frame);
if (SUCCEEDED(hr))
{
hr = p_depth_frame->get_FrameDescription(&p_frame_description);
}
if (SUCCEEDED(hr))
{
hr = p_frame_description->get_Width(&width);
}
if (SUCCEEDED(hr))
{
hr = p_frame_description->get_Height(&height);
}
if (width != 512 || height != 424)
{
safe_release(&p_depth_frame);
safe_release(&p_frame_description);
return false;
}
// process depth frame
if (SUCCEEDED(hr))
{
hr = p_depth_frame->get_DepthMinReliableDistance(&depth_min_distance);
}
if (SUCCEEDED(hr))
{
hr = p_depth_frame->get_DepthMaxReliableDistance(&depth_max_distance);
}
if (SUCCEEDED(hr))
{
int size = 512 * 424;
p_depth_buffer = new unsigned short[size];
hr = p_depth_frame->CopyFrameDataToArray(size, p_depth_buffer);
if (SUCCEEDED(hr))
{
cv::Mat depth_map(cv::Size(DEPTH_WIDTH, DEPTH_HEIGHT), CV_16UC1, p_depth_buffer);
double scale = 255.0 / (depth_max_distance - depth_min_distance);
depth_map.convertTo(depth_frame, CV_8UC1, scale);
cv::imshow("depth", depth_map);
}
}
// Clean up depth frame
safe_release(&p_depth_frame_ref);
safe_release(&p_depth_frame);
safe_release(&p_frame_description);
if (p_depth_buffer != NULL) {
delete[] p_depth_buffer;
p_depth_buffer = NULL;
}
if (FAILED(hr))
{
return false;
}
else
{
return true;
}
}