NtKinect: Kinect V2 C++ Programming with OpenCV on Windows10

How to get Depth image with Kinect V2


2016.07.19: created by
Japanese English
To Table of Contents

Prerequisite knowledge


Getting Depth image

Depth (distance) images can be acquired with a resolution of 512 x 424. The measurable distance range is from 500 mm to 8000 mm, but the range to recognize human beings is from 500 mm to 4500 mm.

In the Kinect20.lib IDepthFrameSource has the "get_DepthMinReliableDistance()" and "get_DepthMaxReliableDistance()" functions, each returns 500 and 4500 respectively.

In NtKinect, the obtained Depth image is represented by UINT16 (16 bit unsigned integer) for each pixel.

NtKinect

NtKinect's function for Depth image

type of return value function name descriptions
void setDepth(bool raw = true) Set the Depth image to the member variable "depthImage".
When this function is called with no argument or "true" as first argument, the distance is set in mm for each pixel.
When this function is called with "false", a value obtained by multiplying the distance by 65535/4500 is set for each pixel. That is, the image is mapped to the luminance of the black and white image of 0 (black) to 65535 (white) with the distance of 0 mm to 4500 mm.
NtKinect

NtKinect's member variable for Depth iamge

type variable name descriptions
cv::Mat depthImage Depth image. The resolution is of 512 x 424 and each pixel is represented by UINT16.
The coordinates of the image are the position in the DepthSpace coordinate system.
  • depthImage.cols --- Resolution in the horizontal direction of the image (= 512)
  • depthImage.rows --- Resolution in the vertical direction of the image (= 424)
  • depthImage.at<UINT16>(y , x ) --- Access pixel in the (x , y ) coordinates of the image
  •         UINT16 depth = rgbImage.at<UINT16>(y , x );
    

How to write program

  1. Start using the Visual Studio's project KinectV2.zip of "NtKinect: How to get RGB camera image with Kinect V2 (Fundamental Settings)"
  2. Change the contents of "main.cpp" as follows.
  3. Call the kinect.setDepth() function and set distance data to kinect.depthImage. Since "false" is specified as an argument, the distance data is multiplied by 65535/4500.

    The Depth data at the 500mm far point is 7281 (11%, black color), and the data at the 4500 mm point is 65535 (100%, white color).

    main.cpp
    #include <iostream>
    #include <sstream>
    
    #include "NtKinect.h"
    
    using namespace std;
    
    void doJob() {
      NtKinect kinect;
      while (1) {
        kinect.setDepth(false);
        cv::imshow("depth", kinect.depthImage);
        auto key = cv::waitKey(1);
        if (key == 'q') break;
      }
      cv::destroyAllWindows();
    }
    
    int main(int argc, char** argv) {
      try {
        doJob();
      } catch (exception &ex) {
        cout << ex.what() << endl;
        string s;
        cin >> s;
      }
      return 0;
    }
    
  4. When you run the program, Depth images are displayed. Exit with 'q' key.
  5. Since "false" is specified in the setDepth() function call, the Depth image displayed here is an image showing a close point (distance 0 mm) as black (0x0000) and a distant point (distance 4500 mm) as white (0xffff).




  6. Please click here for this sample project KinectV2_depth.zip
  7. Since the above zip file may not include the latest "NtKinect.h", Download the latest version from here and replace old one with it.



http://nw.tsuda.ac.jp/