MRPT  1.9.9
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-2018, 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 \
18  (const_cast<CvVideoWriter*>( \
19  static_cast<const CvVideoWriter*>(m_video.get())))
20 #define M_WRITER_PTR (reinterpret_cast<CvVideoWriter**>(m_video.getPtrToPtr()))
21 
22 using namespace mrpt;
23 using namespace mrpt::vision;
24 using namespace mrpt::img;
25 using namespace mrpt::system;
26 
27 /* ----------------------------------------------------------
28  Ctor
29  ---------------------------------------------------------- */
30 CVideoFileWriter::CVideoFileWriter() : m_video(), m_img_size(0, 0) {}
31 /* ----------------------------------------------------------
32  Dtor
33  ---------------------------------------------------------- */
35 /* ----------------------------------------------------------
36  open
37  ---------------------------------------------------------- */
39  const std::string& out_file, double fps,
40  const mrpt::img::TImageSize& frameSize, const std::string& fourcc,
41  bool isColor)
42 {
43 #if MRPT_HAS_OPENCV
44  close();
45 
46  int cc;
47 
48  if (fourcc.empty())
49  {
50 #if MRPT_OPENCV_VERSION_NUM <= 0x100
51  cc = 0; // Default
52 #else
53 #ifdef _WIN32
54  cc = CV_FOURCC_DEFAULT; // Default CV_FOURCC_PROMPT;
55 #else
56  cc = CV_FOURCC_DEFAULT; // Default
57 #endif
58 #endif
59  }
60  else if (fourcc.size() == 4)
61  {
62  cc = CV_FOURCC(fourcc[0], fourcc[1], fourcc[2], fourcc[3]);
63  }
64  else
65  {
66  std::cerr << "[CVideoFileWriter::open] fourcc string must be four "
67  "character length or empty for default."
68  << std::endl;
69  return false;
70  }
71 
72  m_video = cvCreateVideoWriter(
73  out_file.c_str(), cc, fps, cvSize(frameSize.x, frameSize.y),
74  isColor ? 1 : 0);
75 
76  m_img_size = frameSize;
77 
78  return m_video.get() != nullptr;
79 #else
80  std::cerr << "[CVideoFileWriter::open] ERROR: MRPT was compiled without "
81  "OpenCV support "
82  << std::endl;
83  return false;
84 #endif
85 }
86 
87 /* ----------------------------------------------------------
88  close
89  ---------------------------------------------------------- */
91 {
92 #if MRPT_HAS_OPENCV
93  if (!M_WRITER) return;
94  cvReleaseVideoWriter(M_WRITER_PTR);
95  *M_WRITER_PTR = nullptr;
96 #endif
97 }
98 
99 /* ----------------------------------------------------------
100  isOpen
101  ---------------------------------------------------------- */
103 {
104 #if MRPT_HAS_OPENCV
105  return (M_WRITER != nullptr);
106 #else
107  return false;
108 #endif
109 }
110 
111 /* ----------------------------------------------------------
112  operator <<
113  ---------------------------------------------------------- */
115  const mrpt::img::CImage& img) const
116 {
117  if (!m_video.get()) THROW_EXCEPTION("Call open first");
118 
119  if ((size_t)m_img_size.x != img.getWidth() ||
120  (size_t)m_img_size.y != img.getHeight())
122  format(
123  "Video frame size is %ix%i but image is %ux%u", m_img_size.x,
124  m_img_size.y, (unsigned)img.getWidth(),
125  (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()) return false;
140 
141  if ((size_t)m_img_size.x != img.getWidth() ||
142  (size_t)m_img_size.y != img.getHeight())
143  {
144  std::cout << format(
145  "[CVideoFileWriter::writeImage] Error: video frame "
146  "size is %ix%i but image is %ux%u",
147  m_img_size.x, m_img_size.y, (unsigned)img.getWidth(),
148  (unsigned)img.getHeight())
149  << std::endl;
150  return false;
151  }
152 
153 #if MRPT_HAS_OPENCV
154  return 0 != cvWriteFrame(M_WRITER, img.getAs<IplImage>());
155 #else
156  return false;
157 #endif
158 }
const CVideoFileWriter & operator<<(const mrpt::img::CImage &img) const
Write image to the video file.
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
void close()
Finish the file writing and close the file output.
mrpt::void_ptr_noncopy m_video
A pointer to CvVideoWriter.
bool open(const std::string &out_file, double fps, const mrpt::img::TImageSize &frameSize, const std::string &fourcc=std::string(""), bool isColor=true)
Open a file for writing the video.
mrpt::img::TImageSize m_img_size
A copy of the video size.
bool isOpen() const
Return true if already successfully open with open() and not closed yet.
GLint GLvoid * img
Definition: glext.h:3763
Classes for computer vision, detectors, features, etc.
Definition: CCamModel.h:18
GLsizei const GLchar ** string
Definition: glext.h:4101
CVideoFileWriter()
Default constructor, which does not open any file.
virtual ~CVideoFileWriter()
Destructor.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
An output stream which takes a sequence of images and writes a video file in any of a given of compat...
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
TPixelCoord TImageSize
A type for image sizes.
Definition: TPixelCoord.h:51
bool writeImage(const mrpt::img::CImage &img) const
Write image to the video file (method function, alternative to the operator <<).
#define M_WRITER_PTR
#define M_WRITER
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020