Visual Servoing Platform  version 3.4.0 under development (2021-04-11)
tutorial-video-reader.cpp
1 #include <visp3/gui/vpDisplayGDI.h>
3 #include <visp3/gui/vpDisplayOpenCV.h>
4 #include <visp3/gui/vpDisplayX.h>
6 #include <visp3/core/vpTime.h>
7 #include <visp3/io/vpVideoReader.h>
9 
16 int main(int argc, char **argv)
17 {
18 #if (VISP_HAVE_OPENCV_VERSION >= 0x020100)
19  try {
20  std::string videoname = "video.mp4";
21 
22  for (int i = 0; i < argc; i++) {
23  if (std::string(argv[i]) == "--name")
24  videoname = std::string(argv[i + 1]);
25  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
26  std::cout << "\nUsage: " << argv[0]
27  << " [--name <video name> (default: " << videoname << ")]"
28  << " [--help] [-h]\n" << std::endl;
29  return 0;
30  }
31  }
32 
35  vpVideoReader g;
38  g.setFileName(videoname);
41  g.open(I);
43  std::cout << "Video name : " << videoname << std::endl;
44  std::cout << "Video framerate: " << g.getFramerate() << "Hz" << std::endl;
45  std::cout << "Video dimension: " << I.getWidth() << " " << I.getHeight() << std::endl;
46 
47 #ifdef VISP_HAVE_X11
48  vpDisplayX d(I);
49 #elif defined(VISP_HAVE_GDI)
50  vpDisplayGDI d(I);
51 #elif defined(VISP_HAVE_OPENCV)
52  vpDisplayOpenCV d(I);
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;
60  while (!g.end()) {
63  double t = vpTime::measureTimeMs();
66  g.acquire(I);
69  vpDisplay::displayText(I, 20, 20, "Click to quit", vpColor::red);
70  std::stringstream ss;
71  ss << "Frame: " << cpt ++;
72  vpDisplay::displayText(I, 40, 20, ss.str(), vpColor::red);
74  if (vpDisplay::getClick(I, false))
75  break;
77  vpTime::wait(t, 1000. / g.getFramerate());
79  }
80  std::cout << "End of video was reached" << std::endl;
81  } catch (const vpException &e) {
82  std::cout << e.getMessage() << std::endl;
83  }
84 #else
85  (void)argc;
86  (void)argv;
87  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
88 #endif
89 }
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:173
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
error that can be emited by ViSP classes.
Definition: vpException.h:71
const char * getMessage() const
Definition: vpException.cpp:90
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:126
static const vpColor red
Definition: vpColor.h:217
void open(vpImage< vpRGBa > &I)
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
void acquire(vpImage< vpRGBa > &I)
unsigned int getHeight() const
Definition: vpImage.h:188
double getFramerate()
void setFileName(const std::string &filename)
static void setTitle(const vpImage< unsigned char > &I, const std::string &windowtitle)
unsigned int getWidth() const
Definition: vpImage.h:246