Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
tutorial-grabber-v4l2.cpp
1 
2 #include <visp3/core/vpConfig.h>
3 #include <visp3/core/vpImage.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/gui/vpDisplayX.h>
6 #include <visp3/io/vpImageStorageWorker.h>
7 #include <visp3/sensor/vpV4l2Grabber.h>
8 
9 #define USE_COLOR // Comment to acquire gray level images
10 
11 void usage(const char *argv[], int error)
12 {
13  std::cout << "SYNOPSIS" << std::endl
14  << " " << argv[0] << " [--device <index>]"
15  << " [--scale <subsampling factor>]"
16  << " [--seqname <sequence name>]"
17  << " [--record <mode>]"
18  << " [--no-display]"
19  << " [--help] [-h]" << std::endl
20  << std::endl;
21  std::cout << "DESCRIPTION" << std::endl
22  << " --device <index> " << std::endl
23  << " Camera device index. Set 0 to dial with the first camera," << std::endl
24  << " and 1 to dial with the second camera attached to the computer." << std::endl
25  << " Default: 0 to consider /dev/video0 device." << std::endl
26  << std::endl
27  << " --scale <subsampling factor>" << std::endl
28  << " Subsampling factor applied to the images captured by the device." << std::endl
29  << " Default: 1" << std::endl
30  << std::endl
31  << " --seqname <sequence name>" << std::endl
32  << " Name of the sequence of image to create (ie: /tmp/image%04d.jpg)." << std::endl
33  << " Default: empty." << std::endl
34  << std::endl
35  << " --record <mode>" << std::endl
36  << " Allowed values for mode are:" << std::endl
37  << " 0: record all the captures images (continuous mode)," << std::endl
38  << " 1: record only images selected by a user click (single shot mode)." << std::endl
39  << " Default mode: 0" << std::endl
40  << std::endl
41  << " --no-display" << std::endl
42  << " Disable displaying captured images." << std::endl
43  << " When used and sequence name specified, record mode is internally set to 1 (continuous mode)."
44  << std::endl
45  << std::endl
46  << " --help, -h" << std::endl
47  << " Print this helper message." << std::endl
48  << std::endl;
49  std::cout << "USAGE" << std::endl
50  << " Example to visualize images:" << std::endl
51  << " " << argv[0] << std::endl
52  << std::endl
53  << " Example to visualize images from a second camera:" << std::endl
54  << " " << argv[0] << " --device 1" << std::endl
55  << std::endl
56  << " Examples to record a sequence:" << std::endl
57  << " " << argv[0] << " --seqname I%04d.png" << std::endl
58  << " " << argv[0] << " --seqname folder/I%04d.png --record 0" << std::endl
59  << std::endl
60  << " Examples to record single shot images:\n"
61  << " " << argv[0] << " --seqname I%04d.png --record 1\n"
62  << " " << argv[0] << " --seqname folder/I%04d.png --record 1" << std::endl
63  << std::endl;
64 
65  if (error) {
66  std::cout << "Error" << std::endl
67  << " "
68  << "Unsupported parameter " << argv[error] << std::endl;
69  }
70 }
71 
78 int main(int argc, const char *argv[])
79 {
80 #if defined(VISP_HAVE_V4L2) && defined(VISP_HAVE_THREADS)
81 #ifdef ENABLE_VISP_NAMESPACE
82  using namespace VISP_NAMESPACE_NAME;
83 #endif
84  try {
85  int opt_device = 0;
86  unsigned int opt_scale = 1; // Default value is 2 in the constructor. Turn
87  // it to 1 to avoid subsampling
88  std::string opt_seqname;
89  int opt_record_mode = 0;
90  bool opt_display = true;
91 
92  for (int i = 1; i < argc; i++) {
93  if (std::string(argv[i]) == "--device") {
94  opt_device = std::atoi(argv[i + 1]);
95  i++;
96  }
97  else if (std::string(argv[i]) == "--scale") {
98  opt_scale = (unsigned int)atoi(argv[i + 1]);
99  i++;
100  }
101  else if (std::string(argv[i]) == "--seqname") {
102  opt_seqname = std::string(argv[i + 1]);
103  i++;
104  }
105  else if (std::string(argv[i]) == "--record") {
106  opt_record_mode = std::atoi(argv[i + 1]);
107  i++;
108  }
109  else if (std::string(argv[i]) == "--no-display") {
110  opt_display = false;
111  }
112  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
113  usage(argv, 0);
114  return EXIT_SUCCESS;
115  }
116  else {
117  usage(argv, i);
118  return EXIT_FAILURE;
119  }
120  }
121 
122  if ((!opt_display) && (!opt_seqname.empty())) {
123  opt_record_mode = 0;
124  }
125  std::cout << "Use device : " << opt_device << std::endl;
126  std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
127  std::cout << "Display : " << (opt_display ? "enabled" : "disabled") << std::endl;
128 
129  std::string text_record_mode =
130  std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
131 
132  if (!opt_seqname.empty()) {
133  std::cout << text_record_mode << std::endl;
134  std::cout << "Record name: " << opt_seqname << std::endl;
135  }
136 
137 #ifdef USE_COLOR
138  vpImage<vpRGBa> I; // To acquire color images
139 #else
140  vpImage<unsigned char> I; // To acquire gray images
141 #endif
142 
143  vpV4l2Grabber g;
144  std::ostringstream device;
145  device << "/dev/video" << opt_device;
146  g.setDevice(device.str());
147  g.setScale(opt_scale);
148  g.open(I);
149 
150  std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
151 
152  vpDisplay *d = nullptr;
153  if (opt_display) {
154 #if !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_OPENCV))
155  std::cout << "No image viewer is available..." << std::endl;
156  opt_display = false;
157 #endif
158  }
159  if (opt_display) {
160 #ifdef VISP_HAVE_X11
161  d = new vpDisplayX(I);
162 #elif defined(HAVE_OPENCV_HIGHGUI)
163  d = new vpDisplayOpenCV(I);
164 #endif
165  }
166 
167 #ifdef USE_COLOR
168  vpImageQueue<vpRGBa> image_queue(opt_seqname, opt_record_mode);
169  vpImageStorageWorker<vpRGBa> image_storage_worker(std::ref(image_queue));
170  std::thread image_storage_thread(&vpImageStorageWorker<vpRGBa>::run, &image_storage_worker);
171 #else
172  vpImageQueue<unsigned char> image_queue(opt_seqname, opt_record_mode);
173  vpImageStorageWorker<unsigned char> image_storage_worker(std::ref(image_queue));
174  std::thread image_storage_thread(&vpImageStorageWorker<unsigned char>::run, &image_storage_worker);
175 #endif
176 
177  bool quit = false;
178  while (!quit) {
179  double t = vpTime::measureTimeMs();
180  g.acquire(I);
181 
183 
184  quit = image_queue.record(I);
185 
186  std::stringstream ss;
187  ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
188  vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
189  vpDisplay::flush(I);
190  }
191  image_queue.cancel();
192  image_storage_thread.join();
193 
194  if (d) {
195  delete d;
196  }
197  }
198  catch (const vpException &e) {
199  std::cout << "Catch an exception: " << e << std::endl;
200  }
201 #else
202  (void)argc;
203  (void)argv;
204 #ifndef VISP_HAVE_V4L2
205  std::cout << "Install Video 4 Linux 2 (v4l2), configure and build ViSP again to use this example" << std::endl;
206 #endif
207 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
208  std::cout << "This tutorial should be built with c++11 support" << std::endl;
209 #endif
210 
211 #endif
212 }
static const vpColor red
Definition: vpColor.h:217
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
void open(vpImage< unsigned char > &I)
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
void setDevice(const std::string &devname)
void acquire(vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()