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

NtKinect: How to get RGB camera image with Kinect V2 (Fundamental Settings)


2016.07.13: created by
2017.08.30: revised by
2017.10.07: revised by
Japanese English
To Table of Contents

Prerequisite necessary items

It is assumed that the following softwares are installed. If the installation path is different, please read as appropriate.


The projects distributed in the following explanation is created by "Visual Studio Community 2017" and "OpenCV 3.3".


NtKinect: A class for easily using Kinect V2 in C++ programming language

When you declare a variable of NtKinect class in your program, Kinect V2 will be initialized and become available.

    #include "NtKinect.h"
    ...
    NtKinect kinect;     // variable declaration

Various sensors of Kinect V2 are initialized when first used. For example, an RGB camera is initialized when the setRGB() function is called for the first time.

    kinect.setRGB();    // Getting an RGB image. The first call initializeds the RGB camera.


Getting RGB camera image with Kinect V2

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

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'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
    

How to write program

  1. Start a new project in Visual Studio 2017
  2. [Before "Visual Studio 2017 Update 2"]
    1. The programming language is "Visual C++", and select "Win32 console application".Here the project name is "KinectV2".



    2. In the Win32 application wizard, select "Next".



    3. Check "Empty Project" and select "Complete"



    ["Visual Studio 2017 Update 2" or later]
    1. Select "File" -> "New" -> "Project"-> "Visual C++" -> "Windows desktop" -> "Windows desktop wizards"
    2. The name here is "KinectV2". The solution name will automatically be "KinectV2". Click OK button.




    3. In "Windows desktop project" window, select "Console Application (.exe)" in "Application Type", check out the "Empty project" option for additional options.



  3. Change the target architecture of the project to "x64".
  4. Near the center of the menu bar at the top of the Visual Studio window, there is a panel for specifying compile options. Change this to "x64". Also, change "Debug" to "Release" so that execution becomes faster.




  5. Add "main.cpp" to the project source file.
  6. If at least one C++ source file does not exist in the project, "C/C++" item can not be selected in project properties. Therefore, first add "main.cpp" here.










  7. Set the project properties.
    1. Right-click the project name "KinectV2" in "Solution explorer" panel and select "Properties".



    2. Add include paths of Kinect and OpenCV to "C/C++" "General" "Additional include directory" in "Configuration Properties".
    3.   $(KINECTSDK20_DIR)inc
        C:\opencv\include




    4. Add library paths of Kinect and OpenCV to "Linker" "General" "Additional library directory" in "Configuration Properties".
    5.   $(KINECTSDK20_DIR)Lib/x64
        C:\opencv\lib




    6. Add libraries of Kinect and OpenCV to "Linker" "Input" "Additinal dependent files" in "Configuration Properties".
    7.   Kinect20.lib
        opencv_world330.lib




      [CAUTION] If you run the program in Debug mode, you must specify opencv_world330d.lib as a library file of OpenCV in Debug mode of the properties.

  8. (Important)Add NtKinect.h ( this site, github ) to the project's header file.
  9. Please download "NtKinect.h" from the above link. The location of the file is the folder where the source file such as "main.cpp" is placed. In this example, it is "KinectV2/KinectV2/NtKinect.h".







    If you get an error "Cannot include NtKinect.h" at compile time, delete the "NtKinect.h" once. Then, right click "Header file" of "Solution Explorer", and select "Add" "New item" "VisualC++ header file (.h)" from th menu and specify "NtKinect.h" for "Name". Please copy the contents of the downloaded file to the newly created one.

  10. Change the contents of "main.cpp" as follows.
  11. You can easily use various functions of Kinect V2 by rewriting the red letter part.

    main.cpp
    #include <iostream>
    #include <sstream>
    
    #include "NtKinect.h"
    
    using namespace std;
    
    void doJob() {
      NtKinect kinect;
      while (1) {
        kinect.setRGB();
        cv::imshow("rgb", 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;
    }
    
    

    The function whose name is preceded by "cv::" belongs to OpenCV.

    type of return value OpenCV function name descriptions manual
    void imshow(cost string& winname, const Mat& image ) display image in the window named winname. link
    int waitKey(int delay =0) Wait key input for delay milliseconds. 0 means to wait infinitely. link
  12. When you run the program, RGB images are displayed. Exit with 'q' key.



  13. Please click here for this sample project KinectV2.zip
  14. 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/