Main MRPT website > C++ reference for MRPT 1.5.6
CVideoFileWriter.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #include "vision-precomp.h" // Precompiled headers
11 
13 
14 // Universal include for all versions of OpenCV
15 #include <mrpt/otherlibs/do_opencv_includes.h>
16 
17 #define M_WRITER (const_cast<CvVideoWriter*>( static_cast<const CvVideoWriter*>(m_video.get())) )
18 #define M_WRITER_PTR (reinterpret_cast<CvVideoWriter**>(m_video.getPtrToPtr()))
19 
20 
21 using namespace mrpt;
22 using namespace mrpt::vision;
23 using namespace mrpt::system;
24 using namespace mrpt::utils;
25 
26 /* ----------------------------------------------------------
27  Ctor
28  ---------------------------------------------------------- */
29 CVideoFileWriter::CVideoFileWriter() : m_video(), m_img_size(0,0)
30 {
31 }
32 
33 /* ----------------------------------------------------------
34  Dtor
35  ---------------------------------------------------------- */
37 {
38  close();
39 }
40 
41 /* ----------------------------------------------------------
42  open
43  ---------------------------------------------------------- */
45  const std::string &out_file,
46  double fps,
47  const mrpt::utils::TImageSize & frameSize,
48  const std::string &fourcc,
49  bool isColor )
50 {
51 #if MRPT_HAS_OPENCV
52  close();
53 
54  int cc;
55 
56  if (fourcc.empty())
57  {
58 #if MRPT_OPENCV_VERSION_NUM<=0x100
59  cc = 0; // Default
60 #else
61  #ifdef MRPT_OS_WINDOWS
62  cc = CV_FOURCC_DEFAULT; // Default CV_FOURCC_PROMPT;
63  #else
64  cc = CV_FOURCC_DEFAULT; // Default
65  #endif
66 #endif
67  }
68  else
69  if (fourcc.size()==4)
70  {
71  cc = CV_FOURCC( fourcc[0],fourcc[1],fourcc[2],fourcc[3] );
72  }
73  else
74  {
75  std::cerr << "[CVideoFileWriter::open] fourcc string must be four character length or empty for default." << std::endl;
76  return false;
77  }
78 
79  m_video = cvCreateVideoWriter(out_file.c_str(),cc,fps,cvSize(frameSize.x,frameSize.y),isColor ? 1:0);
80 
81  m_img_size = frameSize;
82 
83  return m_video.get() != NULL;
84 #else
85  std::cerr << "[CVideoFileWriter::open] ERROR: MRPT was compiled without OpenCV support " << std::endl;
86  return false;
87 #endif
88 }
89 
90 
91 /* ----------------------------------------------------------
92  close
93  ---------------------------------------------------------- */
95 {
96 #if MRPT_HAS_OPENCV
97  if (!M_WRITER) return;
98  cvReleaseVideoWriter( M_WRITER_PTR );
99  *M_WRITER_PTR = NULL;
100 #endif
101 }
102 
103 /* ----------------------------------------------------------
104  isOpen
105  ---------------------------------------------------------- */
107 {
108 #if MRPT_HAS_OPENCV
109  return (M_WRITER!=NULL);
110 #else
111  return false;
112 #endif
113 }
114 
115 
116 /* ----------------------------------------------------------
117  operator <<
118  ---------------------------------------------------------- */
120 {
121  if (!m_video.get())
122  THROW_EXCEPTION("Call open first")
123 
124  if ((size_t)m_img_size.x!=img.getWidth() || (size_t)m_img_size.y!=img.getHeight())
125  THROW_EXCEPTION(format("Video frame size is %ix%i but image is %ux%u", m_img_size.x,m_img_size.y,(unsigned)img.getWidth(),(unsigned)img.getHeight() ));
126 
127 #if MRPT_HAS_OPENCV
128  if (! cvWriteFrame( M_WRITER, img.getAs<IplImage>() ) )
129  THROW_EXCEPTION("Error writing image frame to video file")
130 #endif
131  return *this;
132 }
133 
134 /* ----------------------------------------------------------
135  writeImage
136  ---------------------------------------------------------- */
138 {
139  if (!m_video.get())
140  return false;
141 
142  if ((size_t)m_img_size.x!=img.getWidth() || (size_t)m_img_size.y!=img.getHeight())
143  {
144  std::cout << format("[CVideoFileWriter::writeImage] Error: video frame size is %ix%i but image is %ux%u", m_img_size.x,m_img_size.y,(unsigned)img.getWidth(),(unsigned)img.getHeight() ) << std::endl;
145  return false;
146  }
147 
148 #if MRPT_HAS_OPENCV
149  return 0!= cvWriteFrame( M_WRITER, img.getAs<IplImage>() );
150 #else
151  return false;
152 #endif
153 }
154 
mrpt::utils::TImageSize m_img_size
A copy of the video size.
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:101
void close()
Finish the file writing and close the file output.
#define THROW_EXCEPTION(msg)
A pair (x,y) of pixel coordinates (integer resolution).
Definition: TPixelCoord.h:37
mrpt::utils::void_ptr_noncopy m_video
A pointer to CvVideoWriter.
GLint GLvoid * img
Definition: glew.h:1290
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:21
CVideoFileWriter()
Default constructor, which does not open any file.
const CVideoFileWriter & operator<<(const mrpt::utils::CImage &img) const
Write image to the video file.
virtual ~CVideoFileWriter()
Destructor.
GLsizei const GLcharARB ** string
Definition: glew.h:3293
An output stream which takes a sequence of images and writes a video file in any of a given of compat...
bool writeImage(const mrpt::utils::CImage &img) const
Write image to the video file (method function, alternative to the operator <<).
const T * getAs() const
Returns a pointer to a const T* containing the image - the idea is to call like "img.getAs<IplImage>()" so we can avoid here including OpenCV's headers.
Definition: CImage.h:517
bool isOpen() const
Return true if already successfully open with open() and not closed yet.
#define M_WRITER_PTR
bool open(const std::string &out_file, double fps, const mrpt::utils::TImageSize &frameSize, const std::string &fourcc=std::string(""), bool isColor=true)
Open a file for writing the video.
size_t getWidth() const MRPT_OVERRIDE
Returns the width of the image in pixels.
Definition: CImage.cpp:855
#define M_WRITER
size_t getHeight() const MRPT_OVERRIDE
Returns the height of the image in pixels.
Definition: CImage.cpp:884



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018