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) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
54 m_writer(), m_framerate(25.0),
56 m_formatType(FORMAT_UNKNOWN), m_videoName(), m_frameName(), m_initFileName(false), m_isOpen(false), m_frameCount(0),
57 m_firstFrame(0), m_width(0), m_height(0), m_frameStep(1)
59 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
60 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
61 m_fourcc = cv::VideoWriter::fourcc(
'P',
'I',
'M',
'1');
63 m_fourcc = CV_FOURCC(
'P',
'I',
'M',
'1');
89 if (filename.empty()) {
93 m_videoName = filename;
94 m_frameName = filename;
96 m_formatType = getFormat(filename);
98 if (m_formatType == FORMAT_UNKNOWN) {
102 m_initFileName =
true;
115 if (!m_initFileName) {
121 if (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG ||
122 m_formatType == FORMAT_PNG) {
126 else if (m_formatType == FORMAT_AVI || m_formatType == FORMAT_MPEG || m_formatType == FORMAT_MPEG4 ||
127 m_formatType == FORMAT_MOV) {
128 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
129 m_writer = cv::VideoWriter(m_videoName, m_fourcc, m_framerate,
132 if (!m_writer.isOpened()) {
140 m_frameCount = m_firstFrame;
155 if (!m_initFileName) {
161 if (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG ||
162 m_formatType == FORMAT_PNG) {
166 else if (m_formatType == FORMAT_AVI || m_formatType == FORMAT_MPEG || m_formatType == FORMAT_MPEG4 ||
167 m_formatType == FORMAT_MOV) {
168 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
169 m_writer = cv::VideoWriter(m_videoName, m_fourcc, m_framerate,
172 if (!m_writer.isOpened()) {
180 m_frameCount = m_firstFrame;
200 if (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG ||
201 m_formatType == FORMAT_PNG) {
202 char name[FILENAME_MAX];
203 snprintf(name, FILENAME_MAX, m_videoName.c_str(), m_frameCount);
205 m_frameName = std::string(name);
208 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
211 m_writer << matFrame;
215 m_frameCount += m_frameStep;
233 if (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG ||
234 m_formatType == FORMAT_PNG) {
235 char name[FILENAME_MAX];
236 snprintf(name, FILENAME_MAX, m_videoName.c_str(), m_frameCount);
238 m_frameName = std::string(name);
241 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
242 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
243 cv::Mat matFrame, rgbMatFrame;
245 cv::cvtColor(matFrame, rgbMatFrame, cv::COLOR_GRAY2BGR);
246 m_writer << rgbMatFrame;
248 cv::Mat matFrame, rgbMatFrame;
250 cv::cvtColor(matFrame, rgbMatFrame, CV_GRAY2BGR);
251 m_writer << rgbMatFrame;
256 m_frameCount += m_frameStep;
274 vpVideoWriter::vpVideoFormatType vpVideoWriter::getFormat(
const std::string &filename)
276 std::string ext = vpVideoWriter::getExtension(filename);
278 if (ext.compare(
".PGM") == 0)
280 else if (ext.compare(
".pgm") == 0)
282 else if (ext.compare(
".PPM") == 0)
284 else if (ext.compare(
".ppm") == 0)
286 else if (ext.compare(
".JPG") == 0)
288 else if (ext.compare(
".jpg") == 0)
290 else if (ext.compare(
".JPEG") == 0)
292 else if (ext.compare(
".jpeg") == 0)
294 else if (ext.compare(
".PNG") == 0)
296 else if (ext.compare(
".png") == 0)
298 else if (ext.compare(
".AVI") == 0)
300 else if (ext.compare(
".avi") == 0)
302 else if (ext.compare(
".MPEG") == 0)
304 else if (ext.compare(
".mpeg") == 0)
306 else if (ext.compare(
".MPG") == 0)
308 else if (ext.compare(
".mpg") == 0)
310 else if (ext.compare(
".MPEG4") == 0)
312 else if (ext.compare(
".mpeg4") == 0)
314 else if (ext.compare(
".MP4") == 0)
316 else if (ext.compare(
".mp4") == 0)
318 else if (ext.compare(
".MOV") == 0)
320 else if (ext.compare(
".mov") == 0)
323 return FORMAT_UNKNOWN;
327 std::string vpVideoWriter::getExtension(
const std::string &filename)
330 size_t dot = filename.find_last_of(
".");
331 std::string ext = filename.substr(dot, filename.size() - 1);
342 if (first_frame < 0) {
345 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)