Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
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 *imSequence;
176 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
177  cv::VideoCapture capture;
179  cv::Mat frame;
180 #endif
181  typedef enum {
183  FORMAT_PGM,
184  FORMAT_PPM,
185  FORMAT_JPEG,
186  FORMAT_PNG,
187  // Formats supported by opencv
188  FORMAT_TIFF,
189  FORMAT_BMP,
190  FORMAT_DIB,
191  FORMAT_PBM,
192  FORMAT_RASTER,
193  FORMAT_JPEG2000,
194  // Video format
195  FORMAT_AVI,
196  FORMAT_MPEG,
197  FORMAT_MPEG4,
198  FORMAT_MOV,
199  FORMAT_OGV,
200  FORMAT_WMV,
201  FORMAT_FLV,
202  FORMAT_MKV,
203  FORMAT_UNKNOWN
204  } vpVideoFormatType;
205 
207  vpVideoFormatType formatType;
208 
210  char fileName[FILENAME_MAX];
212  bool initFileName;
214  bool isOpen;
216  long frameCount; // Index of the next image
218  long firstFrame;
220  long lastFrame;
221  bool firstFrameIndexIsSet;
222  bool lastFrameIndexIsSet;
224  long frameStep;
225  double frameRate;
226 
227  // private:
228  //#ifndef DOXYGEN_SHOULD_SKIP_THIS
229  // vpVideoReader(const vpVideoReader &)
230  // : vpFrameGrabber(), imSequence(NULL),
231  // #if VISP_HAVE_OPENCV_VERSION >= 0x020100
232  // capture(), frame(),
233  // #endif
234  // formatType(FORMAT_UNKNOWN), initFileName(false), isOpen(false),
235  // frameCount(0), firstFrame(0), lastFrame(0),
236  // firstFrameIndexIsSet(false), lastFrameIndexIsSet(false)
237  // {
238  // throw vpException(vpException::functionNotImplementedError, "Not
239  // implemented!");
240  // }
241  // vpVideoReader &operator=(const vpVideoReader &){
242  // throw vpException(vpException::functionNotImplementedError, "Not
243  // implemented!"); return *this;
244  // }
245  //#endif
246 
247 public:
248  vpVideoReader();
249  virtual ~vpVideoReader();
250 
251  void acquire(vpImage<vpRGBa> &I);
253  void close() { ; }
254 
258  inline bool end()
259  {
260  if (frameStep > 0) {
261  if (frameCount + frameStep > lastFrame)
262  return true;
263  } else if (frameStep < 0) {
264  if (frameCount + frameStep < firstFrame)
265  return true;
266  }
267  return false;
268  }
269  bool getFrame(vpImage<vpRGBa> &I, long frame);
270  bool getFrame(vpImage<unsigned char> &I, long frame);
276  double getFramerate()
277  {
278  if (!isOpen) {
279  getProperties();
280  }
281  return frameRate;
282  }
283 
293  inline long getFrameIndex() const { return frameCount; }
294 
300  inline long getFirstFrameIndex()
301  {
302  if (!isOpen) {
303  getProperties();
304  }
305  return firstFrame;
306  }
312  inline long getLastFrameIndex()
313  {
314  if (!isOpen) {
315  getProperties();
316  }
317  return lastFrame;
318  }
324  inline long getFrameStep() const { return frameStep; }
325  void open(vpImage<vpRGBa> &I);
326  void open(vpImage<unsigned char> &I);
327 
328  vpVideoReader &operator>>(vpImage<unsigned char> &I);
329  vpVideoReader &operator>>(vpImage<vpRGBa> &I);
330 
339  inline void resetFrameCounter() { frameCount = firstFrame; }
340  void setFileName(const char *filename);
341  void setFileName(const std::string &filename);
350  inline void setFirstFrameIndex(const long first_frame)
351  {
352  this->firstFrameIndexIsSet = true;
353  this->firstFrame = first_frame;
354  }
362  inline void setLastFrameIndex(const long last_frame)
363  {
364  this->lastFrameIndexIsSet = true;
365  this->lastFrame = last_frame;
366  }
367 
376  inline void setFrameStep(const long frame_step) { this->frameStep = frame_step; }
377 
378 private:
379  vpVideoFormatType getFormat(const char *filename);
380  static std::string getExtension(const std::string &filename);
381  void findFirstFrameIndex();
382  void findLastFrameIndex();
383  bool isImageExtensionSupported();
384  bool isVideoExtensionSupported();
385  long extractImageIndex(const std::string &imageName, const std::string &format);
386  bool checkImageNameFormat(const std::string &format);
387  void getProperties();
388 };
389 
390 #endif
long getFrameIndex() const
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 getFrameStep() const
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.
void setLastFrameIndex(const long last_frame)
double getFramerate()
void setFrameStep(const long frame_step)
void setFirstFrameIndex(const long first_frame)
virtual void acquire(vpImage< unsigned char > &I)=0