MRPT  2.0.4
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 using namespace mrpt;
18 using namespace mrpt::vision;
19 using namespace mrpt::img;
20 using namespace mrpt::system;
21 
22 /** cv::VideoWriter object */
23 struct Impl;
25 
27 {
28 #if MRPT_HAS_OPENCV
29  cv::VideoWriter obj;
30 #endif
31 };
32 
35 {
36 }
37 
39 
41  const std::string& out_file, double fps,
42  const mrpt::img::TImageSize& frameSize, const std::string& fourcc,
43  bool isColor)
44 {
45 #if MRPT_HAS_OPENCV
46  close();
47 
48  int cc;
49 
50  if (fourcc.empty())
51  {
52  cc = CV_FOURCC_DEFAULT; // Default
53  }
54  else if (fourcc.size() == 4)
55  {
56  cc = CV_FOURCC(fourcc[0], fourcc[1], fourcc[2], fourcc[3]);
57  }
58  else
59  {
60  std::cerr << "[CVideoFileWriter::open] fourcc string must be four "
61  "character length or empty for default."
62  << std::endl;
63  return false;
64  }
65 
66  m_img_size = frameSize;
67 
68  return m_video->obj.open(
69  out_file, cc, fps, cv::Size(frameSize.x, frameSize.y), isColor);
70 
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 
80 {
81 #if MRPT_HAS_OPENCV
82  m_video->obj.release();
83 #endif
84 }
85 
86 /* ----------------------------------------------------------
87  isOpen
88  ---------------------------------------------------------- */
90 {
91 #if MRPT_HAS_OPENCV
92  return m_video->obj.isOpened();
93 #else
94  return false;
95 #endif
96 }
97 
99  const mrpt::img::CImage& img)
100 {
101  writeImage(img);
102  return *this;
103 }
104 
106 {
107  if (!m_video.get()) return false;
108 
109  if ((size_t)m_img_size.x != img.getWidth() ||
110  (size_t)m_img_size.y != img.getHeight())
111  {
112  std::cout << format(
113  "[CVideoFileWriter::writeImage] Error: video frame "
114  "size is %ix%i but image is %ux%u",
115  m_img_size.x, m_img_size.y, (unsigned)img.getWidth(),
116  (unsigned)img.getHeight())
117  << std::endl;
118  return false;
119  }
120 
121 #if MRPT_HAS_OPENCV
122  const cv::Mat& m = img.asCvMatRef();
123  m_video->obj.write(m);
124  return true;
125 #else
126  return false;
127 #endif
128 }
bool writeImage(const mrpt::img::CImage &img)
Write image to the video file (method function, alternative to the operator <<).
const CVideoFileWriter & operator<<(const mrpt::img::CImage &img)
Write image to the video file.
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.
spimpl::impl_ptr< T > pimpl
Definition: pimpl.h:15
cv::Mat & asCvMatRef()
Get a reference to the internal cv::Mat, which can be resized, etc.
Definition: CImage.cpp:230
size_t getHeight() const override
Returns the height of the image in pixels.
Definition: CImage.cpp:855
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.
mrpt::pimpl< Impl > m_video
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...
pimpl< T > make_impl(Args &&... args)
Definition: pimpl.h:18
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.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020