Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
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), m_firstFrame(0), m_lastFrame(0),
63  m_firstFrameIndexIsSet(false), m_lastFrameIndexIsSet(false), m_frameStep(1), m_frameRate(0.)
64 {
65 }
66 
71 {
72  if (m_imSequence != NULL) {
73  delete m_imSequence;
74  }
75 }
76 
92 void vpVideoReader::setFileName(const std::string &filename)
93 {
94  if (filename.empty()) {
95  throw(vpImageException(vpImageException::noFileNameError, "filename empty "));
96  }
97 
98  m_videoName = filename;
99  m_frameName = filename;
100 
101  m_formatType = getFormat(filename);
102 
103  if (m_formatType == FORMAT_UNKNOWN) {
104  throw(vpException(vpException::badValue, "Filename extension not supported"));
105  }
106 
107  // checking image name format
108  if (isImageExtensionSupported()) {
109  std::string format = vpIoTools::getName(m_videoName);
110  if (!checkImageNameFormat(format)) {
111  throw(vpException(vpException::badValue, "Format of image name wasn't recognized: %s", format.c_str()));
112  }
113  }
114 
115  m_initFileName = true;
116 }
117 
121 void vpVideoReader::getProperties()
122 {
123  if (m_isOpen) {
124  return;
125  }
126 
127  if (!m_initFileName) {
128  throw(vpImageException(vpImageException::noFileNameError, "The generic filename has to be set"));
129  }
130 
131  if (isImageExtensionSupported()) {
132  m_imSequence = new vpDiskGrabber;
133  m_imSequence->setGenericName(m_videoName.c_str());
134  m_imSequence->setStep(m_frameStep);
135  if (m_firstFrameIndexIsSet) {
136  m_imSequence->setImageNumber(m_firstFrame);
137  }
138  m_frameRate = -1.;
139  } else if (isVideoExtensionSupported()) {
140 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
141  m_capture.open(m_videoName.c_str());
142 
143  if (!m_capture.isOpened()) {
144  throw(vpException(vpException::ioError, "Could not open the video %s with OpenCV", m_videoName.c_str()));
145  }
146 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
147  width = (unsigned int)m_capture.get(cv::CAP_PROP_FRAME_WIDTH);
148  height = (unsigned int)m_capture.get(cv::CAP_PROP_FRAME_HEIGHT);
149  m_frameRate = (double)m_capture.get(cv::CAP_PROP_FPS);
150 #else
151  width = (unsigned int)m_capture.get(CV_CAP_PROP_FRAME_WIDTH);
152  height = (unsigned int)m_capture.get(CV_CAP_PROP_FRAME_HEIGHT);
153  m_frameRate = m_capture.get(CV_CAP_PROP_FPS);
154 #endif
155 
156 #else
157  throw(vpException(vpException::fatalError, "To read video files ViSP should be build with opencv "
158  "3rd >= 2.1.0 party libraries."));
159 #endif
160  } else if (m_formatType == FORMAT_UNKNOWN) {
161  // vpERROR_TRACE("The format of the file does not correspond to a readable
162  // format.");
163  throw(vpException(vpException::fatalError, "The format of the file does "
164  "not correspond to a readable "
165  "format supported by ViSP."));
166  }
167 
168  findFirstFrameIndex();
169  m_isOpen = true;
170  findLastFrameIndex();
171 }
172 
181 {
182  getProperties();
183 
184  m_frameCount = m_firstFrame;
185  if (!getFrame(I, m_firstFrame)) {
186  throw(vpException(vpException::ioError, "Could not read the video first frame"));
187  }
188 
189  // Rewind to the first frame since open() should not increase the frame
190  // counter
191  m_frameCount = m_firstFrame;
192 
193  if (isVideoExtensionSupported()) {
194 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
195 
196 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
197  m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
198 #else
199  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
200 #endif
201  m_frameCount--;
202 #endif
203  }
204 }
205 
214 {
215  getProperties();
216 
217  m_frameCount = m_firstFrame;
218  if (!getFrame(I, m_firstFrame)) {
219  throw(vpException(vpException::ioError, "Could not read the video first frame"));
220  }
221 
222  // Rewind to the first frame since open() should not increase the frame
223  // counter
224  m_frameCount = m_firstFrame;
225 
226  if (isVideoExtensionSupported()) {
227 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
228 
229 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
230  m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
231 #else
232  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
233 #endif
234  m_frameCount--;
235 #endif
236  }
237 }
238 
250 {
251  if (!m_isOpen) {
252  open(I);
253  }
254  if (m_imSequence != NULL) {
255  m_imSequence->setStep(m_frameStep);
256  bool skip_frame = false;
257  do {
258  try {
259  m_imSequence->acquire(I);
260  skip_frame = false;
261  } catch (...) {
262  skip_frame = true;
263  }
264  } while (skip_frame && m_imSequence->getImageNumber() < m_lastFrame);
265  m_frameCount = m_imSequence->getImageNumber();
266  m_frameName = m_imSequence->getImageName();
267  if (m_frameCount + m_frameStep > m_lastFrame) {
268  m_imSequence->setImageNumber(m_frameCount);
269  } else if (m_frameCount + m_frameStep < m_firstFrame) {
270  m_imSequence->setImageNumber(m_frameCount);
271  }
272  }
273 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
274  else {
275  m_capture >> m_frame;
276  if (m_frameStep == 1) {
277  m_frameCount++;
278  } else {
279 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
280  m_frameCount = (long)m_capture.get(cv::CAP_PROP_POS_FRAMES);
281  if (m_frameStep > 0) {
282  if (m_frameCount + m_frameStep <= m_lastFrame) {
283  m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
284  } else {
285  m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount - 1);
286  }
287  } else if (m_frameStep < 0) {
288  if (m_frameCount + m_frameStep >= m_firstFrame) {
289  m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
290  } else {
291  m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
292  }
293  }
294 #else
295  m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
296  if (m_frameStep > 0) {
297  if (m_frameCount + m_frameStep <= m_lastFrame) {
298  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
299  } else {
300  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount - 1);
301  }
302  } else if (m_frameStep < 0) {
303  if (m_frameCount + m_frameStep >= m_firstFrame) {
304  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
305  } else {
306  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
307  }
308  }
309 #endif
310  }
311 
312  if (m_frame.empty()) {
313  std::cout << "Warning: Unable to decode image " << m_frameCount - m_frameStep << std::endl;
314  if (m_lastframe_unknown) {
315  // Set last frame to this image index
316  setLastFrameIndex(m_frameCount - m_frameStep);
317  }
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  }
401  else {
402  vpImageConvert::convert(m_frame, I);
403  }
404  }
405 #endif
406 }
407 
421 bool vpVideoReader::getFrame(vpImage<vpRGBa> &I, long frame_index)
422 {
423  if (m_imSequence != NULL) {
424  try {
425  m_imSequence->acquire(I, frame_index);
426  width = I.getWidth();
427  height = I.getHeight();
428  m_frameCount = m_imSequence->getImageNumber();
429  m_imSequence->setImageNumber(m_frameCount); // to not increment vpDiskGrabber next image
430  if (m_frameCount + m_frameStep > m_lastFrame) {
431  m_imSequence->setImageNumber(m_frameCount);
432  } else if (m_frameCount + m_frameStep < m_firstFrame) {
433  m_imSequence->setImageNumber(m_frameCount);
434  }
435  } catch (...) {
436  vpERROR_TRACE("Couldn't find the %u th frame", frame_index);
437  return false;
438  }
439  } else {
440 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000)
441  if (!m_capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
442  vpERROR_TRACE("Couldn't find the %ld th frame", frame_index);
443  return false;
444  }
445 
446  m_capture >> m_frame;
447  m_frameCount = frame_index + m_frameStep; // next index
448  m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount);
449  if (m_frame.empty()) {
450  // New trial that makes things working with opencv 3.0.0
451  m_capture >> m_frame;
452  if (m_frame.empty()) {
453  setLastFrameIndex(m_frameCount - m_frameStep);
454  return false;
455  } else {
456  vpImageConvert::convert(m_frame, I);
457  }
458  } else
459  vpImageConvert::convert(m_frame, I);
460 #elif defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
461  if (!m_capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
462  vpERROR_TRACE("Couldn't find the %ld th frame", frame_index);
463  return false;
464  }
465 
466  m_capture >> m_frame;
467  m_frameCount = frame_index + m_frameStep; // next index
468  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
469  if (m_frame.empty())
470  setLastFrameIndex(m_frameCount - m_frameStep);
471  else
472  vpImageConvert::convert(m_frame, I);
473 #endif
474  }
475  return true;
476 }
477 
492 {
493  if (m_imSequence != NULL) {
494  try {
495  m_imSequence->acquire(I, frame_index);
496  width = I.getWidth();
497  height = I.getHeight();
498  m_frameCount = m_imSequence->getImageNumber();
499  m_imSequence->setImageNumber(m_frameCount); // to not increment vpDiskGrabber next image
500  if (m_frameCount + m_frameStep > m_lastFrame) {
501  m_imSequence->setImageNumber(m_frameCount);
502  } else if (m_frameCount + m_frameStep < m_firstFrame) {
503  m_imSequence->setImageNumber(m_frameCount);
504  }
505  } catch (...) {
506  vpERROR_TRACE("Couldn't find the %u th frame", frame_index);
507  return false;
508  }
509  } else {
510 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
511  if (!m_capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
512  vpERROR_TRACE("Couldn't find the %ld th frame", frame_index);
513  return false;
514  }
515  m_capture >> m_frame;
516  if (m_frame.empty()) {
517  // New trial that makes things working with opencv 3.0.0
518  m_capture >> m_frame;
519  if (m_frame.empty()) {
520  setLastFrameIndex(m_frameCount - m_frameStep);
521  return false;
522  } else {
523  vpImageConvert::convert(m_frame, I);
524  }
525  } else {
526  vpImageConvert::convert(m_frame, I);
527  }
528 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
529  if (!m_capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
530  vpERROR_TRACE("Couldn't find the %ld th frame",
531  frame_index); // next index
532  return false;
533  }
534  m_capture >> m_frame;
535  m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
536  if (m_frameStep > 1) {
537  m_frameCount += m_frameStep - 1; // next index
538  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
539  } else if (m_frameStep < -1) {
540  m_frameCount += m_frameStep - 1; // next index
541  m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
542  }
543  if (m_frame.empty())
544  setLastFrameIndex(m_frameCount - m_frameStep);
545  else
546  vpImageConvert::convert(m_frame, I);
547 #endif
548  }
549  return true;
550 }
551 
557 vpVideoReader::vpVideoFormatType vpVideoReader::getFormat(const std::string &filename) const
558 {
559  std::string ext = vpVideoReader::getExtension(filename);
560 
561  if (ext.compare(".PGM") == 0)
562  return FORMAT_PGM;
563  else if (ext.compare(".pgm") == 0)
564  return FORMAT_PGM;
565  else if (ext.compare(".PPM") == 0)
566  return FORMAT_PPM;
567  else if (ext.compare(".ppm") == 0)
568  return FORMAT_PPM;
569  else if (ext.compare(".JPG") == 0)
570  return FORMAT_JPEG;
571  else if (ext.compare(".jpg") == 0)
572  return FORMAT_JPEG;
573  else if (ext.compare(".JPEG") == 0)
574  return FORMAT_JPEG;
575  else if (ext.compare(".jpeg") == 0)
576  return FORMAT_JPEG;
577  else if (ext.compare(".PNG") == 0)
578  return FORMAT_PNG;
579  else if (ext.compare(".png") == 0)
580  return FORMAT_PNG;
581  else if (ext.compare(".TIFF") == 0)
582  return FORMAT_TIFF;
583  else if (ext.compare(".tiff") == 0)
584  return FORMAT_TIFF;
585  else if (ext.compare(".BMP") == 0)
586  return FORMAT_BMP;
587  else if (ext.compare(".bmp") == 0)
588  return FORMAT_BMP;
589  else if (ext.compare(".DIB") == 0)
590  return FORMAT_DIB;
591  else if (ext.compare(".dib") == 0)
592  return FORMAT_DIB;
593  else if (ext.compare(".PBM") == 0)
594  return FORMAT_PBM;
595  else if (ext.compare(".pbm") == 0)
596  return FORMAT_PBM;
597  else if (ext.compare(".SR") == 0)
598  return FORMAT_PBM;
599  else if (ext.compare(".sr") == 0)
600  return FORMAT_PBM;
601  else if (ext.compare(".RAS") == 0)
602  return FORMAT_RASTER;
603  else if (ext.compare(".ras") == 0)
604  return FORMAT_RASTER;
605  else if (ext.compare(".JP2") == 0)
606  return FORMAT_JPEG2000;
607  else if (ext.compare(".jp2") == 0)
608  return FORMAT_JPEG2000;
609  else if (ext.compare(".AVI") == 0)
610  return FORMAT_AVI;
611  else if (ext.compare(".avi") == 0)
612  return FORMAT_AVI;
613  else if (ext.compare(".MPEG") == 0)
614  return FORMAT_MPEG;
615  else if (ext.compare(".mpeg") == 0)
616  return FORMAT_MPEG;
617  else if (ext.compare(".MPG") == 0)
618  return FORMAT_MPEG;
619  else if (ext.compare(".mpg") == 0)
620  return FORMAT_MPEG;
621  else if (ext.compare(".MPEG4") == 0)
622  return FORMAT_MPEG4;
623  else if (ext.compare(".mpeg4") == 0)
624  return FORMAT_MPEG4;
625  else if (ext.compare(".MP4") == 0)
626  return FORMAT_MPEG4;
627  else if (ext.compare(".mp4") == 0)
628  return FORMAT_MPEG4;
629  else if (ext.compare(".MOV") == 0)
630  return FORMAT_MOV;
631  else if (ext.compare(".mov") == 0)
632  return FORMAT_MOV;
633  else if (ext.compare(".OGV") == 0)
634  return FORMAT_OGV;
635  else if (ext.compare(".ogv") == 0)
636  return FORMAT_OGV;
637  else if (ext.compare(".WMV") == 0)
638  return FORMAT_WMV;
639  else if (ext.compare(".wmv") == 0)
640  return FORMAT_WMV;
641  else if (ext.compare(".FLV") == 0)
642  return FORMAT_FLV;
643  else if (ext.compare(".flv") == 0)
644  return FORMAT_FLV;
645  else if (ext.compare(".MKV") == 0)
646  return FORMAT_MKV;
647  else if (ext.compare(".mkv") == 0)
648  return FORMAT_MKV;
649  else if (ext.compare(".MTS") == 0)
650  return FORMAT_MTS;
651  else if (ext.compare(".mts") == 0)
652  return FORMAT_MTS;
653  else
654  return FORMAT_UNKNOWN;
655 }
656 
657 // return the extension of the file including the dot
658 std::string vpVideoReader::getExtension(const std::string &filename)
659 {
660  // extract the extension
661  size_t dot = filename.find_last_of(".");
662  std::string ext = filename.substr(dot, filename.size() - 1);
663  return ext;
664 }
665 
669 void vpVideoReader::findLastFrameIndex()
670 {
671  if (!m_isOpen) {
672  vpERROR_TRACE("Use the open method before");
673  throw(vpException(vpException::notInitialized, "file not yet opened"));
674  }
675 
676  if (m_imSequence != NULL) {
677  if (!m_lastFrameIndexIsSet) {
678  std::string imageNameFormat = vpIoTools::getName(m_videoName);
679  std::string dirName = vpIoTools::getParent(m_videoName);
680  if (dirName == "") {
681  dirName = ".";
682  }
683  std::vector<std::string> files = vpIoTools::getDirFiles(dirName);
684  m_lastFrame = 0;
685  for (size_t i = 0; i < files.size(); i++) {
686  // Checking that file name satisfies image format,
687  // specified by imageNameFormat, and extracting imageIndex
688  long imageIndex = vpIoTools::getIndex(files[i], imageNameFormat);
689  if ((imageIndex != -1) && (imageIndex > m_lastFrame)) {
690  m_lastFrame = imageIndex;
691  }
692  }
693  }
694  }
695 
696 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
697  else if (!m_lastFrameIndexIsSet) {
698  m_lastFrame = (long)m_capture.get(cv::CAP_PROP_FRAME_COUNT);
699  if (m_lastFrame <= 2) {
700  // With visp/tutorial/detection/matching/video-postcard.mpeg that is MPEG-2 it return 2 with OpenCV 3.0.0
701  // With visp-images/video/cube.mpeg that is MPEG-1 it return 1 with OpenCV 4.1.1
702  // We set video last frame to an arbitrary value 100000 and set a flag
703  m_lastframe_unknown = true;
704  m_lastFrame = 100000; // Set lastFrame to an arbitrary value
705  }
706  }
707 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
708  else if (!m_lastFrameIndexIsSet) {
709  m_lastFrame = (long)m_capture.get(CV_CAP_PROP_FRAME_COUNT);
710  if (m_lastFrame <= 2) {
711  // With visp/tutorial/detection/matching/video-postcard.mpeg that is MPEG-2 it return 2 with OpenCV 3.0.0
712  // With visp-images/video/cube.mpeg that is MPEG-1 it return 1 with OpenCV 4.1.1
713  // We set video last frame to an arbitrary value 100000 and set a flag
714  m_lastframe_unknown = true;
715  m_lastFrame = 100000; // Set lastFrame to an arbitrary value
716  }
717  }
718 #endif
719 }
720 
724 void vpVideoReader::findFirstFrameIndex()
725 {
726  if (m_imSequence != NULL) {
727  if (!m_firstFrameIndexIsSet) {
728  std::string imageNameFormat = vpIoTools::getName(m_videoName);
729  std::string dirName = vpIoTools::getParent(m_videoName);
730  if (dirName == "") {
731  dirName = ".";
732  }
733  std::vector<std::string> files = vpIoTools::getDirFiles(dirName);
734  m_firstFrame = -1;
735  for (size_t i = 0; i < files.size(); i++) {
736  // Checking that file name satisfies image format, specified by
737  // imageNameFormat, and extracting imageIndex
738  long imageIndex = vpIoTools::getIndex(files[i], imageNameFormat);
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 }
long getImageNumber() const
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:97
void setGenericName(const std::string &genericName)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
#define vpERROR_TRACE
Definition: vpDebug.h:393
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void setStep(long step)
error that can be emited by ViSP classes.
Definition: vpException.h:71
Error that can be emited by the vpImage class and its derivates.
static std::vector< std::string > getDirFiles(const std::string &dirname)
Definition: vpIoTools.cpp:1929
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1606
void open(vpImage< vpRGBa > &I)
bool getFrame(vpImage< vpRGBa > &I, long frame)
bool isVideoFormat() const
unsigned int height
Number of rows in the image.
std::string getImageName() const
void acquire(vpImage< vpRGBa > &I)
void setImageNumber(long number)
vpVideoReader & operator>>(vpImage< unsigned char > &I)
Class to grab (ie. read) images from the disk.
static std::string getName(const std::string &pathname)
Definition: vpIoTools.cpp:1510
Base class for all video devices. It is designed to provide a front end to video sources.
virtual ~vpVideoReader()
static long getIndex(const std::string &filename, const std::string &format)
Definition: vpIoTools.cpp:1573
void setLastFrameIndex(const long last_frame)
unsigned int getHeight() const
Definition: vpImage.h:188
Used to indicate that a parameter is not initialized.
Definition: vpException.h:98
void setFileName(const std::string &filename)
void acquire(vpImage< unsigned char > &I)
unsigned int getWidth() const
Definition: vpImage.h:246
unsigned int width
Number of columns in the image.