Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
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.mpg";
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] << " [--name <video name> (default: " << videoname << ")] [--help] [-h]\n" << std::endl;
27  return 0;
28  }
29  }
30 
33  vpVideoReader g;
36  g.setFileName(videoname);
39  g.open(I);
41  std::cout << "Video name : " << videoname << std::endl;
42  std::cout << "Video framerate: " << g.getFramerate() << "Hz" << std::endl;
43  std::cout << "Video dimension: " << I.getWidth() << " " << I.getHeight() << std::endl;
44 
45 #ifdef VISP_HAVE_X11
46  vpDisplayX d(I);
47 #elif defined(VISP_HAVE_GDI)
48  vpDisplayGDI d(I);
49 #elif defined(VISP_HAVE_OPENCV)
50  vpDisplayOpenCV d(I);
51 #else
52  std::cout << "No image viewer is available..." << std::endl;
53 #endif
54  vpDisplay::setTitle(I, "Video reader");
55 
56  unsigned cpt = 1;
58  while (!g.end()) {
61  double t = vpTime::measureTimeMs();
64  g.acquire(I);
67  vpDisplay::displayText(I, 20, 20, "Click to quit", vpColor::red);
68  std::stringstream ss;
69  ss << "Frame: " << cpt ++;
70  vpDisplay::displayText(I, 40, 20, ss.str(), vpColor::red);
72  if (vpDisplay::getClick(I, false))
73  break;
75  vpTime::wait(t, 1000. / g.getFramerate());
77  }
78  std::cout << "End of video was reached" << std::endl;
79  } catch (const vpException &e) {
80  std::cout << e.getMessage() << std::endl;
81  }
82 #else
83  (void)argc;
84  (void)argv;
85  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
86 #endif
87 }
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
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:126
static const vpColor red
Definition: vpColor.h:179
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)
const char * getMessage(void) const
Definition: vpException.cpp:90
unsigned int getHeight() const
Definition: vpImage.h:186
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:244