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

How to make RGB image correspond to Depth image with Kinect V2


2016.07.19: created by
Japanese English
To Table of Contents

Prerequisite knowledge


Getting RGB came image with Kinect V2

Kinect V2 can acquire RGB camera image with resolution of 1920 x 1080. Since OpenCV uses BGR format or BGRA format as the basis, NtKinect adopts the BGRA format.

NtKinect

NtKinect's member functions for RGB image

type of return value function name description
void setRGB() Get the RGB image and set it to the public member variable rgbImage.
NtKinect

NtKinect's member variable for RGB image

type variable name description
cv::Mat rgbImage Image of RGB camera. The resolution is 1920 x 1080 and GBRA format.
The coordinates of the image are the positions in the ColorSpace coordinate system.
  • rgbImage.cols --- Resolution in the horizontal direction of the image (=1920)
  • rgbImage.rows --- Resolution in the vertical direction of the image (=1080)
  • rgbImage.at<cv::Vec4b>(y , x ) --- Access the pixel in the (x , y ) coordinates of the image
  •         cv::Vec4b pixel = rgbImage.at<cv::Vec4b>(y,x);
                pixel[0] // Blue
                pixel[1] // Green
                pixel[2] // Red
                pixel[3] // Alpha
    

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_DepthMaxReliableDistance()" 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 );
    

NtKinect

3 types of coordinate system of Kinect V2

Since the position and resolution of each sensor is different, the data is obtained as a value expressed in the coordinate system of each sensor. When using data obtained from different sensors at the same time, it is necessary to convert the coordinates to match.

Kinect V2 has 3 coordinate systems, ColorSpace, DepthSpace, and CameraSpace. There are 3 data types ColorSpacePoint, DepthSpacePoint, and CameraSpacePoint representing coordinates in each coordinate system.

Quoted from Kinect.h of Kinect for Windows SDK 2.0
typedef struct _ColorSpacePoint {
    float X;
    float Y;
} ColorSpacePoint;

typedef struct _DepthSpacePoint {
    float X;
    float Y;
} DepthSpacePoint;

typedef struct _CameraSpacePoint {
    float X;
    float Y;
    float Z;
} CameraSpacePoint;
NtKinect

Coordinate systems and data types of Kinect V2

For the RGB image, Depth image, and skeleton information, the coordinate system is different. The coordinate system of the RGB image is ColorSpace, that of the Depth image is DepthSpace, and that of the skeleton information is CameraSpace.

Coordinate systemtype of coordinatesCaptured Data
ColorSpaceColorSpacePointRGB image
DepthSpaceDepthSpacePointdepth image, bodyIndex image, infrared image
CameraSpaceCameraSpacePointskeleton information



CameraSpace coordinate system representing skeleton position

The CameraSpace is a 3-dimensional coordinate system with the following features.

  • Kinect V2 is located at the origin of the coordinate system.
  • The direction of the camera lense is the positive direction of the z-axis.
  • Vertical upward direction is positive direction of y-axis.
  • Right-handed.
That is, in all 3 types of coordinate systems, CameraSpace, ColorSpace, and DepthSpace, "the horizontal direction from left to right seen from the user facing Kienct V2" is the positive direction of the x-axis. I think you may understand "Data is aquired and displayed as if the image reflected in the mirror is seen from the user facing Kinect V2".
(2016/11/12 figure changed, and description added).

NtKinect

Kinect V2's function for mapping coordinate systems

"Coordinate system conversion function" held by ICoordinateMapper class of Kinect V2 is as follows.

type of return value function name descriptions
HRESULT MapCameraPointToColorSpace(
    CameraSpacePoint sp ,
    ColorSpacePoint *cp )
Convert the coordinates sp in the CameraSpace to the coordinates cp in the ColorSpace. Return value is S_OK or error code.
HRESULT MapCameraPointToDepthSpace(
  CameraSpacePoint sp ,
  DelpthSpacePoint *dp )
Convert the coordinates sp in the CameraSpace to the coordinates dp in DepthSpace. Return value is S_OK or error code.
HRESULT MapDepthPointToColorSpace(
  DepthSpacePoint dp ,
  UINT16 depth ,
  ColorSpacePoint *cp )
Convert the coordinates dp in DepthSpace and distance depth to the coordinates cp in ColorSpace. Return value is S_OK or error code.
HRESULT MapDepthPointToCameraSpace(
  DepthSpacePoint dp ,
  UINT16 depth ,
  CameraSpacePoint *sp )
Convert the coordinates dp in DepthSpace and distance depth to the coordinates sp in CameraSpace. Return value is S_OK or error code.
NtKinect

NtKinect's member variable for mapping coordinate system

An instance of ICoordinateMapper class used for mapping coordinate systems in Kinect V2 is held in NtKinect's member variable "coordinateMapper".

type variable name descriptions
CComPtr<ICoordinateMapper> coordinateMapper An instance of ICoordinateMapper used for mapping coordinate systems.

How to write program

  • Start using the Visual Studio's project KinectV2.zip of "NtKinect: How to get RGB camera image with Kinect V2 (Fundamental Settings)"
  • Change the contents of main.cpp.
  • Call kinect.setDepth() function to set depth (distance) data to kinect.depthImage. Since no argument is specified, the value of pixel is raw, that is, the distance to the object in millimeters.

    By painting pixels farther than 2000 mm with blue, only images of nearby objects are displayed. Since we can not measure a value smaller than 500 mm, we treat that pixel as being more than 8000 mm.

    Since RGB image and Depth image are different in resolution, one pixel of Depth image corresponds to 4 pixels of the RGB image and the 4 x 4 rectangle area is painted.

    main.cpp
    #include <iostream>
    #include <sstream>
    
    #include "NtKinect.h"
    
    using namespace std;
    
    void doJob() {
      NtKinect kinect;
      while (1) {
        kinect.setRGB();
        kinect.setDepth();
        for (int y=0; y<kinect.depthImage.rows; y++) {
          for (int x=0; x<kinect.depthImage.cols; x++) {
            UINT16 d = kinect.depthImage.at<UINT16>(y,x);
            DepthSpacePoint dp; dp.X=x; dp.Y=y;
            ColorSpacePoint cp;
            HRESULT hr = kinect.coordinateMapper->MapDepthPointToColorSpace(dp,d,&cp);
            if (hr != S_OK) continue;
            if (d > 2000 || d < 500) {
              int ax = (int) cp.X;
              int ay = (int) cp.Y;
              cv::rectangle(kinect.rgbImage,cv::Rect(ax-2,ay-2,4,4),cv::Scalar(255,0,0),2);
            }
          }
        }
        cv::imshow("rbg near", kinect.rgbImage);
        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;
    }
    
  • When you run the program, RGB images are displayed. Exit with 'q' key.



  • Please click here for this sample project KinectV2_depth2.zip
  • 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/