Visual Servoing Platform  version 3.6.1 under development (2025-01-20)
vpVideoReader.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Read videos and sequences of images .
32  */
33 
39 #ifndef VP_VIDEO_READER_H
40 #define VP_VIDEO_READER_H
41 
42 #include <string>
43 
44 #include <visp3/core/vpConfig.h>
45 #include <visp3/io/vpDiskGrabber.h>
46 
47 #if (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
48 #include <opencv2/highgui/highgui.hpp>
49 #elif (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
50 #include <opencv2/videoio/videoio.hpp>
51 #endif
52 
53 BEGIN_VISP_NAMESPACE
54 
180 class VISP_EXPORT vpVideoReader : public vpFrameGrabber
181 {
182 private:
184  vpDiskGrabber *m_imSequence;
185 #if ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
187  cv::VideoCapture m_capture;
188  cv::Mat m_frame;
189  bool m_lastframe_unknown;
190 #endif
192  typedef enum
193  {
194  FORMAT_PGM,
195  FORMAT_PPM,
196  FORMAT_JPEG,
197  FORMAT_PNG,
198  // Formats supported by opencv
199  FORMAT_TIFF,
200  FORMAT_BMP,
201  FORMAT_DIB,
202  FORMAT_PBM,
203  FORMAT_RASTER,
204  FORMAT_JPEG2000,
205  // Video format
206  FORMAT_AVI,
207  FORMAT_MPEG,
208  FORMAT_MPEG4,
209  FORMAT_MTS,
210  FORMAT_MOV,
211  FORMAT_OGV,
212  FORMAT_WMV,
213  FORMAT_FLV,
214  FORMAT_MKV,
215  FORMAT_UNKNOWN
216  } vpVideoFormatType;
217 
219  vpVideoFormatType m_formatType;
220 
222  std::string m_videoName;
223  std::string m_frameName;
225  bool m_initFileName;
227  bool m_isOpen;
229  long m_frameCount; // Index of the next image
231  long m_firstFrame;
233  long m_lastFrame;
234  bool m_firstFrameIndexIsSet;
235  bool m_lastFrameIndexIsSet;
237  long m_frameStep;
238  double m_frameRate;
239 
240 public:
241  vpVideoReader();
242  virtual ~vpVideoReader();
243 
244  void acquire(vpImage<vpRGBa> &I);
246  void close() { ; }
247 
251  inline bool end()
252  {
253  if (m_frameStep > 0) {
254  if (m_frameCount + m_frameStep > m_lastFrame)
255  return true;
256  }
257  else if (m_frameStep < 0) {
258  if (m_frameCount + m_frameStep < m_firstFrame)
259  return true;
260  }
261  return false;
262  }
263  bool getFrame(vpImage<vpRGBa> &I, long frame);
264  bool getFrame(vpImage<unsigned char> &I, long frame);
265 
271  double getFramerate()
272  {
273  if (!m_isOpen) {
274  getProperties();
275  }
276  return m_frameRate;
277  }
278 
288  inline long getFrameIndex() const { return m_frameCount; }
289 
293  inline std::string getFrameName() const { return m_frameName; }
294 
300  inline long getFirstFrameIndex()
301  {
302  if (!m_isOpen) {
303  getProperties();
304  }
305  return m_firstFrame;
306  }
307 
313  inline long getLastFrameIndex()
314  {
315  if (!m_isOpen) {
316  getProperties();
317  }
318  return m_lastFrame;
319  }
320 
326  inline long getFrameStep() const { return m_frameStep; }
327 
328  bool isVideoFormat() const;
329  void open(vpImage<vpRGBa> &I);
330  void open(vpImage<unsigned char> &I);
331 
332  vpVideoReader &operator>>(vpImage<unsigned char> &I);
333  vpVideoReader &operator>>(vpImage<vpRGBa> &I);
334 
343  inline void resetFrameCounter() { m_frameCount = m_firstFrame; }
344  void setFileName(const std::string &filename);
345 
354  inline void setFirstFrameIndex(const long first_frame)
355  {
356  m_firstFrameIndexIsSet = true;
357  m_firstFrame = first_frame;
358  }
359 
367  inline void setLastFrameIndex(const long last_frame)
368  {
369  this->m_lastFrameIndexIsSet = true;
370  m_lastFrame = last_frame;
371  }
372 
381  inline void setFrameStep(const long frame_step) { m_frameStep = frame_step; }
382 
383 private:
384  vpVideoFormatType getFormat(const std::string &filename) const;
385  static std::string getExtension(const std::string &filename);
386  void findFirstFrameIndex();
387  void findLastFrameIndex();
388  bool isImageExtensionSupported() const;
389  bool isVideoExtensionSupported() const;
390  bool checkImageNameFormat(const std::string &format) const;
391  void getProperties();
392 };
393 
394 END_VISP_NAMESPACE
395 
396 #endif
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.
virtual void open(vpImage< unsigned char > &I)=0
virtual void acquire(vpImage< unsigned char > &I)=0
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void setLastFrameIndex(const long last_frame)
long getLastFrameIndex()
void resetFrameCounter()
void setFirstFrameIndex(const long first_frame)
long getFirstFrameIndex()
void setFrameStep(const long frame_step)
long getFrameStep() const
std::string getFrameName() const
double getFramerate()
long getFrameIndex() const