Visual Servoing Platform  version 3.4.0
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 #include <visp3/io/vpImageStorageWorker.h>
7 
8 //#define USE_COLOR // Comment to acquire gray level images
9 
16 int main(int argc, const char *argv[])
17 {
18 #if defined(VISP_HAVE_V4L2) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
19  try {
20  int opt_device = 0;
21  unsigned int opt_scale = 1; // Default value is 2 in the constructor. Turn
22  // it to 1 to avoid subsampling
23  std::string opt_seqname;
24  int opt_record_mode = 0;
25 
26  for (int i = 0; i < argc; i++) {
27  if (std::string(argv[i]) == "--camera_device")
28  opt_device = std::atoi(argv[i + 1]);
29  else if (std::string(argv[i]) == "--scale")
30  opt_scale = (unsigned int)atoi(argv[i + 1]);
31  else if (std::string(argv[i]) == "--seqname")
32  opt_seqname = std::string(argv[i + 1]);
33  else if (std::string(argv[i]) == "--record")
34  opt_record_mode = std::atoi(argv[i + 1]);
35  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
36  std::cout << "\nUsage: " << argv[0]
37  << " [--camera_device <camera device (default: 0)>] [--scale <subsampling factor (default: 1)>]"
38  " [--seqname <sequence name (default: empty>] [--record <0: continuous | 1: single shot (default: 0)>]"
39  " [--help] [-h]\n"
40  << "\nExample to visualize images:\n"
41  << " " << argv[0] << "\n"
42  << "\nExample to visualize images from a second camera:\n"
43  << " " << argv[0] << " --camera_device 1\n"
44  << "\nExamples to record a sequence:\n"
45  << " " << argv[0] << " --seqname I%04d.png \n"
46  << " " << argv[0] << " --seqname folder/I%04d.png --record 0\n"
47  << "\nExamples to record single shot images:\n"
48  << " " << argv[0] << " --seqname I%04d.png --record 1\n"
49  << " " << argv[0] << " --seqname folder/I%04d.png --record 1\n"
50  << std::endl;
51  return 0;
52  }
53  }
54 
55  std::cout << "Use device : " << opt_device << std::endl;
56  std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
57 
58  std::string text_record_mode = std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
59 
60  if (! opt_seqname.empty()) {
61  std::cout << text_record_mode << std::endl;
62  std::cout << "Record name: " << opt_seqname << std::endl;
63  }
64 
65 #ifdef USE_COLOR
66  vpImage<vpRGBa> I; // To acquire color images
67 #else
68  vpImage<unsigned char> I; // To acquire gray images
69 #endif
70 
71  vpV4l2Grabber g;
72  std::ostringstream device;
73  device << "/dev/video" << opt_device;
74  g.setDevice(device.str());
75  g.setScale(opt_scale);
76  g.open(I);
77 
78  std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
79 
80 #ifdef VISP_HAVE_X11
81  vpDisplayX d(I);
82 #elif defined(VISP_HAVE_OPENCV)
83  vpDisplayOpenCV d(I);
84 #else
85  std::cout << "No image viewer is available..." << std::endl;
86 #endif
87 
88 #ifdef USE_COLOR
89  vpImageQueue<vpRGBa> image_queue(opt_seqname, opt_record_mode);
90  vpImageStorageWorker<vpRGBa> image_storage_worker(std::ref(image_queue));
91  std::thread image_storage_thread(&vpImageStorageWorker<vpRGBa>::run, &image_storage_worker);
92 #else
93  vpImageQueue<unsigned char> image_queue(opt_seqname, opt_record_mode);
94  vpImageStorageWorker<unsigned char> image_storage_worker(std::ref(image_queue));
95  std::thread image_storage_thread(&vpImageStorageWorker<unsigned char>::run, &image_storage_worker);
96 #endif
97 
98  bool quit = false;
99  while (! quit) {
100  double t = vpTime::measureTimeMs();
101  g.acquire(I);
102 
104 
105  quit = image_queue.record(I);
106 
107  std::stringstream ss;
108  ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
109  vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
110  vpDisplay::flush(I);
111  }
112  image_queue.cancel();
113  image_storage_thread.join();
114  } catch (const vpException &e) {
115  std::cout << "Catch an exception: " << e << std::endl;
116  }
117 #else
118  (void) argc;
119  (void) argv;
120 #ifndef VISP_HAVE_V4L2
121  std::cout << "Install Video 4 Linux 2 (v4l2), configure and build ViSP again to use this example" << std::endl;
122 #endif
123 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
124  std::cout << "This turorial should be built with c++11 support" << std::endl;
125 #endif
126 #endif
127 }
void acquire(vpImage< unsigned char > &I)
void open(vpImage< unsigned char > &I)
unsigned int getWidth() const
Definition: vpImage.h:246
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:217
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:188