video_writer.cpp
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2017, Southwest Research Institute® (SwRI®)
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //     * Redistributions of source code must retain the above copyright
00009 //       notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //     * Neither the name of Southwest Research Institute® (SwRI®) nor the
00014 //       names of its contributors may be used to endorse or promote products
00015 //       derived from this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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           // The image received should have its format set to ARGB32, but it's
00089           // actually BGRA.  Need to convert it to BGR and flip it vertically
00090           // before giving it to the cv::VideoWriter.
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 }


mapviz
Author(s): Marc Alban
autogenerated on Thu Aug 24 2017 02:45:59