Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
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/io/vpDiskGrabber.h>
45 
46 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_VIDEOIO) && defined(HAVE_OPENCV_HIGHGUI)
47 #include <opencv2/videoio/videoio.hpp>
48 #include <opencv2/highgui/highgui.hpp>
49 #endif
50 
52 
178 class VISP_EXPORT vpVideoReader : public vpFrameGrabber
179 {
180 private:
182  vpDiskGrabber *m_imSequence;
183 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
185  cv::VideoCapture m_capture;
186  cv::Mat m_frame;
187  bool m_lastframe_unknown;
188 #endif
190  typedef enum
191  {
192  FORMAT_PGM,
193  FORMAT_PPM,
194  FORMAT_JPEG,
195  FORMAT_PNG,
196  // Formats supported by opencv
197  FORMAT_TIFF,
198  FORMAT_BMP,
199  FORMAT_DIB,
200  FORMAT_PBM,
201  FORMAT_RASTER,
202  FORMAT_JPEG2000,
203  // Video format
204  FORMAT_AVI,
205  FORMAT_MPEG,
206  FORMAT_MPEG4,
207  FORMAT_MTS,
208  FORMAT_MOV,
209  FORMAT_OGV,
210  FORMAT_WMV,
211  FORMAT_FLV,
212  FORMAT_MKV,
213  FORMAT_UNKNOWN
214  } vpVideoFormatType;
215 
217  vpVideoFormatType m_formatType;
218 
220  std::string m_videoName;
221  std::string m_frameName;
223  bool m_initFileName;
225  bool m_isOpen;
227  long m_frameCount; // Index of the next image
229  long m_firstFrame;
231  long m_lastFrame;
232  bool m_firstFrameIndexIsSet;
233  bool m_lastFrameIndexIsSet;
235  long m_frameStep;
236  double m_frameRate;
237 
238 public:
239  vpVideoReader();
240  virtual ~vpVideoReader();
241 
242  void acquire(vpImage<vpRGBa> &I);
244  void close() { ; }
245 
249  inline bool end()
250  {
251  if (m_frameStep > 0) {
252  if (m_frameCount + m_frameStep > m_lastFrame)
253  return true;
254  }
255  else if (m_frameStep < 0) {
256  if (m_frameCount + m_frameStep < m_firstFrame)
257  return true;
258  }
259  return false;
260  }
261  bool getFrame(vpImage<vpRGBa> &I, long frame);
262  bool getFrame(vpImage<unsigned char> &I, long frame);
263 
269  double getFramerate()
270  {
271  if (!m_isOpen) {
272  getProperties();
273  }
274  return m_frameRate;
275  }
276 
286  inline long getFrameIndex() const { return m_frameCount; }
287 
291  inline std::string getFrameName() const { return m_frameName; }
292 
298  inline long getFirstFrameIndex()
299  {
300  if (!m_isOpen) {
301  getProperties();
302  }
303  return m_firstFrame;
304  }
305 
311  inline long getLastFrameIndex()
312  {
313  if (!m_isOpen) {
314  getProperties();
315  }
316  return m_lastFrame;
317  }
318 
324  inline long getFrameStep() const { return m_frameStep; }
325 
326  bool isVideoFormat() const;
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);
343 
352  inline void setFirstFrameIndex(const long first_frame)
353  {
354  m_firstFrameIndexIsSet = true;
355  m_firstFrame = first_frame;
356  }
357 
365  inline void setLastFrameIndex(const long last_frame)
366  {
367  this->m_lastFrameIndexIsSet = true;
368  m_lastFrame = last_frame;
369  }
370 
379  inline void setFrameStep(const long frame_step) { m_frameStep = frame_step; }
380 
381 private:
382  vpVideoFormatType getFormat(const std::string &filename) const;
383  static std::string getExtension(const std::string &filename);
384  void findFirstFrameIndex();
385  void findLastFrameIndex();
386  bool isImageExtensionSupported() const;
387  bool isVideoExtensionSupported() const;
388  bool checkImageNameFormat(const std::string &format) const;
389  void getProperties();
390 };
391 
392 END_VISP_NAMESPACE
393 
394 #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