I want to get depth and RGB data from Kinect V2 by Windows SDK 2.0 in Viual Studio 2013. So I write these codes:
#include <Kinect.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <Windows.h>
#include <iostream>
using namespace std;
const int width = 512;
const int height = 424;
const int colorwidth = 1920;
const int colorheight = 1080;
// Kinect Variables
IKinectSensor* sensor; // Kinect sensor
IMultiSourceFrameReader* reader; // Kinect data source
ICoordinateMapper* mapper;
int main(int argc, char* argv[]) {
if (FAILED(GetDefaultKinectSensor(&sensor))) {
printf("not found sensor");
getchar();
return -100;
}
if (sensor) {
sensor->get_CoordinateMapper(&mapper);
sensor->Open();
sensor->OpenMultiSourceFrameReader(
FrameSourceTypes::FrameSourceTypes_Depth | FrameSourceTypes::FrameSourceTypes_Color,
&reader);
IMultiSourceFrame* framesource;
cout << "Find!!!";
getchar();
return 100;
}
else {
return -100;
}
getchar();
return 10;
}
Logically, when I do not plug Kinect sensor to my Laptop, in Console should be printed: "not found sensor",isn't it? But, in console printed: "Find!!!". what is the problem?
No, Since SDK v2, You can debug an application by using KinectStudio, even without a physical kinect is connected to the system. If you want to check whether the actual Kinect is connected, you need to use IsAvailable
property of the sensor itself. GetDefaultKinectSensor
will always give you S_OK
unless you have runtime or installation issues. GetDefaultKinectSensor
won't check whether is this stream comes from actual physical kinect sensor or from KinectStudio.
The following is a code snippet from my project. If you want more examples about Kinect, please refer my project in github https://github.com/shanilfernando/VRInteraction or comment here. I'm more than happy to help you.
HRESULT CDepthBasics::InitializeDefaultSensor()
{
HRESULT hr;
hr = GetDefaultKinectSensor(&m_pKinectSensor);
if (FAILED(hr))
{
return hr;
}
if (m_pKinectSensor)
{
if (SUCCEEDED(hr))
{
hr = m_pKinectSensor->get_CoordinateMapper(&m_pCoordinateMapper);
}
if (SUCCEEDED(hr))
{
hr = m_pKinectSensor->Open();
}
if (SUCCEEDED(hr))
{
hr = m_pKinectSensor->OpenMultiSourceFrameReader(
FrameSourceTypes::FrameSourceTypes_Depth | FrameSourceTypes::FrameSourceTypes_Color | FrameSourceTypes::FrameSourceTypes_Body | FrameSourceTypes::FrameSourceTypes_BodyIndex,
&m_pMultiSourceFrameReader);
}
}
if (!m_pKinectSensor || FAILED(hr))
{
std::cout << "No ready Kinect found!" << std::endl;
return E_FAIL;
}
else
{
std::cout << "Kinect found!" << std::endl;
}
return hr;
}
https://github.com/shanilfernando/VRInteraction/blob/master/DepthBasics.cpp
Edit
I didn't use IsAvailable
since it doesn't matter for me where that stream comes from. Sometimes I used KinectStudio to get the streams while Kinect is not with me. I said GetDefaultKinectSensor
will return S_OK
unless you have runtime or installation issues, I did NOT said GetDefaultKinectSensor
will always give you a valid m_pKinectSensor
. Since we don't have access to the implementation of the GetDefaultKinectSensor
and as a good practice, it is better to check it null
or not before using it. This is the official answer for your question from the Microsoft