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

How to record video with Kinect V2


2016.07.20: created by
Japanese English
To Table of Contents

Prerequisite knowledge


OpenCV Video creation class cv::VideoWriter

Constructor and methods of cv::VideoWriter class

OpenCV cv::VideoWriter Class Reference
type of return value method name descriptions
Constructor cv::VideoWriter(const String& filename ,
    int fourcc ,
    double fps ,
    cv::Size frameSize ,
    bool isColor = true )
Constructor
Arguments
filename
file name of output video
fourcc
4 characters code of codec Video Codecs by FOURCC
fps
Frames Per Second
frameSize
Vide Frame Size
isColor
Color or monochrome
  • ".avi" --- XVID
  • ".mp4" --- FMP4, MP4V
bool isOpened() Whether VideoWriter is initialized correctly or not
void write(const cv::Mat& image ) write image to stream as a video frame
VideoWriter& operator<<(const cv::Mat& image ) write image to stream as a video frame
void release() close VideoWriter

OpenCV has cv::VideoWriter which creates movies from multiple cv::Mat. Among the movie format supported by cv::VideoWriter, the ".avi" file can be played on a large number of platforms. However, the format of cv::Mat image to be written can not be BGRA, but BGR. So, the image format conversion is needed. You also need to convert the resolution and make it smaller.

    NtKinect kinect;
    cv::Size sz(640,480);
    cv::VideoWriter vw("videofile.avi", CV_FOURCC_MACRO('X','V','I','D'), 20.0, sz, true);
    if (! vw.isOpened()) throw runtime_error("cannot create videowriter");
    cv:Mat img ;  // prepare new cv::Mat
    while (1) {
        kinect.setRGB();
        cv::resize(kinect.rgbImage, img, sz, 0, 0); // change resolution to 640 x 480
        cv::cvtColor(kinect.rgbImage, img , CV_BGRA2BGR); // change image format from BGRA to BGR
        vw << img;     // as same as vw.write(img)
        ...
    }
    vw.release();

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. Prepare cv::VideoWriter.

    Call kinect.setRGB() function, and set RGB image to kinect.rgbImage variable. Each pixel of kinect.rgbImage is BGRA format, you convert it into BGR format and write to cv::VideoWriter.

    AVI file is generated whose filename is composed of date and time.

    Using the code commented out will generate an mp4 file. Whether to use "avi" or "mp4" as the video format depends on the playing environment of the video.

    main.cpp
    #include <iostream>
    #include <sstream>
    
    #include "NtKinect.h"
    
    using namespace std;
    
    #include <time.h>
    string now() {
      char s[1024];
      time_t t = time(NULL);
      struct tm lnow;
      localtime_s(&lnow, &t);
      sprintf_s(s, "%04d-%02d-%02d_%02d-%02d-%02d", lnow.tm_year + 1900, lnow.tm_mon + 1, lnow.tm_mday, lnow.tm_hour, lnow.tm_min, lnow.tm_sec);
      return string(s);
    }
    
    void doJob() {
      NtKinect kinect;
      cv::VideoWriter vw;
      int scale = 1;
      cv::Size sz(1920/scale,1080/scale);
      bool onSave = false;
      cv::Mat img;
      while (1) {
        kinect.setRGB();
        if (onSave) {
          cv::resize(kinect.rgbImage, img, sz, 0, 0);
          cv::cvtColor(img, img, CV_BGRA2BGR);
          vw << img;
        }
        cv::imshow("rgb", kinect.rgbImage);
        auto key = cv::waitKey(33);
        if (key == 'q') break;
        else if (key == 'r' && !onSave) {
          vw = cv::VideoWriter(now()+".avi",CV_FOURCC_MACRO('X','V','I','D'), 30.0, sz);
          //vw = cv::VideoWriter(now()+".mp4",CV_FOURCC_MACRO('F','M','P','4'), 30.0, sz);
          //vw = cv::VideoWriter(now()+".mp4",CV_FOURCC_MACRO('M','P','4','V'), 30.0, sz);
          if (!vw.isOpened()) throw runtime_error("cannot create video file");
          onSave = true;
        } else if (key == 's' && onSave) {
          vw.release();
          onSave = false;
        }
      }
      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, RGB images are displayed. Exit with 'q' key. Recording starts with 'r' key, and stops with 's' key.
  5. "Date_Time.avi" is generated in the "KinectV2_video/KinectV2/" folder.

  6. Please click here for this sample project KinectV2_vide.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/