Visual Servoing Platform  version 3.1.0
tutorial-grabber-v4l2.cpp
1 
2 #include <visp3/core/vpImage.h>
3 #include <visp3/gui/vpDisplayX.h>
4 #include <visp3/sensor/vpV4l2Grabber.h>
5 
12 int main(int argc, const char *argv[])
13 {
14 #ifdef VISP_HAVE_V4L2
15  try {
16  unsigned int opt_device = 0;
17  unsigned int opt_scale = 2; // Default value is 2 in the constructor. Turn
18  // it to 1 to avoid subsampling
19 
20  for (int i = 0; i < argc; i++) {
21  if (std::string(argv[i]) == "--device")
22  opt_device = (unsigned int)atoi(argv[i + 1]);
23  else if (std::string(argv[i]) == "--scale")
24  opt_scale = (unsigned int)atoi(argv[i + 1]);
25  else if (std::string(argv[i]) == "--help") {
26  std::cout << "Usage: " << argv[0]
27  << " [--device <camera device>] [--scale <subsampling "
28  "factor>] [--help]"
29  << std::endl;
30  return 0;
31  }
32  }
33 
35 
36  vpV4l2Grabber g;
37  std::ostringstream device;
38  device << "/dev/video" << opt_device;
39  g.setDevice(device.str());
40  g.setScale(opt_scale);
41  g.open(I);
42 
43  std::cout << "Image size: " << I.getWidth() << " " << I.getHeight() << std::endl;
44 
45 #ifdef VISP_HAVE_X11
46  vpDisplayX d(I);
47 #else
48  std::cout << "No image viewer is available..." << std::endl;
49 #endif
50 
51  while (1) {
52  g.acquire(I);
54  vpDisplay::displayText(I, 10, 10, "A click to quit", vpColor::red);
56  if (vpDisplay::getClick(I, false))
57  break;
58  }
59  } catch (vpException &e) {
60  std::cout << "Catch an exception: " << e << std::endl;
61  }
62 #else
63  (void)argc;
64  (void)argv;
65 #endif
66 }
void acquire(vpImage< unsigned char > &I)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void open(vpImage< unsigned char > &I)
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:151
void setDevice(const std::string &devname)
error that can be emited by ViSP classes.
Definition: vpException.h:71
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:180
static void display(const vpImage< unsigned char > &I)
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
unsigned int getHeight() const
Definition: vpImage.h:178
unsigned int getWidth() const
Definition: vpImage.h:229