Visual Servoing Platform  version 3.5.1 under development (2023-03-14)
vpVideoReader.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Read videos and image sequences.
33  *
34  * Authors:
35  * Nicolas Melchior
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
45 #include <visp3/core/vpDebug.h>
46 #include <visp3/core/vpIoTools.h>
47 #include <visp3/io/vpVideoReader.h>
48 
49 #include <cctype>
50 #include <fstream>
51 #include <iostream>
52 #include <limits> // numeric_limits
53 
58  : vpFrameGrabber(), m_imSequence(NULL),
59 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
60  m_capture(), m_frame(), m_lastframe_unknown(false),
61 #endif
62  m_formatType(FORMAT_UNKNOWN), m_videoName(), m_frameName(), m_initFileName(false), m_isOpen(false), m_frameCount(0),
63  m_firstFrame(0), m_lastFrame(0), m_firstFrameIndexIsSet(false), m_lastFrameIndexIsSet(false), m_frameStep(1),
64  m_frameRate(0.)
65 {
66 }
67 
72 {
73  if (m_imSequence != NULL) {
74  delete m_imSequence;
75  }
76 }
77 
93 void vpVideoReader::setFileName(const std::string &filename)
94 {
95  if (filename.empty()) {
96  throw(vpImageException(vpImageException::noFileNameError, "filename empty "));
97  }
98 
99  m_videoName = filename;
100  m_frameName = filename;
101 
102  m_formatType = getFormat(filename);
103 
104  if (m_formatType == FORMAT_UNKNOWN) {
105  throw(vpException(vpException::badValue, "Filename extension not supported"));
106  }
107 
108  // checking image name format
109  if (isImageExtensionSupported()) {
110  std::string format = vpIoTools::getName(m_videoName);
111  if (!checkImageNameFormat(format)) {
112  throw(vpException(vpException::badValue, "Format of image name wasn't recognized: %s", format.c_str()));
113  }
114  }
115 
116  m_initFileName = true;
117 }
118 
122 void vpVideoReader::getProperties()
123 {
124  if (m_isOpen) {
125  return;
126  }
127 
128  if (!m_initFileName) {
129  throw(vpImageException(vpImageException::noFileNameError, "The generic filename has to be set"));
130  }
131 
132  if (isImageExtensionSupported()) {
133  m_imSequence = new vpDiskGrabber;
134  m_imSequence->setGenericName(m_videoName.c_str());
135  m_imSequence->setStep(m_frameStep);
136  if (m_firstFrameIndexIsSet) {
137  m_imSequence->setImageNumber(m_firstFrame);
138  }
139  m_frameRate = -1.;
140  } else if (isVideoExtensionSupported()) {
141 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
142  m_capture.open(m_videoName.c_str());
143 
144  if (!m_capture.isOpened()) {
145  throw(vpException(vpException::ioError, "Could not open the video %s with OpenCV", m_videoName.c_str()));
146  }
147 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
148  width = (unsigned int)m_capture.get(cv::CAP_PROP_FRAME_WIDTH);
149  height = (unsigned int)m_capture.get(cv::CAP_PROP_FRAME_HEIGHT);
150  m_frameRate = (double)m_capture.get(cv::CAP_PROP_FPS);
151 #else
152  width = (unsigned int)m_capture.get(CV_CAP_PROP_FRAME_WIDTH);
153  height = (unsigned int)m_capture.get(CV_CAP_PROP_FRAME_HEIGHT);
154  m_frameRate = m_capture.get(CV_CAP_PROP_FPS);
155 #endif
156 
157 #else
158  throw(vpException(vpException::fatalError, "To read video files ViSP should be build with opencv "
159  "3rd >= 2.1.0 party libraries."));
160 #endif
161  } else if (m_formatType == FORMAT_UNKNOWN) {
162  // vpERROR_TRACE("The format of the file does not correspond to a readable
163  // format.");
164  throw(vpException(vpException::fatalError, "The format of the file does "
165  "not correspond to a readable "
166  "format supported by ViSP."));
167  }
168 
169  findFirstFrameIndex();
170  m_isOpen = true;
171  findLastFrameIndex();
172 }
173 
182 {
183  getProperties();
184 
185  m_frameCount = m_firstFrame;
186  if (!getFrame(I, m_firstFrame)) {
187  throw(vpException(vpException::ioError, "Could not read the video first frame"));
188  }
189 
190  // Rewind to the first frame since open() should not increase the frame
191  // counter
192  m_frameCount = m_firstFrame;
193 
194  if (isVideoExtensionSupported()) {
195 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
196 
197 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
198  m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
199 #else
200  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
201 #endif
202  m_frameCount--;
203 #endif
204  }
205 }
206 
215 {
216  getProperties();
217 
218  m_frameCount = m_firstFrame;
219  if (!getFrame(I, m_firstFrame)) {
220  throw(vpException(vpException::ioError, "Could not read the video first frame"));
221  }
222 
223  // Rewind to the first frame since open() should not increase the frame
224  // counter
225  m_frameCount = m_firstFrame;
226 
227  if (isVideoExtensionSupported()) {
228 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
229 
230 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
231  m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
232 #else
233  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
234 #endif
235  m_frameCount--;
236 #endif
237  }
238 }
239 
251 {
252  if (!m_isOpen) {
253  open(I);
254  }
255  if (m_imSequence != NULL) {
256  m_imSequence->setStep(m_frameStep);
257  bool skip_frame = false;
258  do {
259  try {
260  m_imSequence->acquire(I);
261  skip_frame = false;
262  } catch (...) {
263  skip_frame = true;
264  }
265  } while (skip_frame && m_imSequence->getImageNumber() < m_lastFrame);
266  m_frameCount = m_imSequence->getImageNumber();
267  m_frameName = m_imSequence->getImageName();
268  if (m_frameCount + m_frameStep > m_lastFrame) {
269  m_imSequence->setImageNumber(m_frameCount);
270  } else if (m_frameCount + m_frameStep < m_firstFrame) {
271  m_imSequence->setImageNumber(m_frameCount);
272  }
273  }
274 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
275  else {
276  m_capture >> m_frame;
277  if (m_frameStep == 1) {
278  m_frameCount++;
279  } else {
280 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
281  m_frameCount = (long)m_capture.get(cv::CAP_PROP_POS_FRAMES);
282  if (m_frameStep > 0) {
283  if (m_frameCount + m_frameStep <= m_lastFrame) {
284  m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
285  } else {
286  m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount - 1);
287  }
288  } else if (m_frameStep < 0) {
289  if (m_frameCount + m_frameStep >= m_firstFrame) {
290  m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
291  } else {
292  m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
293  }
294  }
295 #else
296  m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
297  if (m_frameStep > 0) {
298  if (m_frameCount + m_frameStep <= m_lastFrame) {
299  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
300  } else {
301  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount - 1);
302  }
303  } else if (m_frameStep < 0) {
304  if (m_frameCount + m_frameStep >= m_firstFrame) {
305  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
306  } else {
307  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
308  }
309  }
310 #endif
311  }
312 
313  if (m_frame.empty()) {
314  std::cout << "Warning: Unable to decode image " << m_frameCount - m_frameStep << std::endl;
315  if (m_lastframe_unknown) {
316  // Set last frame to this image index
317  setLastFrameIndex(m_frameCount - m_frameStep);
318  }
319  } else {
320  vpImageConvert::convert(m_frame, I);
321  }
322  }
323 #endif
324 }
325 
335 {
336  if (!m_isOpen) {
337  open(I);
338  }
339 
340  if (m_imSequence != NULL) {
341  m_imSequence->setStep(m_frameStep);
342  bool skip_frame = false;
343  do {
344  try {
345  m_imSequence->acquire(I);
346  skip_frame = false;
347  } catch (...) {
348  skip_frame = true;
349  }
350  } while (skip_frame && m_imSequence->getImageNumber() < m_lastFrame);
351  m_frameCount = m_imSequence->getImageNumber();
352  m_frameName = m_imSequence->getImageName();
353  if (m_frameCount + m_frameStep > m_lastFrame) {
354  m_imSequence->setImageNumber(m_frameCount);
355  } else if (m_frameCount + m_frameStep < m_firstFrame) {
356  m_imSequence->setImageNumber(m_frameCount);
357  }
358  }
359 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
360  else {
361  m_capture >> m_frame;
362  if (m_frameStep == 1) {
363  m_frameCount++;
364  } else {
365 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
366  m_frameCount = (long)m_capture.get(cv::CAP_PROP_POS_FRAMES);
367  if (m_frameStep > 0) {
368  if (m_frameCount + m_frameStep <= m_lastFrame) {
369  m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
370  } else {
371  m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount - 1);
372  }
373  } else if (m_frameStep < 0) {
374  if (m_frameCount + m_frameStep >= m_firstFrame) {
375  m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
376  } else {
377  m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
378  }
379  }
380 #else
381  m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
382  if (m_frameStep > 0) {
383  if (m_frameCount + m_frameStep <= m_lastFrame) {
384  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
385  } else {
386  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount - 1);
387  }
388  } else if (m_frameStep < 0) {
389  if (m_frameCount + m_frameStep >= m_firstFrame) {
390  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
391  } else {
392  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
393  }
394  }
395 #endif
396  }
397 
398  if (m_frame.empty()) {
399  std::cout << "Warning: Unable to decode image " << m_frameCount - m_frameStep << std::endl;
400  } else {
401  vpImageConvert::convert(m_frame, I);
402  }
403  }
404 #endif
405 }
406 
420 bool vpVideoReader::getFrame(vpImage<vpRGBa> &I, long frame_index)
421 {
422  if (m_imSequence != NULL) {
423  try {
424  m_imSequence->acquire(I, frame_index);
425  width = I.getWidth();
426  height = I.getHeight();
427  m_frameCount = m_imSequence->getImageNumber();
428  m_imSequence->setImageNumber(m_frameCount); // to not increment vpDiskGrabber next image
429  if (m_frameCount + m_frameStep > m_lastFrame) {
430  m_imSequence->setImageNumber(m_frameCount);
431  } else if (m_frameCount + m_frameStep < m_firstFrame) {
432  m_imSequence->setImageNumber(m_frameCount);
433  }
434  } catch (...) {
435  vpERROR_TRACE("Couldn't find the %u th frame", frame_index);
436  return false;
437  }
438  } else {
439 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000)
440  if (!m_capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
441  vpERROR_TRACE("Couldn't find the %ld th frame", frame_index);
442  return false;
443  }
444 
445  m_capture >> m_frame;
446  m_frameCount = frame_index + m_frameStep; // next index
447  m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount);
448  if (m_frame.empty()) {
449  // New trial that makes things working with opencv 3.0.0
450  m_capture >> m_frame;
451  if (m_frame.empty()) {
452  setLastFrameIndex(m_frameCount - m_frameStep);
453  return false;
454  } else {
455  vpImageConvert::convert(m_frame, I);
456  }
457  } else
458  vpImageConvert::convert(m_frame, I);
459 #elif defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
460  if (!m_capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
461  vpERROR_TRACE("Couldn't find the %ld th frame", frame_index);
462  return false;
463  }
464 
465  m_capture >> m_frame;
466  m_frameCount = frame_index + m_frameStep; // next index
467  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
468  if (m_frame.empty())
469  setLastFrameIndex(m_frameCount - m_frameStep);
470  else
471  vpImageConvert::convert(m_frame, I);
472 #endif
473  }
474  return true;
475 }
476 
491 {
492  if (m_imSequence != NULL) {
493  try {
494  m_imSequence->acquire(I, frame_index);
495  width = I.getWidth();
496  height = I.getHeight();
497  m_frameCount = m_imSequence->getImageNumber();
498  m_imSequence->setImageNumber(m_frameCount); // to not increment vpDiskGrabber next image
499  if (m_frameCount + m_frameStep > m_lastFrame) {
500  m_imSequence->setImageNumber(m_frameCount);
501  } else if (m_frameCount + m_frameStep < m_firstFrame) {
502  m_imSequence->setImageNumber(m_frameCount);
503  }
504  } catch (...) {
505  vpERROR_TRACE("Couldn't find the %u th frame", frame_index);
506  return false;
507  }
508  } else {
509 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
510  if (!m_capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
511  vpERROR_TRACE("Couldn't find the %ld th frame", frame_index);
512  return false;
513  }
514  m_capture >> m_frame;
515  if (m_frame.empty()) {
516  // New trial that makes things working with opencv 3.0.0
517  m_capture >> m_frame;
518  if (m_frame.empty()) {
519  setLastFrameIndex(m_frameCount - m_frameStep);
520  return false;
521  } else {
522  vpImageConvert::convert(m_frame, I);
523  }
524  } else {
525  vpImageConvert::convert(m_frame, I);
526  }
527 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
528  if (!m_capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
529  vpERROR_TRACE("Couldn't find the %ld th frame",
530  frame_index); // next index
531  return false;
532  }
533  m_capture >> m_frame;
534  m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
535  if (m_frameStep > 1) {
536  m_frameCount += m_frameStep - 1; // next index
537  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
538  } else if (m_frameStep < -1) {
539  m_frameCount += m_frameStep - 1; // next index
540  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
541  }
542  if (m_frame.empty())
543  setLastFrameIndex(m_frameCount - m_frameStep);
544  else
545  vpImageConvert::convert(m_frame, I);
546 #endif
547  }
548  return true;
549 }
550 
556 vpVideoReader::vpVideoFormatType vpVideoReader::getFormat(const std::string &filename) const
557 {
558  std::string ext = vpVideoReader::getExtension(filename);
559 
560  if (ext.compare(".PGM") == 0)
561  return FORMAT_PGM;
562  else if (ext.compare(".pgm") == 0)
563  return FORMAT_PGM;
564  else if (ext.compare(".PPM") == 0)
565  return FORMAT_PPM;
566  else if (ext.compare(".ppm") == 0)
567  return FORMAT_PPM;
568  else if (ext.compare(".JPG") == 0)
569  return FORMAT_JPEG;
570  else if (ext.compare(".jpg") == 0)
571  return FORMAT_JPEG;
572  else if (ext.compare(".JPEG") == 0)
573  return FORMAT_JPEG;
574  else if (ext.compare(".jpeg") == 0)
575  return FORMAT_JPEG;
576  else if (ext.compare(".PNG") == 0)
577  return FORMAT_PNG;
578  else if (ext.compare(".png") == 0)
579  return FORMAT_PNG;
580  else if (ext.compare(".TIFF") == 0)
581  return FORMAT_TIFF;
582  else if (ext.compare(".tiff") == 0)
583  return FORMAT_TIFF;
584  else if (ext.compare(".BMP") == 0)
585  return FORMAT_BMP;
586  else if (ext.compare(".bmp") == 0)
587  return FORMAT_BMP;
588  else if (ext.compare(".DIB") == 0)
589  return FORMAT_DIB;
590  else if (ext.compare(".dib") == 0)
591  return FORMAT_DIB;
592  else if (ext.compare(".PBM") == 0)
593  return FORMAT_PBM;
594  else if (ext.compare(".pbm") == 0)
595  return FORMAT_PBM;
596  else if (ext.compare(".SR") == 0)
597  return FORMAT_PBM;
598  else if (ext.compare(".sr") == 0)
599  return FORMAT_PBM;
600  else if (ext.compare(".RAS") == 0)
601  return FORMAT_RASTER;
602  else if (ext.compare(".ras") == 0)
603  return FORMAT_RASTER;
604  else if (ext.compare(".JP2") == 0)
605  return FORMAT_JPEG2000;
606  else if (ext.compare(".jp2") == 0)
607  return FORMAT_JPEG2000;
608  else if (ext.compare(".AVI") == 0)
609  return FORMAT_AVI;
610  else if (ext.compare(".avi") == 0)
611  return FORMAT_AVI;
612  else if (ext.compare(".MPEG") == 0)
613  return FORMAT_MPEG;
614  else if (ext.compare(".mpeg") == 0)
615  return FORMAT_MPEG;
616  else if (ext.compare(".MPG") == 0)
617  return FORMAT_MPEG;
618  else if (ext.compare(".mpg") == 0)
619  return FORMAT_MPEG;
620  else if (ext.compare(".MPEG4") == 0)
621  return FORMAT_MPEG4;
622  else if (ext.compare(".mpeg4") == 0)
623  return FORMAT_MPEG4;
624  else if (ext.compare(".MP4") == 0)
625  return FORMAT_MPEG4;
626  else if (ext.compare(".mp4") == 0)
627  return FORMAT_MPEG4;
628  else if (ext.compare(".MOV") == 0)
629  return FORMAT_MOV;
630  else if (ext.compare(".mov") == 0)
631  return FORMAT_MOV;
632  else if (ext.compare(".OGV") == 0)
633  return FORMAT_OGV;
634  else if (ext.compare(".ogv") == 0)
635  return FORMAT_OGV;
636  else if (ext.compare(".WMV") == 0)
637  return FORMAT_WMV;
638  else if (ext.compare(".wmv") == 0)
639  return FORMAT_WMV;
640  else if (ext.compare(".FLV") == 0)
641  return FORMAT_FLV;
642  else if (ext.compare(".flv") == 0)
643  return FORMAT_FLV;
644  else if (ext.compare(".MKV") == 0)
645  return FORMAT_MKV;
646  else if (ext.compare(".mkv") == 0)
647  return FORMAT_MKV;
648  else if (ext.compare(".MTS") == 0)
649  return FORMAT_MTS;
650  else if (ext.compare(".mts") == 0)
651  return FORMAT_MTS;
652  else
653  return FORMAT_UNKNOWN;
654 }
655 
656 // return the extension of the file including the dot
657 std::string vpVideoReader::getExtension(const std::string &filename)
658 {
659  // extract the extension
660  size_t dot = filename.find_last_of(".");
661  std::string ext = filename.substr(dot, filename.size() - 1);
662  return ext;
663 }
664 
668 void vpVideoReader::findLastFrameIndex()
669 {
670  if (!m_isOpen) {
671  vpERROR_TRACE("Use the open method before");
672  throw(vpException(vpException::notInitialized, "file not yet opened"));
673  }
674 
675  if (m_imSequence != NULL) {
676  if (!m_lastFrameIndexIsSet) {
677  std::string imageNameFormat = vpIoTools::getName(m_videoName);
678  std::string dirName = vpIoTools::getParent(m_videoName);
679  if (dirName == "") {
680  dirName = ".";
681  }
682  std::vector<std::string> files = vpIoTools::getDirFiles(dirName);
683  m_lastFrame = 0;
684  for (size_t i = 0; i < files.size(); i++) {
685  // Checking that file name satisfies image format,
686  // specified by imageNameFormat, and extracting imageIndex
687  long imageIndex = vpIoTools::getIndex(files[i], imageNameFormat);
688  if ((imageIndex != -1) && (imageIndex > m_lastFrame)) {
689  m_lastFrame = imageIndex;
690  }
691  }
692  }
693  }
694 
695 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
696  else if (!m_lastFrameIndexIsSet) {
697  m_lastFrame = (long)m_capture.get(cv::CAP_PROP_FRAME_COUNT);
698  if (m_lastFrame <= 2) {
699  // With visp/tutorial/detection/matching/video-postcard.mpeg that is MPEG-2 it return 2 with OpenCV 3.0.0
700  // With visp-images/video/cube.mpeg that is MPEG-1 it return 1 with OpenCV 4.1.1
701  // We set video last frame to an arbitrary value 100000 and set a flag
702  m_lastframe_unknown = true;
703  m_lastFrame = 100000; // Set lastFrame to an arbitrary value
704  }
705  }
706 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
707  else if (!m_lastFrameIndexIsSet) {
708  m_lastFrame = (long)m_capture.get(CV_CAP_PROP_FRAME_COUNT);
709  if (m_lastFrame <= 2) {
710  // With visp/tutorial/detection/matching/video-postcard.mpeg that is MPEG-2 it return 2 with OpenCV 3.0.0
711  // With visp-images/video/cube.mpeg that is MPEG-1 it return 1 with OpenCV 4.1.1
712  // We set video last frame to an arbitrary value 100000 and set a flag
713  m_lastframe_unknown = true;
714  m_lastFrame = 100000; // Set lastFrame to an arbitrary value
715  }
716  }
717 #endif
718 }
719 
723 void vpVideoReader::findFirstFrameIndex()
724 {
725  if (m_imSequence != NULL) {
726  if (!m_firstFrameIndexIsSet) {
727  std::string imageNameFormat = vpIoTools::getName(m_videoName);
728  std::string dirName = vpIoTools::getParent(m_videoName);
729  if (dirName == "") {
730  dirName = ".";
731  }
732  std::vector<std::string> files = vpIoTools::getDirFiles(dirName);
733  m_firstFrame = -1;
734  for (size_t i = 0; i < files.size(); i++) {
735  // Checking that file name satisfies image format, specified by
736  // imageNameFormat, and extracting imageIndex
737  long imageIndex = vpIoTools::getIndex(files[i], imageNameFormat);
738 
739  if ((imageIndex != -1) && (imageIndex < m_firstFrame || m_firstFrame == -1)) {
740  m_firstFrame = imageIndex;
741  }
742  }
743  m_imSequence->setImageNumber(m_firstFrame);
744  }
745  }
746 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
747  else if (!m_firstFrameIndexIsSet) {
748  m_firstFrame = 1L;
749  }
750 #endif
751 }
752 
756 bool vpVideoReader::isImageExtensionSupported() const
757 {
758  return (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG ||
759  m_formatType == FORMAT_PNG || m_formatType == FORMAT_TIFF || m_formatType == FORMAT_BMP ||
760  m_formatType == FORMAT_DIB || m_formatType == FORMAT_PBM || m_formatType == FORMAT_RASTER ||
761  m_formatType == FORMAT_JPEG2000);
762 }
763 
767 bool vpVideoReader::isVideoExtensionSupported() const
768 {
769  return (m_formatType == FORMAT_AVI || m_formatType == FORMAT_MPEG || m_formatType == FORMAT_MPEG4 ||
770  m_formatType == FORMAT_MOV || m_formatType == FORMAT_OGV || m_formatType == FORMAT_WMV ||
771  m_formatType == FORMAT_FLV || m_formatType == FORMAT_MKV || m_formatType == FORMAT_MTS);
772 }
773 
798 {
799  this->acquire(I);
800  return *this;
801 }
802 
827 {
828  this->acquire(I);
829  return *this;
830 }
831 
836 bool vpVideoReader::checkImageNameFormat(const std::string &format) const
837 {
838  size_t indexBegin = format.find_last_of('%');
839  size_t indexEnd = format.find_first_of('d', indexBegin);
840  if (indexBegin == std::string::npos || indexEnd == std::string::npos) {
841  return false;
842  }
843  for (size_t i = indexBegin + 1; i < indexEnd; i++) {
844  if (!std::isdigit(format[i])) {
845  return false;
846  }
847  }
848  return true;
849 }
850 
859 {
860  // Video format
861  switch (m_formatType) {
862  case FORMAT_AVI:
863  case FORMAT_MPEG:
864  case FORMAT_MPEG4:
865  case FORMAT_MTS:
866  case FORMAT_MOV:
867  case FORMAT_OGV:
868  case FORMAT_WMV:
869  case FORMAT_FLV:
870  case FORMAT_MKV:
871  return true;
872  default:
873  return false;
874  }
875 }
Class to grab (ie. read) images from the disk.
void setStep(long step)
void setGenericName(const std::string &genericName)
std::string getImageName() const
void setImageNumber(long number)
long getImageNumber() const
void acquire(vpImage< unsigned char > &I)
error that can be emited by ViSP classes.
Definition: vpException.h:72
@ ioError
I/O error.
Definition: vpException.h:91
@ badValue
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:97
@ notInitialized
Used to indicate that a parameter is not initialized.
Definition: vpException.h:98
@ fatalError
Fatal error.
Definition: vpException.h:96
Base class for all video devices. It is designed to provide a front end to video sources.
unsigned int height
Number of rows in the image.
unsigned int width
Number of columns in the image.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Error that can be emited by the vpImage class and its derivates.
unsigned int getWidth() const
Definition: vpImage.h:247
unsigned int getHeight() const
Definition: vpImage.h:189
static long getIndex(const std::string &filename, const std::string &format)
Definition: vpIoTools.cpp:1591
static std::vector< std::string > getDirFiles(const std::string &dirname)
Definition: vpIoTools.cpp:1958
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1633
static std::string getName(const std::string &pathname)
Definition: vpIoTools.cpp:1528
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
bool isVideoFormat() const
void acquire(vpImage< vpRGBa > &I)
void setLastFrameIndex(const long last_frame)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
bool getFrame(vpImage< vpRGBa > &I, long frame)
virtual ~vpVideoReader()
vpVideoReader & operator>>(vpImage< unsigned char > &I)
#define vpERROR_TRACE
Definition: vpDebug.h:393