MRPT  2.0.1
CVideoFileWriter.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://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/3rdparty/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  cc = CV_FOURCC_DEFAULT; // Default
51  }
52  else if (fourcc.size() == 4)
53  {
54  cc = CV_FOURCC(fourcc[0], fourcc[1], fourcc[2], fourcc[3]);
55  }
56  else
57  {
58  std::cerr << "[CVideoFileWriter::open] fourcc string must be four "
59  "character length or empty for default."
60  << std::endl;
61  return false;
62  }
63 
64  m_video = cvCreateVideoWriter(
65  out_file.c_str(), cc, fps, cvSize(frameSize.x, frameSize.y),
66  isColor ? 1 : 0);
67 
68  m_img_size = frameSize;
69 
70  return m_video.get() != nullptr;
71 #else
72  std::cerr << "[CVideoFileWriter::open] ERROR: MRPT was compiled without "
73  "OpenCV support "
74  << std::endl;
75  return false;
76 #endif
77 }
78 
79 /* ----------------------------------------------------------
80  close
81  ---------------------------------------------------------- */
83 {
84 #if MRPT_HAS_OPENCV
85  if (!M_WRITER) return;
86  cvReleaseVideoWriter(M_WRITER_PTR);
87  *M_WRITER_PTR = nullptr;
88 #endif
89 }
90 
91 /* ----------------------------------------------------------
92  isOpen
93  ---------------------------------------------------------- */
95 {
96 #if MRPT_HAS_OPENCV
97  return (M_WRITER != nullptr);
98 #else
99  return false;
100 #endif
101 }
102 
103 /* ----------------------------------------------------------
104  operator <<
105  ---------------------------------------------------------- */
107  const mrpt::img::CImage& img) const
108 {
109  if (!m_video.get()) THROW_EXCEPTION("Call open first");
110 
111  if ((size_t)m_img_size.x != img.getWidth() ||
112  (size_t)m_img_size.y != img.getHeight())
114  "Video frame size is %ix%i but image is %ux%u", m_img_size.x,
115  m_img_size.y, (unsigned)img.getWidth(), (unsigned)img.getHeight()));
116 
117 #if MRPT_HAS_OPENCV
118  cv::Mat m = img.asCvMat<cv::Mat>(SHALLOW_COPY);
119  IplImage ipl = cvIplImage(m);
120  if (!cvWriteFrame(M_WRITER, &ipl))
121  THROW_EXCEPTION("Error writing image frame to video file");
122 #endif
123  return *this;
124 }
125 
126 /* ----------------------------------------------------------
127  writeImage
128  ---------------------------------------------------------- */
130 {
131  if (!m_video.get()) return false;
132 
133  if ((size_t)m_img_size.x != img.getWidth() ||
134  (size_t)m_img_size.y != img.getHeight())
135  {
136  std::cout << format(
137  "[CVideoFileWriter::writeImage] Error: video frame "
138  "size is %ix%i but image is %ux%u",
139  m_img_size.x, m_img_size.y, (unsigned)img.getWidth(),
140  (unsigned)img.getHeight())
141  << std::endl;
142  return false;
143  }
144 
145 #if MRPT_HAS_OPENCV
146  cv::Mat m = img.asCvMat<cv::Mat>(SHALLOW_COPY);
147  IplImage ipl = cvIplImage(m);
148  return 0 != cvWriteFrame(M_WRITER, &ipl);
149 #else
150  return false;
151 #endif
152 }
Shallow copy: the copied object is a reference to the original one.
Definition: img/CImage.h:75
const CVideoFileWriter & operator<<(const mrpt::img::CImage &img) const
Write image to the video file.
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
void close()
Finish the file writing and close the file output.
mrpt::void_ptr_noncopy m_video
A pointer to CvVideoWriter.
size_t getHeight() const override
Returns the height of the image in pixels.
Definition: CImage.cpp:849
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.
void asCvMat(cv::Mat &out_img, copy_type_t copy_type) const
Makes a shallow or deep copy of this image into the provided cv::Mat.
Definition: CImage.cpp:217
bool isOpen() const
Return true if already successfully open with open() and not closed yet.
size_t getWidth() const override
Returns the width of the image in pixels.
Definition: CImage.cpp:818
A pair (x,y) of pixel coordinates (integer resolution).
Definition: TPixelCoord.h:40
Classes for computer vision, detectors, features, etc.
Definition: CDifodo.h:17
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...
struct _IplImage IplImage
Definition: img/CImage.h:22
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:148



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020