问候语,
我正在使用Kinect 360在C ++中开发一个应用程序。我在运行时遇到了KinectGrabber的问题,它没有像我希望的那样在运行时初始化相机。在调试它时,我发现在测试“hr”的值时它在“MSFTKinectGrabber :: Init”中出错。这个测试尽职尽责地打断了程序,但我不知道为什么。
我的设置:
当程序进入IF语句(@“MSFTKinectGrabber :: Init”)时,这是一切似乎出错的地方:
hr = m_NuiSensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH, NUI_IMAGE_RESOLUTION_640x480,
depthFlags, 2, m_NextDepthFrameEvent, &m_pDepthStreamHandle);
if( FAILED( hr ) )
{
std::cout << "Error: NuiImageStreamOpen " << hr << std::endl;
return false;
}
下面我有完整的代码:
功能:主要
int main(int argc, char** argv){
processor = new KinectDataProcessor();
MSFTKinectGrabber grabberMSFT;
int kinect_state = initializeKinect(*processor, grabberMSFT, argc, argv);
if(kinect_state==0){
cloudProcessor = new CloudProcessor();
int v=0;
while (run){
v++;
HandleKeyboardEvents();
processor->Update();
if (startClassify && !continueClassify){
continueClassify = true;
boost::thread classificationThread(&threadClassify);
}
}
grabberMSFT.DeInit();
processor->DeInit();
}else{
system("PAUSE");
return -1;
}
return 0;
}
功能: initializeKinect
int initializeKinect(KinectDataProcessor & processor, MSFTKinectGrabber & grabberMSFT, int argc,char** argv){
int nRetCode = 0;
if (hModule != NULL)
{
// initialize MFC and print and error on failure
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
std::cout << "Kinect Parameters: " << std::endl;
std::cout << "1 - MinDepth (float) - minimum allowed is '0.0f'" << std::endl;
std::cout << "2 - MaxDepth (float) - maximum allowed is '4.0f'" << std::endl;
std::cout << "3 - NearMode (int) - '0' for true, '1' for false" << std::endl;
float minDepth = MIN_DEPTH;//float (atof(argv[1]));
float maxDepth = MAX_DEPTH;//float (atof(argv[2]));
std::cout << "[MinDepth, MaxDepth] = [" << minDepth << ", " << maxDepth << "]" << std::endl;
bool nearModeActive = true;
std::cout << "Near mode selected\n";
//KinectDataProcessor processor;
if(!processor.Init(KinectProcessor::GrabberType::MSFT))
{
processor.DeInit();
run = false;
return -1;
}
processor.SetDepthParameters(minDepth, maxDepth, nearModeActive);
//MSFTKinectGrabber grabberMSFT;
if (!grabberMSFT.Init(&processor)){
grabberMSFT.DeInit();
processor.DeInit();
run = false;
return -1;
}
cout<<"Ready to run"<<endl;
}
}
else
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
nRetCode = 1;
}
return nRetCode;
}
功能: MSFTKinectGrabber :: Init
bool MSFTKinectGrabber::Init(KinectProcessor * kinectProcessor)
{
m_KinectProcessor = kinectProcessor;
//get NuiSensor obj
HRESULT hr = NuiCreateSensorByIndex(0, &m_NuiSensor);
if ( FAILED(hr) )
{
std::cout << "Error: NuiCreateSensorByIndex " << hr << std::endl;
return false;
}
m_DeviceID = m_NuiSensor->NuiDeviceConnectionId();
hr = m_NuiSensor->NuiInitialize( NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH );
if( FAILED(hr) )
{
std::cout << "Error: NuiInitialize " << hr << std::endl;
return false;
}
//Create color and depth events
m_NextColorFrameEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
m_NextDepthFrameEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
//open video streams
hr = m_NuiSensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, 0, 2, m_NextColorFrameEvent, &m_pColorStreamHandle);
if( FAILED( hr ) )
{
std::cout << "Error: NuiImageStreamOpen " << hr << std::endl;
return false;
}
DWORD depthFlags = NULL;
if(kinectProcessor->IsMSFTNearMode())
depthFlags = NUI_IMAGE_STREAM_FLAG_ENABLE_NEAR_MODE;
hr = m_NuiSensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH, NUI_IMAGE_RESOLUTION_640x480, depthFlags, 2, m_NextDepthFrameEvent, &m_pDepthStreamHandle);
if( FAILED( hr ) )
{
std::cout << "Error: NuiImageStreamOpen " << hr << std::endl;
return false;
}
// Start the NUI processing thread
m_ProcessThreadStopEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
m_ProcessThread = CreateThread( NULL, 0, ProcessThread, this, 0, NULL );
return true;
}
感谢您的时间!
答案 0 :(得分:0)
快速回答:
如果你的Kinect有&#34; XBOX360&#34;写在它的正面而不是你不能使用近模式,你需要将nearModeActive从true更改为false。
更多详情:
我认为问题在于您尝试使用常规Kinect的近模式。在您的initializeKinect中,以下是硬编码:
bool nearModeActive = true;
std::cout << "Near mode selected\n";
然后
processor.SetDepthParameters(minDepth, maxDepth, nearModeActive);
MSFTKinectGrabber :: Init然后尝试以近模式打开深度流,原因如下:
if(kinectProcessor->IsMSFTNearMode())
depthFlags = NUI_IMAGE_STREAM_FLAG_ENABLE_NEAR_MODE;
问题是:常规XBOX Kinect不支持此功能。只有适用于Windows的Kinect。事实上,这是他们之间唯一真正的区别。 Kinect for Windows为Configure Apps with Over 64K Methods。您可以阅读有关近模式much more expensive的更多信息。