Search code examples
c++visual-studiokinect-sdkkinect-v2color-depth

Kinect V2 read Depth issue


The following C++ code is given which continuously fetches the latest frame from a Kinect 2.

#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;
IDepthFrameReader* reader;     // Kinect depth data source
IKinectSensor* sensor = nullptr;
int main(int argc, char* argv[]) {
    if (FAILED(GetDefaultKinectSensor(&sensor))) {
        return 19;
    }
    if (sensor) {
        sensor->Open();
        IDepthFrameSource* framesource = NULL;
        sensor->get_DepthFrameSource(&framesource);
        framesource->OpenReader(&reader);
        if (framesource) {
            framesource->Release();
            framesource = NULL;
        }
        IDepthFrame* frame = NULL;
        if (SUCCEEDED(reader->AcquireLatestFrame(&frame))) {
            cout << "not bad";
            getchar();
            return 100;
        }
        else{
            cout << "not found";
            getchar();
            return 23;
        }
    }
    else {
        return -1;
    }
}

In fact, if I connect Kinect to my laptop or not, the output is no changing and it is: "not found". When I connect Kinect and run the program, the lights of Kinect turns on. In some ready codes, Kinect works correctly. Where is the problem?


Solution

  • You should check out the returned error code for AcquireLatestFrame, maybe it can tell you what's the problem.

    Here's a tip: maybe, when you call AcquireLatestFrame, the frame is not available yet. So put this call into a loop, sooner or later the frame will be available.