39 #include <visp3/core/vpDebug.h>
40 #include <visp3/core/vpIoTools.h>
41 #include <visp3/io/vpVideoWriter.h>
43 #if defined(HAVE_OPENCV_IMGPROC)
44 #include <opencv2/imgproc/imgproc.hpp>
52 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
53 m_writer(), m_framerate(25.0),
55 m_formatType(FORMAT_UNKNOWN), m_videoName(), m_frameName(), m_initFileName(false), m_isOpen(false), m_frameCount(0),
56 m_firstFrame(0), m_width(0), m_height(0), m_frameStep(1)
58 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
59 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
60 m_fourcc = cv::VideoWriter::fourcc(
'P',
'I',
'M',
'1');
62 m_fourcc = CV_FOURCC(
'P',
'I',
'M',
'1');
88 if (filename.empty()) {
92 m_videoName = filename;
93 m_frameName = filename;
95 m_formatType = getFormat(filename);
97 if (m_formatType == FORMAT_UNKNOWN) {
101 m_initFileName =
true;
114 if (!m_initFileName) {
120 if (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG ||
121 m_formatType == FORMAT_PNG) {
124 }
else if (m_formatType == FORMAT_AVI || m_formatType == FORMAT_MPEG || m_formatType == FORMAT_MPEG4 ||
125 m_formatType == FORMAT_MOV) {
126 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
127 m_writer = cv::VideoWriter(m_videoName, m_fourcc, m_framerate,
130 if (!m_writer.isOpened()) {
138 m_frameCount = m_firstFrame;
153 if (!m_initFileName) {
159 if (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG ||
160 m_formatType == FORMAT_PNG) {
163 }
else if (m_formatType == FORMAT_AVI || m_formatType == FORMAT_MPEG || m_formatType == FORMAT_MPEG4 ||
164 m_formatType == FORMAT_MOV) {
165 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
166 m_writer = cv::VideoWriter(m_videoName, m_fourcc, m_framerate,
169 if (!m_writer.isOpened()) {
177 m_frameCount = m_firstFrame;
197 if (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG ||
198 m_formatType == FORMAT_PNG) {
199 char name[FILENAME_MAX];
200 snprintf(name, FILENAME_MAX, m_videoName.c_str(), m_frameCount);
202 m_frameName = std::string(name);
204 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
207 m_writer << matFrame;
211 m_frameCount += m_frameStep;
229 if (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG ||
230 m_formatType == FORMAT_PNG) {
231 char name[FILENAME_MAX];
232 snprintf(name, FILENAME_MAX, m_videoName.c_str(), m_frameCount);
234 m_frameName = std::string(name);
236 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
237 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
238 cv::Mat matFrame, rgbMatFrame;
240 cv::cvtColor(matFrame, rgbMatFrame, cv::COLOR_GRAY2BGR);
241 m_writer << rgbMatFrame;
243 cv::Mat matFrame, rgbMatFrame;
245 cv::cvtColor(matFrame, rgbMatFrame, CV_GRAY2BGR);
246 m_writer << rgbMatFrame;
251 m_frameCount += m_frameStep;
269 vpVideoWriter::vpVideoFormatType vpVideoWriter::getFormat(
const std::string &filename)
271 std::string ext = vpVideoWriter::getExtension(filename);
273 if (ext.compare(
".PGM") == 0)
275 else if (ext.compare(
".pgm") == 0)
277 else if (ext.compare(
".PPM") == 0)
279 else if (ext.compare(
".ppm") == 0)
281 else if (ext.compare(
".JPG") == 0)
283 else if (ext.compare(
".jpg") == 0)
285 else if (ext.compare(
".JPEG") == 0)
287 else if (ext.compare(
".jpeg") == 0)
289 else if (ext.compare(
".PNG") == 0)
291 else if (ext.compare(
".png") == 0)
293 else if (ext.compare(
".AVI") == 0)
295 else if (ext.compare(
".avi") == 0)
297 else if (ext.compare(
".MPEG") == 0)
299 else if (ext.compare(
".mpeg") == 0)
301 else if (ext.compare(
".MPG") == 0)
303 else if (ext.compare(
".mpg") == 0)
305 else if (ext.compare(
".MPEG4") == 0)
307 else if (ext.compare(
".mpeg4") == 0)
309 else if (ext.compare(
".MP4") == 0)
311 else if (ext.compare(
".mp4") == 0)
313 else if (ext.compare(
".MOV") == 0)
315 else if (ext.compare(
".mov") == 0)
318 return FORMAT_UNKNOWN;
322 std::string vpVideoWriter::getExtension(
const std::string &filename)
325 size_t dot = filename.find_last_of(
".");
326 std::string ext = filename.substr(dot, filename.size() - 1);
337 if (first_frame < 0) {
340 m_firstFrame = first_frame;
error that can be emitted by ViSP classes.
@ badValue
Used to indicate that a value is not in the allowed range.
@ notInitialized
Used to indicate that a parameter is not initialized.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Error that can be emitted by the vpImage class and its derivatives.
@ noFileNameError
Image file name error.
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
unsigned int getWidth() const
unsigned int getHeight() const
void saveFrame(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void open(vpImage< vpRGBa > &I)
void setFirstFrameIndex(int first_frame)