Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <mapviz/video_writer.h>
00031
00032 #include <ros/ros.h>
00033
00034 #include <opencv2/imgproc/imgproc.hpp>
00035
00036 namespace mapviz
00037 {
00038 bool VideoWriter::initializeWriter(const std::string& directory, int width, int height)
00039 {
00040 QMutexLocker locker(&video_mutex_);
00041 if (!video_writer_)
00042 {
00043 width_ = width;
00044 height_ = height;
00045
00046 ROS_INFO("Initializing recording:\nWidth/Height/Filename: %d / %d / %s", width_, height_, directory.c_str() );
00047 video_writer_ = boost::make_shared<cv::VideoWriter>(
00048 directory,
00049 CV_FOURCC('M', 'J', 'P', 'G'),
00050 30,
00051 cv::Size(width_, height_));
00052
00053 if (!video_writer_->isOpened())
00054 {
00055 ROS_ERROR("Failed to open video file for writing.");
00056 stop();
00057 return false;
00058 }
00059 }
00060
00061 return true;
00062 }
00063
00064 bool VideoWriter::isRecording()
00065 {
00066 return video_writer_.get() != NULL;
00067 }
00068
00069 void VideoWriter::processFrame(QImage frame)
00070 {
00071 try
00072 {
00073 ROS_DEBUG_THROTTLE(1.0, "VideoWriter::processFrame()");
00074 {
00075 QMutexLocker locker(&video_mutex_);
00076 if (!video_writer_)
00077 {
00078 ROS_WARN_THROTTLE(1.0, "Got frame, but video writer wasn't open.");
00079 return;
00080 }
00081 }
00082
00083 cv::Mat image;
00084 cv::Mat temp_image;
00085 switch (frame.format())
00086 {
00087 case QImage::Format_ARGB32:
00088
00089
00090
00091 image = cv::Mat(frame.height(), frame.width(), CV_8UC4, frame.bits());
00092 cv::cvtColor(image, temp_image, CV_BGRA2BGR);
00093 cv::flip(temp_image, image, 0);
00094 break;
00095 default:
00096 ROS_WARN_THROTTLE(1.0, "Unexpected image format: %d", frame.format());
00097 return;
00098 }
00099
00100 {
00101 QMutexLocker locker(&video_mutex_);
00102 if (video_writer_)
00103 {
00104 ROS_DEBUG_THROTTLE(1.0, "Writing frame.");
00105 video_writer_->write(image);
00106 }
00107 }
00108 }
00109 catch (const std::exception& e)
00110 {
00111 ROS_ERROR_THROTTLE(1.0, "Error when processing video frame: %s", e.what());
00112 }
00113 }
00114
00115 void VideoWriter::stop()
00116 {
00117 ROS_INFO("Stopping video recording.");
00118 QMutexLocker locker(&video_mutex_);
00119 video_writer_.reset();
00120 }
00121 }