Visual Servoing Platform  version 3.6.1 under development (2025-03-12)
tutorial-video-reader.cpp
1 #include <visp3/core/vpConfig.h>
3 #include <visp3/gui/vpDisplayFactory.h>
5 #include <visp3/core/vpTime.h>
6 #include <visp3/io/vpVideoReader.h>
8 
15 int main(int argc, char **argv)
16 {
17 #if ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
18 #ifdef ENABLE_VISP_NAMESPACE
19  using namespace VISP_NAMESPACE_NAME;
20 #endif
21  try {
22  std::string videoname = "video.mp4";
23 
24  for (int i = 1; i < argc; i++) {
25  if (std::string(argv[i]) == "--name" && i + 1 < argc) {
26  videoname = std::string(argv[++i]);
27  }
28  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
29  std::cout << "\nUsage: " << argv[0]
30  << " [--name <video name> (default: " << videoname << ")]"
31  << " [--help] [-h]\n"
32  << std::endl;
33  return EXIT_SUCCESS;
34  }
35  }
36 
39  vpVideoReader g;
42  g.setFileName(videoname);
45  g.open(I);
47  std::cout << "Video name : " << videoname << std::endl;
48  std::cout << "Video framerate: " << g.getFramerate() << "Hz" << std::endl;
49  std::cout << "Video dimension: " << I.getWidth() << " " << I.getHeight() << std::endl;
50 
51 #ifdef VISP_HAVE_DISPLAY
53 #else
54  std::cout << "No image viewer is available..." << std::endl;
55 #endif
56  vpDisplay::setTitle(I, "Video reader");
57 
58  unsigned cpt = 1;
59  bool quit = false;
61  while ((!g.end()) && (!quit)) {
64  double t = vpTime::measureTimeMs();
67  g.acquire(I);
70  vpDisplay::displayText(I, 20, 20, "Click to quit", vpColor::red);
71  std::stringstream ss;
72  ss << "Frame: " << ++cpt;
73  std::cout << "Read " << ss.str() << std::endl;
74  vpDisplay::displayText(I, 40, 20, ss.str(), vpColor::red);
76  if (vpDisplay::getClick(I, false)) {
77  quit = true;
78  }
80  vpTime::wait(t, 1000. / g.getFramerate());
82  }
83  if (!quit) {
84  std::cout << "End of video was reached" << std::endl;
85  }
86 #ifdef VISP_HAVE_DISPLAY
87  delete d;
88 #endif
89  }
90  catch (const vpException &e) {
91  std::cout << e.getMessage() << std::endl;
92  }
93 #else
94  (void)argc;
95  (void)argv;
96  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
97 #endif
98  return EXIT_SUCCESS;
99 }
static const vpColor red
Definition: vpColor.h:198
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void setTitle(const vpImage< unsigned char > &I, const std::string &windowtitle)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
const char * getMessage() const
Definition: vpException.cpp:65
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
double getFramerate()
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()