Visual Servoing Platform  version 3.6.1 under development (2024-04-25)
tutorial-grabber-ids-ueye.cpp
1 
2 #include <visp3/core/vpImage.h>
3 #include <visp3/gui/vpDisplayGDI.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/gui/vpDisplayX.h>
6 #include <visp3/io/vpImageStorageWorker.h>
7 #include <visp3/sensor/vpUeyeGrabber.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  << " [--config-file <filename.ini>]"
16  << " [--fps <auto|fps value like 6|15|30|60>]"
17  << " [--gain <auto|value in 0 - 100>]"
18  << " [--shutter <auto|exposure value in ms>]"
19  << " [--subsample <factor>]"
20  << " [--white-balance <value>]"
21  << " [--color-mode <mode>]"
22  << " [--seqname <sequence name>]"
23  << " [--record <mode>]"
24  << " [--no-display]"
25  << " [--verbose] [-v]"
26  << " [--help] [-h]" << std::endl
27  << std::endl;
28  std::cout << "DESCRIPTION" << std::endl
29  << " --device <index>" << std::endl
30  << " Camera device index. Set 0 to dial with the first camera," << std::endl
31  << " and 1 to dial with the second camera attached to the computer." << std::endl
32  << " Default: 0" << std::endl
33  << std::endl
34  << " --config-file <filename.ini>" << std::endl
35  << " Camera config file." << std::endl
36  << " Default: empty." << std::endl
37  << std::endl
38  << " --fps <auto|fps value like 6|15|30|60>" << std::endl
39  << " \"Auto\" or a frames per second value." << std::endl
40  << " Default: current setting." << std::endl
41  << std::endl
42  << " --gain <auto|value in 0 - 100>" << std::endl
43  << " \"Auto\" or manual gain with a value in 0 - 100." << std::endl
44  << " Default: current setting." << std::endl
45  << std::endl
46  << " --shutter <auto|manu>" << std::endl
47  << " \"Auto\" or manual shutter." << std::endl
48  << " Default: current setting." << std::endl
49  << std::endl
50  << " --subsample <factor>" << std::endl
51  << " Subsample factor to reduce image size alog rows and columns." << std::endl
52  << " Default: 1." << std::endl
53  << std::endl
54  << " --white-balance <value>" << std::endl
55  << " Possible values are 0 (disabled) or 1 (enabled)." << std::endl
56  << " Default: current setting." << std::endl
57  << std::endl
58  << " --color-mode <mode>" << std::endl
59  << " Possible modes are: mono8, rgb24, rgb32, bayer8." << std::endl
60  << " Default: current setting." << std::endl
61  << std::endl
62  << " --seqname <sequence name>" << std::endl
63  << " Name of the sequence of image to create (ie: /tmp/image%04d.jpg)." << std::endl
64  << " Default: empty." << std::endl
65  << std::endl
66  << " --record <mode>" << std::endl
67  << " Allowed values for mode are:" << std::endl
68  << " 0: record all the captures images (continuous mode)," << std::endl
69  << " 1: record only images selected by a user click (single shot mode)." << std::endl
70  << " Default mode: 0" << std::endl
71  << std::endl
72  << " --no-display" << std::endl
73  << " Disable displaying captured images." << std::endl
74  << " When used and sequence name specified, record mode is internally set to 1 (continuous mode)."
75  << std::endl
76  << std::endl
77  << " --verbose, -v" << std::endl
78  << " Enable extra printings." << std::endl
79  << std::endl
80  << " --help, -h" << std::endl
81  << " Print this helper message." << std::endl
82  << std::endl;
83  std::cout << "USAGE" << std::endl
84  << " Example to visualize images:" << std::endl
85  << " " << argv[0] << std::endl
86  << std::endl
87  << " Examples to record a sequence of images:" << std::endl
88  << " " << argv[0] << " --seqname I%04d.png" << std::endl
89  << " " << argv[0] << " --seqname folder/I%04d.png --record 0" << std::endl
90  << std::endl
91  << " Examples to record single shot images:\n"
92  << " " << argv[0] << " --seqname I%04d.png --record 1\n"
93  << " " << argv[0] << " --seqname folder/I%04d.png --record 1" << std::endl
94  << std::endl;
95 
96  if (error) {
97  std::cout << "Error" << std::endl
98  << " "
99  << "Unsupported parameter " << argv[error] << std::endl;
100  }
101 }
102 
108 int main(int argc, const char *argv[])
109 {
110 #if defined(VISP_HAVE_UEYE) && defined(VISP_HAVE_THREADS)
111  try {
112  unsigned int opt_device = 0;
113  std::string opt_seqname;
114  int opt_record_mode = 0;
115  std::string opt_config_file = "";
116  std::string opt_fps = "";
117  std::string opt_gain = "";
118  std::string opt_shutter = "";
119  std::string opt_color_mode = "";
120  int opt_white_balance = -1;
121  int opt_subsample = 1;
122  bool opt_verbose = false;
123  bool opt_display = true;
124 
125  for (int i = 1; i < argc; i++) {
126  if (std::string(argv[i]) == "--device") {
127  opt_device = static_cast<unsigned int>(std::atoi(argv[i + 1]));
128  i++;
129  }
130  else if (std::string(argv[i]) == "--config-file") {
131  opt_config_file = std::string(argv[i + 1]);
132  i++;
133  }
134  else if (std::string(argv[i]) == "--fps") {
135  opt_fps = std::string(argv[i + 1]);
136  i++;
137  }
138  else if (std::string(argv[i]) == "--gain") {
139  opt_gain = std::string(argv[i + 1]);
140  i++;
141  }
142  else if (std::string(argv[i]) == "--shutter") {
143  opt_shutter = std::string(argv[i + 1]);
144  i++;
145  }
146  else if (std::string(argv[i]) == "--subsample") {
147  opt_subsample = std::atoi(argv[i + 1]);
148  i++;
149  }
150  else if (std::string(argv[i]) == "--white-balance") {
151  opt_white_balance = std::atoi(argv[i + 1]);
152  i++;
153  }
154  else if (std::string(argv[i]) == "--color-mode") {
155  opt_color_mode = std::string(argv[i + 1]);
156  i++;
157  }
158  else if (std::string(argv[i]) == "--seqname") {
159  opt_seqname = std::string(argv[i + 1]);
160  i++;
161  }
162  else if (std::string(argv[i]) == "--record") {
163  opt_record_mode = std::atoi(argv[i + 1]);
164  i++;
165  }
166  else if (std::string(argv[i]) == "--verbose" || std::string(argv[i]) == "-v") {
167  opt_verbose = true;
168  }
169  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
170  usage(argv, 0);
171  return EXIT_SUCCESS;
172  }
173  else {
174  usage(argv, i);
175  return EXIT_FAILURE;
176  }
177  }
178 
179  if ((!opt_display) && (!opt_seqname.empty())) {
180  opt_record_mode = 0;
181  }
182 
184 #ifdef USE_COLOR
185  vpImage<vpRGBa> I; // To acquire color images
186 #else
187  vpImage<unsigned char> I; // To acquire gray images
188 #endif
190 
192  vpUeyeGrabber g;
193 
194  // Get info on connected cameras
195  std::vector<unsigned int> cam_ids = g.getCameraIDList();
196  std::vector<std::string> cam_models = g.getCameraModelList();
197  std::vector<std::string> cam_serials = g.getCameraSerialNumberList();
198 
199  if (!cam_ids.size()) {
200  std::cout << "No camera detected. Plug a camera and try again..." << std::endl;
201  return EXIT_FAILURE;
202  }
203  std::cout << "Found " << cam_ids.size() << " cameras :" << std::endl;
204  for (unsigned int i = 0; i < cam_ids.size(); i++) {
205  std::cout << (opt_device == i ? " * Camera " : " Camera ") << i << " - ID: " << cam_ids[i]
206  << " Model: " << cam_models[i] << " S/N: " << cam_serials[i] << std::endl;
207  }
209 
211  if (!g.setActiveCamera(opt_device)) {
212  std::cout << "Unable to select camera " << opt_device << std::endl;
213  return EXIT_FAILURE;
214  };
216 
217  std::cout << "Active camera is Model " << g.getActiveCameraModel()
218  << " with S/N: " << g.getActiveCameraSerialNumber() << std::endl;
219 
221  g.open(I);
223 
224  if (!opt_config_file.empty()) {
226  g.loadParameters(opt_config_file);
228  // Since loaded parameters may affect image size, rescale image in case of
230  I.resize(g.getFrameHeight(), g.getFrameWidth());
232  }
233 
234  if (opt_subsample > 1) {
235  std::cout << "Subsampling factor: " << opt_subsample << std::endl;
236  g.setSubsampling(opt_subsample);
237  // Since subsampling may affect image size, rescale image in case of
238  I.resize(g.getFrameHeight(), g.getFrameWidth());
239  }
240 
241  if (!opt_gain.empty()) {
242  if (opt_gain == "auto") {
243  std::cout << "Auto gain : " << (g.setGain(true) ? "enabled" : "N/A") << std::endl;
244  }
245  else {
246  std::cout << "Manual gain : "
247  << (g.setGain(false, std::atoi(opt_gain.c_str())) ? (std::string(opt_gain) + " %") : "N/A")
248  << std::endl;
249  }
250  }
251  if (!opt_shutter.empty()) {
252  if (opt_shutter == "auto") {
253  std::cout << "Auto shutter : " << (g.setExposure(true) ? "enabled" : "N/A") << std::endl;
254  }
255  else {
256  std::cout << "Manual shutter : "
257  << (g.setExposure(false, std::atof(opt_shutter.c_str())) ? (std::string(opt_shutter) + " ms") : "N/A")
258  << std::endl;
259  }
260  }
261 
262  if (opt_white_balance > 0) {
263  bool wb = (opt_white_balance ? true : false);
264  std::cout << "Subsampling factor: " << opt_subsample << std::endl;
265  std::cout << "White balance : " << (wb ? "auto" : "disabled") << std::endl;
266  g.setWhiteBalance(wb);
267  }
268 
269  if (!opt_color_mode.empty()) {
270  if (g.setColorMode(opt_color_mode)) {
271  std::cout << "Color mode : " << opt_color_mode << std::endl;
272  }
273  }
274 
275  if (!opt_fps.empty()) {
276  if (opt_fps == "auto") {
277  std::cout << "Auto framerate : " << (g.setFrameRate(true) ? "enabled" : "N/A") << std::endl;
278  }
279  else {
280  std::cout << "Manual framerate : "
281  << (g.setFrameRate(false, std::atof(opt_fps.c_str())) ? (std::string(opt_fps) + " Hz") : "N/A")
282  << std::endl;
283  }
284  }
285 
286  std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
287  std::cout << "Display : " << (opt_display ? "enabled" : "disabled") << std::endl;
288 
289  std::string text_record_mode =
290  std::string("Record mode : ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
291 
292  if (!opt_seqname.empty()) {
293  std::cout << text_record_mode << std::endl;
294  std::cout << "Record name : " << opt_seqname << std::endl;
295  }
296 
297  std::cout << "Config file : " << (opt_config_file.empty() ? "empty" : opt_config_file) << std::endl;
298  std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
299 
300  vpDisplay *d = nullptr;
301  if (opt_display) {
302 #if !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
303  std::cout << "No image viewer is available..." << std::endl;
304  opt_display = false;
305 #endif
306  }
307  if (opt_display) {
308 #ifdef VISP_HAVE_X11
309  d = new vpDisplayX;
310 #elif defined(VISP_HAVE_GDI)
311  d = new vpDisplayGDI;
312 #elif defined(HAVE_OPENCV_HIGHGUI)
313  d = new vpDisplayOpenCV;
314 #endif
316  d->init(I);
317  }
318 
319 #ifdef USE_COLOR
320  vpImageQueue<vpRGBa> image_queue(opt_seqname, opt_record_mode);
321  vpImageStorageWorker<vpRGBa> image_storage_worker(std::ref(image_queue));
322  std::thread image_storage_thread(&vpImageStorageWorker<vpRGBa>::run, &image_storage_worker);
323 #else
324  vpImageQueue<unsigned char> image_queue(opt_seqname, opt_record_mode);
325  vpImageStorageWorker<unsigned char> image_storage_worker(std::ref(image_queue));
326  std::thread image_storage_thread(&vpImageStorageWorker<unsigned char>::run, &image_storage_worker);
327 #endif
328 
329  bool quit = false;
330  double timestamp_camera = 0, timestamp_camera_prev = 0;
331  std::string timestamp_system;
332  while (!quit) {
333  g.acquire(I, &timestamp_camera, &timestamp_system);
334  double fps = g.getFramerate();
335 
337 
338  quit = image_queue.record(I, &timestamp_system);
339 
340  if (opt_verbose) {
341  std::cout << "System timestamp: " << timestamp_system << std::endl;
342  std::cout << "Camera timestamp diff: " << timestamp_camera - timestamp_camera_prev << std::endl;
343  timestamp_camera_prev = timestamp_camera;
344  }
345  vpDisplay::displayText(I, static_cast<int>(I.getHeight() - 40 * vpDisplay::getDownScalingFactor(I)),
346  static_cast<int>(10 * vpDisplay::getDownScalingFactor(I)), timestamp_system, vpColor::red);
347  {
348  std::stringstream ss;
349  ss << "Camera framerate: " << fps;
350  vpDisplay::displayText(I, static_cast<int>(I.getHeight() - 60 * vpDisplay::getDownScalingFactor(I)),
351  static_cast<int>(10 * vpDisplay::getDownScalingFactor(I)), ss.str(), vpColor::red);
352  }
353 
354  vpDisplay::flush(I);
355  }
356  image_queue.cancel();
357  image_storage_thread.join();
358 
359  if (d) {
360  delete d;
361  }
362  }
363  catch (const vpException &e) {
364  std::cout << "Catch an exception: " << e << std::endl;
365  }
366 #else
367  (void)argc;
368  (void)argv;
369 #ifndef VISP_HAVE_UEYE
370  std::cout << "Install IDS uEye SDK, configure and build ViSP again to use this example" << std::endl;
371 #endif
372 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
373  std::cout << "This tutorial should be built with c++11 support" << std::endl;
374 #endif
375 #endif
376 }
static const vpColor red
Definition: vpColor.h:211
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="") vp_override
Class that defines generic functionalities for display.
Definition: vpDisplay.h:173
virtual void setDownScalingFactor(unsigned int scale)
Definition: vpDisplay.cpp:227
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
@ SCALE_AUTO
Definition: vpDisplay.h:179
unsigned int getDownScalingFactor()
Definition: vpDisplay.h:231
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:59
unsigned int getWidth() const
Definition: vpImage.h:245
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:783
unsigned int getHeight() const
Definition: vpImage.h:184
void open(vpImage< unsigned char > &I)
std::vector< std::string > getCameraSerialNumberList() const
bool setExposure(bool auto_exposure, double exposure_ms=-1)
bool setFrameRate(bool auto_frame_rate, double manual_frame_rate_hz=-1)
void setWhiteBalance(bool auto_wb)
bool setGain(bool auto_gain, int master_gain=-1, bool gain_boost=false)
void acquire(vpImage< unsigned char > &I, double *timestamp_camera=nullptr, std::string *timestamp_system=nullptr)
double getFramerate() const
void loadParameters(const std::string &filename)
std::vector< std::string > getCameraModelList() const
std::vector< unsigned int > getCameraIDList() const
void setSubsampling(int factor)
unsigned int getFrameHeight() const
unsigned int getFrameWidth() const
bool setActiveCamera(unsigned int cam_index)
bool setColorMode(const std::string &color_mode)
std::string getActiveCameraModel() const
std::string getActiveCameraSerialNumber() const