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


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.

授業で配布するプリントを参照して下さい。 授業で配布するプリントを参照して下さい。

How to write program

  1. Start a new project in Visual Studio 2015
    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"



  2. Change the target architecture of the project to "x64".
  3. 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".




  4. Add "main.cpp" to the project source file.
  5. 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.







  6. 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
        D:\opencv\include



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



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



  7. (Important)Add NtKinect.h ( this site, github ) to the project's header file.
  8. 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.

  9. Change the contents of "main.cpp" as follows.
  10. 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
  11. When you run the program, RGB images are displayed. Exit with 'q' key.



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