Visual Servoing Platform  version 3.6.1 under development (2024-04-18)
vpVideoReader.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 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 _vpVideoReader_h_
40 #define _vpVideoReader_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 
164 class VISP_EXPORT vpVideoReader : public vpFrameGrabber
165 {
166 private:
168  vpDiskGrabber *m_imSequence;
169 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
171  cv::VideoCapture m_capture;
172  cv::Mat m_frame;
173  bool m_lastframe_unknown;
174 #endif
176  typedef enum
177  {
178  FORMAT_PGM,
179  FORMAT_PPM,
180  FORMAT_JPEG,
181  FORMAT_PNG,
182  // Formats supported by opencv
183  FORMAT_TIFF,
184  FORMAT_BMP,
185  FORMAT_DIB,
186  FORMAT_PBM,
187  FORMAT_RASTER,
188  FORMAT_JPEG2000,
189  // Video format
190  FORMAT_AVI,
191  FORMAT_MPEG,
192  FORMAT_MPEG4,
193  FORMAT_MTS,
194  FORMAT_MOV,
195  FORMAT_OGV,
196  FORMAT_WMV,
197  FORMAT_FLV,
198  FORMAT_MKV,
199  FORMAT_UNKNOWN
200  } vpVideoFormatType;
201 
203  vpVideoFormatType m_formatType;
204 
206  std::string m_videoName;
207  std::string m_frameName;
209  bool m_initFileName;
211  bool m_isOpen;
213  long m_frameCount; // Index of the next image
215  long m_firstFrame;
217  long m_lastFrame;
218  bool m_firstFrameIndexIsSet;
219  bool m_lastFrameIndexIsSet;
221  long m_frameStep;
222  double m_frameRate;
223 
224 public:
225  vpVideoReader();
226  virtual ~vpVideoReader();
227 
228  void acquire(vpImage<vpRGBa> &I);
230  void close() { ; }
231 
235  inline bool end()
236  {
237  if (m_frameStep > 0) {
238  if (m_frameCount + m_frameStep > m_lastFrame)
239  return true;
240  }
241  else if (m_frameStep < 0) {
242  if (m_frameCount + m_frameStep < m_firstFrame)
243  return true;
244  }
245  return false;
246  }
247  bool getFrame(vpImage<vpRGBa> &I, long frame);
248  bool getFrame(vpImage<unsigned char> &I, long frame);
249 
255  double getFramerate()
256  {
257  if (!m_isOpen) {
258  getProperties();
259  }
260  return m_frameRate;
261  }
262 
272  inline long getFrameIndex() const { return m_frameCount; }
273 
277  inline std::string getFrameName() const { return m_frameName; }
278 
284  inline long getFirstFrameIndex()
285  {
286  if (!m_isOpen) {
287  getProperties();
288  }
289  return m_firstFrame;
290  }
291 
297  inline long getLastFrameIndex()
298  {
299  if (!m_isOpen) {
300  getProperties();
301  }
302  return m_lastFrame;
303  }
304 
310  inline long getFrameStep() const { return m_frameStep; }
311 
312  bool isVideoFormat() const;
313  void open(vpImage<vpRGBa> &I);
314  void open(vpImage<unsigned char> &I);
315 
316  vpVideoReader &operator>>(vpImage<unsigned char> &I);
317  vpVideoReader &operator>>(vpImage<vpRGBa> &I);
318 
327  inline void resetFrameCounter() { m_frameCount = m_firstFrame; }
328  void setFileName(const std::string &filename);
329 
338  inline void setFirstFrameIndex(const long first_frame)
339  {
340  m_firstFrameIndexIsSet = true;
341  m_firstFrame = first_frame;
342  }
343 
351  inline void setLastFrameIndex(const long last_frame)
352  {
353  this->m_lastFrameIndexIsSet = true;
354  m_lastFrame = last_frame;
355  }
356 
365  inline void setFrameStep(const long frame_step) { m_frameStep = frame_step; }
366 
367 private:
368  vpVideoFormatType getFormat(const std::string &filename) const;
369  static std::string getExtension(const std::string &filename);
370  void findFirstFrameIndex();
371  void findLastFrameIndex();
372  bool isImageExtensionSupported() const;
373  bool isVideoExtensionSupported() const;
374  bool checkImageNameFormat(const std::string &format) const;
375  void getProperties();
376 };
377 
378 #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