Visual Servoing Platform  version 3.4.0
tutorial-grabber-basler-pylon.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/sensor/vpPylonFactory.h>
7 #include <visp3/io/vpImageStorageWorker.h>
8 
14 int main(int argc, const char *argv[])
15 {
16 #if defined(VISP_HAVE_PYLON) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
17  try {
18  unsigned int opt_camera = 0;
19  std::string opt_device("GigE");
20  std::string opt_seqname;
21  int opt_record_mode = 0;
22  bool opt_change_settings = false;
23 
24  for (int i = 0; i < argc; i++) {
25  if (std::string(argv[i]) == "--camera")
26  opt_camera = (unsigned int)atoi(argv[i + 1]);
27  else if (std::string(argv[i]) == "--device")
28  opt_device = std::string(argv[i + 1]);
29  else if (std::string(argv[i]) == "--seqname")
30  opt_seqname = std::string(argv[i + 1]);
31  else if (std::string(argv[i]) == "--record")
32  opt_record_mode = std::atoi(argv[i + 1]);
33  else if (std::string(argv[i]) == "--change_settings")
34  opt_change_settings = true;
35  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
36  std::cout << "\nUsage: " << argv[0]
37  << " [--camera <0...9> (default: 0)] [--device <\"GigE\"|\"USB\" (default: GigE)>]"
38  << " [--seqname <sequence name (default: empty)>] [--record <0: continuous | 1: single shot (default: 0)>]"
39  << " [--change_settings] [--help] [-h]\n"
40  << "\nExample to visualize images:\n"
41  << " " << argv[0] << " \n"
42  << " " << argv[0] << " --device GigE --camera 0\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 << "Settings : " << (opt_change_settings ? "modified" : "current") << 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 
67 
68  vpPylonGrabber *g;
69  if (opt_device == "GigE" || opt_device == "gige") {
71  std::cout << "Opening Basler GigE camera: " << opt_camera << std::endl;
72  } else if (opt_device == "USB" || opt_device == "usb") {
74  std::cout << "Opening Basler USB camera: " << opt_camera << std::endl;
75  } else {
76  std::cout << "Error: only Basler GigE or USB cameras are supported." << std::endl;
77  return EXIT_SUCCESS;
78  }
79  g->setCameraIndex(opt_camera);
80 
81  g->open(I);
82 
83  std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
84 
85 #ifdef VISP_HAVE_X11
86  vpDisplayX d(I);
87 #elif defined(VISP_HAVE_GDI)
88  vpDisplayGDI d(I);
89 #elif defined(VISP_HAVE_OPENCV)
90  vpDisplayOpenCV d(I);
91 #else
92  std::cout << "No image viewer is available..." << std::endl;
93 #endif
94 
95  vpImageQueue<unsigned char> image_queue(opt_seqname, opt_record_mode);
96  vpImageStorageWorker<unsigned char> image_storage_worker(std::ref(image_queue));
97  std::thread image_storage_thread(&vpImageStorageWorker<unsigned char>::run, &image_storage_worker);
98 
99  bool quit = false;
100  while (! quit) {
101  double t = vpTime::measureTimeMs();
102  g->acquire(I);
103 
105 
106  quit = image_queue.record(I);
107 
108  std::stringstream ss;
109  ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
110  vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
111  vpDisplay::flush(I);
112  }
113  image_queue.cancel();
114  image_storage_thread.join();
115  } catch (const vpException &e) {
116  std::cout << "Catch an exception: " << e << std::endl;
117  }
118 #else
119  (void) argc;
120  (void) argv;
121 #ifndef VISP_HAVE_PYLON
122  std::cout << "Install Basler Pylon SDK, configure and build ViSP again to use this example" << std::endl;
123 #endif
124 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
125  std::cout << "This turorial should be built with c++11 support" << std::endl;
126 #endif
127 #endif
128 }
virtual void open(vpImage< unsigned char > &I)=0
unsigned int getWidth() const
Definition: vpImage.h:246
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
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
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
Basler GigE camera.
Factory singleton class to create vpPylonGrabber subclass instances.
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
virtual void acquire(vpImage< unsigned char > &I)=0
static vpPylonFactory & instance()
Get the vpPylonFactory singleton.
Basler USB camera.
virtual void setCameraIndex(unsigned int index)=0
unsigned int getHeight() const
Definition: vpImage.h:188
vpPylonGrabber * createPylonGrabber(DeviceClass dev_class)
Create an object of vpPylonGrabber.