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