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
|
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(); |
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 |
|
"Date_Time.avi" is generated in the "KinectV2_video/KinectV2/" folder.
Since the above zip file may not include the latest "NtKinect.h", Download the latest version from here and replace old one with it.