45 #include <visp3/core/vpDebug.h> 46 #include <visp3/io/vpVideoWriter.h> 48 #if VISP_HAVE_OPENCV_VERSION >= 0x020200 49 #include <opencv2/imgproc/imgproc.hpp> 57 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
58 m_writer(), m_framerate(25.0),
60 m_formatType(FORMAT_UNKNOWN), m_videoName(), m_frameName(),
61 m_initFileName(false), m_isOpen(false), m_frameCount(0), m_firstFrame(0),
62 m_width(0), m_height(0), m_frameStep(1)
64 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 65 m_fourcc = cv::VideoWriter::fourcc(
'P',
'I',
'M',
'1');
66 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 67 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;
112 if (! m_initFileName) {
116 if (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG || m_formatType == FORMAT_PNG) {
119 }
else if (m_formatType == FORMAT_AVI || m_formatType == FORMAT_MPEG || m_formatType == FORMAT_MPEG4 ||
120 m_formatType == FORMAT_MOV) {
121 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 122 m_writer = cv::VideoWriter(m_videoName, m_fourcc, m_framerate,
125 if (! m_writer.isOpened()) {
133 m_frameCount = m_firstFrame;
145 if (! m_initFileName) {
149 if (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG || m_formatType == FORMAT_PNG) {
152 }
else if (m_formatType == FORMAT_AVI || m_formatType == FORMAT_MPEG || m_formatType == FORMAT_MPEG4 ||
153 m_formatType == FORMAT_MOV) {
154 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 155 m_writer = cv::VideoWriter(m_videoName, m_fourcc, m_framerate,
158 if (! m_writer.isOpened()) {
166 m_frameCount = m_firstFrame;
186 if (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG || m_formatType == FORMAT_PNG) {
187 char name[FILENAME_MAX];
188 sprintf(name, m_videoName.c_str(), m_frameCount);
190 m_frameName = std::string(name);
192 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 195 m_writer << matFrame;
199 m_frameCount += m_frameStep;
217 if (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG || m_formatType == FORMAT_PNG) {
218 char name[FILENAME_MAX];
219 sprintf(name, m_videoName.c_str(), m_frameCount);
221 m_frameName = std::string(name);
223 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 224 cv::Mat matFrame, rgbMatFrame;
226 cv::cvtColor(matFrame, rgbMatFrame, cv::COLOR_GRAY2BGR);
227 m_writer << rgbMatFrame;
228 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 229 cv::Mat matFrame, rgbMatFrame;
231 cv::cvtColor(matFrame, rgbMatFrame, CV_GRAY2BGR);
232 m_writer << rgbMatFrame;
236 m_frameCount += m_frameStep;
254 vpVideoWriter::vpVideoFormatType vpVideoWriter::getFormat(
const std::string &filename)
256 std::string ext = vpVideoWriter::getExtension(filename);
258 if (ext.compare(
".PGM") == 0)
260 else if (ext.compare(
".pgm") == 0)
262 else if (ext.compare(
".PPM") == 0)
264 else if (ext.compare(
".ppm") == 0)
266 else if (ext.compare(
".JPG") == 0)
268 else if (ext.compare(
".jpg") == 0)
270 else if (ext.compare(
".JPEG") == 0)
272 else if (ext.compare(
".jpeg") == 0)
274 else if (ext.compare(
".PNG") == 0)
276 else if (ext.compare(
".png") == 0)
278 else if (ext.compare(
".AVI") == 0)
280 else if (ext.compare(
".avi") == 0)
282 else if (ext.compare(
".MPEG") == 0)
284 else if (ext.compare(
".mpeg") == 0)
286 else if (ext.compare(
".MPG") == 0)
288 else if (ext.compare(
".mpg") == 0)
290 else if (ext.compare(
".MPEG4") == 0)
292 else if (ext.compare(
".mpeg4") == 0)
294 else if (ext.compare(
".MP4") == 0)
296 else if (ext.compare(
".mp4") == 0)
298 else if (ext.compare(
".MOV") == 0)
300 else if (ext.compare(
".mov") == 0)
303 return FORMAT_UNKNOWN;
307 std::string vpVideoWriter::getExtension(
const std::string &filename)
310 size_t dot = filename.find_last_of(
".");
311 std::string ext = filename.substr(dot, filename.size() - 1);
321 if (first_frame < 0) {
324 m_firstFrame = first_frame;
Used to indicate that a value is not in the allowed range.
void setFirstFrameIndex(int first_frame)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
error that can be emited by ViSP classes.
Error that can be emited by the vpImage class and its derivates.
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
void saveFrame(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
unsigned int getHeight() const
Used to indicate that a parameter is not initialized.
void setFileName(const std::string &filename)
unsigned int getWidth() const