39#include <visp3/core/vpIoTools.h>
40#include <visp3/io/vpVideoWriter.h>
42#if defined(HAVE_OPENCV_IMGPROC)
43#include <opencv2/imgproc/imgproc.hpp>
53#if defined(VISP_HAVE_OPENCV) && \
54 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
55 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
56 m_writer(), m_framerate(25.0),
58 m_formatType(FORMAT_UNKNOWN), m_videoName(), m_frameName(), m_initFileName(false), m_isOpen(false), m_frameCount(0),
59 m_firstFrame(0), m_width(0), m_height(0), m_frameStep(1)
61#if defined(VISP_HAVE_OPENCV) && \
62 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
63 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
64#if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
65 m_fourcc = cv::VideoWriter::fourcc(
'P',
'I',
'M',
'1');
67 m_fourcc = CV_FOURCC(
'P',
'I',
'M',
'1');
93 if (filename.empty()) {
97 m_videoName = filename;
98 m_frameName = filename;
100 m_formatType = getFormat(filename);
102 if (m_formatType == FORMAT_UNKNOWN) {
106 m_initFileName =
true;
119 if (!m_initFileName) {
125 if (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG ||
126 m_formatType == FORMAT_PNG) {
127 m_width = I.getWidth();
128 m_height = I.getHeight();
130 else if (m_formatType == FORMAT_AVI || m_formatType == FORMAT_MPEG || m_formatType == FORMAT_MPEG4 ||
131 m_formatType == FORMAT_MOV) {
132#if defined(VISP_HAVE_OPENCV) && \
133 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
134 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
135 m_writer = cv::VideoWriter(m_videoName, m_fourcc, m_framerate,
136 cv::Size(
static_cast<int>(I.getWidth()),
static_cast<int>(I.getHeight())));
138 if (!m_writer.isOpened()) {
146 m_frameCount = m_firstFrame;
161 if (!m_initFileName) {
167 if (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG ||
168 m_formatType == FORMAT_PNG) {
169 m_width = I.getWidth();
170 m_height = I.getHeight();
172 else if (m_formatType == FORMAT_AVI || m_formatType == FORMAT_MPEG || m_formatType == FORMAT_MPEG4 ||
173 m_formatType == FORMAT_MOV) {
174#if defined(VISP_HAVE_OPENCV) && \
175 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
176 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
177 m_writer = cv::VideoWriter(m_videoName, m_fourcc, m_framerate,
178 cv::Size(
static_cast<int>(I.getWidth()),
static_cast<int>(I.getHeight())));
180 if (!m_writer.isOpened()) {
188 m_frameCount = m_firstFrame;
208 if (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG ||
209 m_formatType == FORMAT_PNG) {
214#if defined(VISP_HAVE_OPENCV) && \
215 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
216 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
219 m_writer << matFrame;
223 m_frameCount += m_frameStep;
241 if (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG ||
242 m_formatType == FORMAT_PNG) {
247#if defined(VISP_HAVE_OPENCV) && \
248 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
249 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
250#if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
251 cv::Mat matFrame, rgbMatFrame;
253 cv::cvtColor(matFrame, rgbMatFrame, cv::COLOR_GRAY2BGR);
254 m_writer << rgbMatFrame;
256 cv::Mat matFrame, rgbMatFrame;
258 cv::cvtColor(matFrame, rgbMatFrame, CV_GRAY2BGR);
259 m_writer << rgbMatFrame;
264 m_frameCount += m_frameStep;
282vpVideoWriter::vpVideoFormatType vpVideoWriter::getFormat(
const std::string &filename)
284 std::string ext = vpVideoWriter::getExtension(filename);
286 if (ext.compare(
".PGM") == 0)
288 else if (ext.compare(
".pgm") == 0)
290 else if (ext.compare(
".PPM") == 0)
292 else if (ext.compare(
".ppm") == 0)
294 else if (ext.compare(
".JPG") == 0)
296 else if (ext.compare(
".jpg") == 0)
298 else if (ext.compare(
".JPEG") == 0)
300 else if (ext.compare(
".jpeg") == 0)
302 else if (ext.compare(
".PNG") == 0)
304 else if (ext.compare(
".png") == 0)
306 else if (ext.compare(
".AVI") == 0)
308 else if (ext.compare(
".avi") == 0)
310 else if (ext.compare(
".MPEG") == 0)
312 else if (ext.compare(
".mpeg") == 0)
314 else if (ext.compare(
".MPG") == 0)
316 else if (ext.compare(
".mpg") == 0)
318 else if (ext.compare(
".MPEG4") == 0)
320 else if (ext.compare(
".mpeg4") == 0)
322 else if (ext.compare(
".MP4") == 0)
324 else if (ext.compare(
".mp4") == 0)
326 else if (ext.compare(
".MOV") == 0)
328 else if (ext.compare(
".mov") == 0)
331 return FORMAT_UNKNOWN;
335std::string vpVideoWriter::getExtension(
const std::string &filename)
338 size_t dot =
filename.find_last_of(
".");
350 if (first_frame < 0) {
353 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)
Definition of the vpImage class member functions.
void saveFrame(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void open(vpImage< vpRGBa > &I)
void setFirstFrameIndex(int first_frame)