Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
tutorial-grabber-v4l2.cpp
1 
2 #include <visp3/core/vpImage.h>
3 #include <visp3/gui/vpDisplayX.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/sensor/vpV4l2Grabber.h>
6 
7 #include "record_helper.h"
8 
15 int main(int argc, const char *argv[])
16 {
17 #ifdef VISP_HAVE_V4L2
18  try {
19  int opt_device = 0;
20  unsigned int opt_scale = 1; // Default value is 2 in the constructor. Turn
21  // it to 1 to avoid subsampling
22  std::string opt_seqname;
23  int opt_record_mode = 0;
24 
25  for (int i = 0; i < argc; i++) {
26  if (std::string(argv[i]) == "--camera_device")
27  opt_device = std::atoi(argv[i + 1]);
28  else if (std::string(argv[i]) == "--scale")
29  opt_scale = (unsigned int)atoi(argv[i + 1]);
30  else if (std::string(argv[i]) == "--seqname")
31  opt_seqname = std::string(argv[i + 1]);
32  else if (std::string(argv[i]) == "--record")
33  opt_record_mode = std::atoi(argv[i + 1]);
34  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
35  std::cout << "\nUsage: " << argv[0]
36  << " [--camera_device <camera device (default: 0)>] [--scale <subsampling factor (default: 1)>]"
37  " [--seqname <sequence name (default: empty>] [--record <0: continuous | 1: single shot (default: 0)>]"
38  " [--help] [-h]\n"
39  << "\nExample to visualize images:\n"
40  << " " << argv[0] << "\n"
41  << "\nExample to visualize images from a second camera:\n"
42  << " " << argv[0] << " --camera_device 1\n"
43  << "\nExamples to record a sequence:\n"
44  << " " << argv[0] << " --seqname I%04d.png \n"
45  << " " << argv[0] << " --seqname folder/I%04d.png --record 0\n"
46  << "\nExamples to record single shot images:\n"
47  << " " << argv[0] << " --seqname I%04d.png --record 1\n"
48  << " " << argv[0] << " --seqname folder/I%04d.png --record 1\n"
49  << std::endl;
50  return 0;
51  }
52  }
53 
54  std::cout << "Use device : " << opt_device << std::endl;
55  std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
56 
57  std::string text_record_mode = std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
58 
59  if (! opt_seqname.empty()) {
60  std::cout << text_record_mode << std::endl;
61  std::cout << "Record name: " << opt_seqname << std::endl;
62  }
63 
65 
66  vpV4l2Grabber g;
67  std::ostringstream device;
68  device << "/dev/video" << opt_device;
69  g.setDevice(device.str());
70  g.setScale(opt_scale);
71  g.open(I);
72 
73  std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
74 
75 #ifdef VISP_HAVE_X11
76  vpDisplayX d(I);
77 #elif defined(VISP_HAVE_OPENCV)
78  vpDisplayOpenCV d(I);
79 #else
80  std::cout << "No image viewer is available..." << std::endl;
81 #endif
82 
83  bool quit = false;
84  while (! quit) {
85  double t = vpTime::measureTimeMs();
86  g.acquire(I);
87 
89 
90  quit = record_helper(opt_seqname, opt_record_mode, I);
91 
92  std::stringstream ss;
93  ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
94  vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
96  }
97  } catch (const vpException &e) {
98  std::cout << "Catch an exception: " << e << std::endl;
99  }
100 #else
101  (void) argc;
102  (void) argv;
103  std::cout << "Install Video 4 Linux 2 (v4l2), configure and build ViSP again to use this example" << std::endl;
104 #endif
105 }
void acquire(vpImage< unsigned char > &I)
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:150
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)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:126
static const vpColor red
Definition: vpColor.h:179
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 setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
unsigned int getHeight() const
Definition: vpImage.h:186
unsigned int getWidth() const
Definition: vpImage.h:244