Main MRPT website > C++ reference for 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 }
mrpt::vision::CVideoFileWriter::~CVideoFileWriter
virtual ~CVideoFileWriter()
Destructor.
Definition: CVideoFileWriter.cpp:34
mrpt::vision
Classes for computer vision, detectors, features, etc.
Definition: CCamModel.h:20
mrpt::vision::CVideoFileWriter::writeImage
bool writeImage(const mrpt::img::CImage &img) const
Write image to the video file (method function, alternative to the operator <<).
Definition: CVideoFileWriter.cpp:137
mrpt::vision::CVideoFileWriter::isOpen
bool isOpen() const
Return true if already successfully open with open() and not closed yet.
Definition: CVideoFileWriter.cpp:102
M_WRITER
#define M_WRITER
Definition: CVideoFileWriter.cpp:17
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::vision::CVideoFileWriter::CVideoFileWriter
CVideoFileWriter()
Default constructor, which does not open any file.
Definition: CVideoFileWriter.cpp:30
mrpt::vision::CVideoFileWriter::m_video
mrpt::void_ptr_noncopy m_video
A pointer to CvVideoWriter.
Definition: CVideoFileWriter.h:47
mrpt::vision::CVideoFileWriter
An output stream which takes a sequence of images and writes a video file in any of a given of compat...
Definition: CVideoFileWriter.h:43
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
vision-precomp.h
CVideoFileWriter.h
mrpt::vision::CVideoFileWriter::close
void close()
Finish the file writing and close the file output.
Definition: CVideoFileWriter.cpp:90
mrpt::img
Definition: CCanvas.h:17
mrpt::vision::CVideoFileWriter::operator<<
const CVideoFileWriter & operator<<(const mrpt::img::CImage &img) const
Write image to the video file.
Definition: CVideoFileWriter.cpp:114
mrpt::format
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
M_WRITER_PTR
#define M_WRITER_PTR
Definition: CVideoFileWriter.cpp:20
mrpt::vision::CVideoFileWriter::m_img_size
mrpt::img::TImageSize m_img_size
A copy of the video size.
Definition: CVideoFileWriter.h:49
mrpt::img::CImage
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
img
GLint GLvoid * img
Definition: glext.h:3763
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::img::TImageSize
TPixelCoord TImageSize
A type for image sizes.
Definition: TPixelCoord.h:53
mrpt::vision::CVideoFileWriter::open
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.
Definition: CVideoFileWriter.cpp:38
mrpt::non_copiable_ptr_basic::get
T *& get()
Definition: safe_pointers.h:146
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST