Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
vpVideoReader.h
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 sequences of images .
33  *
34  * Authors:
35  * Nicolas Melchior
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
45 #ifndef vpVideoReader_H
46 #define vpVideoReader_H
47 
48 #include <string>
49 
50 #include <visp3/io/vpDiskGrabber.h>
51 
52 #if VISP_HAVE_OPENCV_VERSION >= 0x020200
53 #include "opencv2/highgui/highgui.hpp"
54 #elif VISP_HAVE_OPENCV_VERSION >= 0x020000
55 #include "opencv/highgui.h"
56 #endif
57 
171 class VISP_EXPORT vpVideoReader : public vpFrameGrabber
172 {
173 private:
175  vpDiskGrabber *m_imSequence;
176 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
177  cv::VideoCapture m_capture;
179  cv::Mat m_frame;
180  bool m_lastframe_unknown;
181 #endif
182  typedef enum {
184  FORMAT_PGM,
185  FORMAT_PPM,
186  FORMAT_JPEG,
187  FORMAT_PNG,
188  // Formats supported by opencv
189  FORMAT_TIFF,
190  FORMAT_BMP,
191  FORMAT_DIB,
192  FORMAT_PBM,
193  FORMAT_RASTER,
194  FORMAT_JPEG2000,
195  // Video format
196  FORMAT_AVI,
197  FORMAT_MPEG,
198  FORMAT_MPEG4,
199  FORMAT_MTS,
200  FORMAT_MOV,
201  FORMAT_OGV,
202  FORMAT_WMV,
203  FORMAT_FLV,
204  FORMAT_MKV,
205  FORMAT_UNKNOWN
206  } vpVideoFormatType;
207 
209  vpVideoFormatType m_formatType;
210 
212  std::string m_fileName;
214  bool m_initFileName;
216  bool m_isOpen;
218  long m_frameCount; // Index of the next image
220  long m_firstFrame;
222  long m_lastFrame;
223  bool m_firstFrameIndexIsSet;
224  bool m_lastFrameIndexIsSet;
226  long m_frameStep;
227  double m_frameRate;
228 
229  // private:
230  //#ifndef DOXYGEN_SHOULD_SKIP_THIS
231  // vpVideoReader(const vpVideoReader &)
232  // : vpFrameGrabber(), m_imSequence(NULL),
233  // #if VISP_HAVE_OPENCV_VERSION >= 0x020100
234  // m_capture(), m_frame(),
235  // #endif
236  // m_formatType(FORMAT_UNKNOWN), m_initFileName(false), m_isOpen(false),
237  // m_frameCount(0), m_firstFrame(0), m_lastFrame(0),
238  // m_firstFrameIndexIsSet(false), m_lastFrameIndexIsSet(false)
239  // {
240  // throw vpException(vpException::functionNotImplementedError, "Not
241  // implemented!");
242  // }
243  // vpVideoReader &operator=(const vpVideoReader &){
244  // throw vpException(vpException::functionNotImplementedError, "Not
245  // implemented!"); return *this;
246  // }
247  //#endif
248 
249 public:
250  vpVideoReader();
251  virtual ~vpVideoReader();
252 
253  void acquire(vpImage<vpRGBa> &I);
255  void close() { ; }
256 
260  inline bool end()
261  {
262  if (m_frameStep > 0) {
263  if (m_frameCount + m_frameStep > m_lastFrame)
264  return true;
265  } else if (m_frameStep < 0) {
266  if (m_frameCount + m_frameStep < m_firstFrame)
267  return true;
268  }
269  return false;
270  }
271  bool getFrame(vpImage<vpRGBa> &I, long frame);
272  bool getFrame(vpImage<unsigned char> &I, long frame);
278  double getFramerate()
279  {
280  if (!m_isOpen) {
281  getProperties();
282  }
283  return m_frameRate;
284  }
285 
295  inline long getFrameIndex() const { return m_frameCount; }
296 
302  inline long getFirstFrameIndex()
303  {
304  if (!m_isOpen) {
305  getProperties();
306  }
307  return m_firstFrame;
308  }
314  inline long getLastFrameIndex()
315  {
316  if (!m_isOpen) {
317  getProperties();
318  }
319  return m_lastFrame;
320  }
326  inline long getFrameStep() const { return m_frameStep; }
327  void open(vpImage<vpRGBa> &I);
328  void open(vpImage<unsigned char> &I);
329 
330  vpVideoReader &operator>>(vpImage<unsigned char> &I);
331  vpVideoReader &operator>>(vpImage<vpRGBa> &I);
332 
341  inline void resetFrameCounter() { m_frameCount = m_firstFrame; }
342  void setFileName(const std::string &filename);
351  inline void setFirstFrameIndex(const long first_frame)
352  {
353  m_firstFrameIndexIsSet = true;
354  m_firstFrame = first_frame;
355  }
363  inline void setLastFrameIndex(const long last_frame)
364  {
365  this->m_lastFrameIndexIsSet = true;
366  m_lastFrame = last_frame;
367  }
368 
377  inline void setFrameStep(const long frame_step) { m_frameStep = frame_step; }
378 
379 private:
380  vpVideoFormatType getFormat(const std::string &filename) const;
381  static std::string getExtension(const std::string &filename);
382  void findFirstFrameIndex();
383  void findLastFrameIndex();
384  bool isImageExtensionSupported() const;
385  bool isVideoExtensionSupported() const;
386  long extractImageIndex(const std::string &imageName, const std::string &format) const;
387  bool checkImageNameFormat(const std::string &format) const;
388  void getProperties();
389 };
390 
391 #endif
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
long getFirstFrameIndex()
virtual void open(vpImage< unsigned char > &I)=0
long getLastFrameIndex()
void resetFrameCounter()
Class to grab (ie. read) images from the disk.
Base class for all video devices. It is designed to provide a front end to video sources.
long getFrameIndex() const
void setLastFrameIndex(const long last_frame)
long getFrameStep() const
double getFramerate()
void setFrameStep(const long frame_step)
void setFirstFrameIndex(const long first_frame)
virtual void acquire(vpImage< unsigned char > &I)=0