-1

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?

ma98
  • 83
  • 3
  • 14
  • If it doesn't find a default Kinect sensor, will it set sensor to NULL? If not, perhaps try initializing your sensor to NULL. Maybe it successfully found no sensors and didn't set the sensor pointer? I don't know much about Kinect programming, so I may be wrong. :) – David Vereb Jul 07 '17 at 19:52

1 Answers1

1

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

Shanil Fernando
  • 1,302
  • 11
  • 13
  • On your github I see that you initialize m_pKinectSensor to NULL. Since the code leading up to GetDefaultKinectSensor in your project looks about the same, I'm thinking it's the uninitialized pointer causing @masoomi's problems. – David Vereb Jul 10 '17 at 12:14
  • 1
    @DavidVereb Please check the Edit, I added the answer for your confusion there. – Shanil Fernando Jul 10 '17 at 12:39
  • Thanks for the extra information! I tried to find `IsAvailable` in your github project but was unable to locate it. I'm still curious, though. If `GetDefaultKinectSensor` always returns a good sensor pointer on `S_OK` (whether pointing to a real kinect device or a KinectStudio emulated device), why even have the check `if(m_pKinectSensor)`? – David Vereb Jul 10 '17 at 15:14
  • Also, after reading [this Stack Overflow response](https://stackoverflow.com/a/30899735/1364178) it seems the \_COM_Outptr_ annotation guarantees the pointer is set to NULL on failure, so forgetting to initialize to NULL definitely wouldn't be the issue. – David Vereb Jul 10 '17 at 15:16
  • I wish we did have access to the implementation. I'm used to open source. Haha. Thanks for entertaining my thought process, though. Too bad OP has nothing to say. :( – David Vereb Jul 10 '17 at 18:20
  • 1
    If you want open source, check OPENNI. It is the heart of the Kinect SDK. OPENNI was developed by one of Israel company. Microsoft used OPENNI to build there SDK and later APPLE brought the whole company and shutdown the OPENNI opensource project. But still you can find the OPENNI folk. I kept one copy myself since it is a very useful. All the skeleton tracking algorithms and much more are implemented in there and they are the base for Kinect Official SDK and for the APPLE's Structure Sensor SDK . – Shanil Fernando Jul 10 '17 at 18:29