Visual Servoing Platform  version 3.6.1 under development (2024-03-18)
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/io/vpImageStorageWorker.h>
7 #include <visp3/sensor/vpPylonFactory.h>
8 
9 void usage(const char *argv[], int error)
10 {
11  std::cout << "SYNOPSIS" << std::endl
12  << " " << argv[0] << " [--device <index>]"
13  << " [--type <device type>]"
14  << " [--seqname <sequence name>]"
15  << " [--record <mode>]"
16  << " [--no-display]"
17  << " [--help] [-h]" << std::endl
18  << std::endl;
19  std::cout << "DESCRIPTION" << std::endl
20  << " --device <index> " << std::endl
21  << " Camera device index in range [0...9]. Set 0 to dial with the first camera," << std::endl
22  << " and 1 to dial with the second camera attached to the computer." << std::endl
23  << " Default: 0." << std::endl
24  << std::endl
25  << " --type <device type>" << std::endl
26  << " Camera device type: GigE or USB" << std::endl
27  << " Default: GigE" << std::endl
28  << std::endl
29  << " --seqname <sequence name>" << std::endl
30  << " Name of the sequence of image to create (ie: /tmp/image%04d.jpg)." << std::endl
31  << " Default: empty." << std::endl
32  << std::endl
33  << " --record <mode>" << std::endl
34  << " Allowed values for mode are:" << std::endl
35  << " 0: record all the captures images (continuous mode)," << std::endl
36  << " 1: record only images selected by a user click (single shot mode)." << std::endl
37  << " Default mode: 0" << std::endl
38  << std::endl
39  << " --no-display" << std::endl
40  << " Disable displaying captured images." << std::endl
41  << " When used and sequence name specified, record mode is internally set to 1 (continuous mode)."
42  << std::endl
43  << std::endl
44  << " --help, -h" << std::endl
45  << " Print this helper message." << std::endl
46  << std::endl;
47  std::cout << "USAGE" << std::endl
48  << " Example to visualize images:" << std::endl
49  << " " << argv[0] << std::endl
50  << std::endl
51  << " Example to visualize images from a second camera GigE:" << std::endl
52  << " " << argv[0] << " --device 1 --type GigE" << std::endl
53  << std::endl
54  << " Examples to record a sequence:" << std::endl
55  << " " << argv[0] << " --seqname I%04d.png" << std::endl
56  << " " << argv[0] << " --seqname folder/I%04d.png --record 0" << std::endl
57  << std::endl
58  << " Examples to record single shot images:\n"
59  << " " << argv[0] << " --seqname I%04d.png --record 1\n"
60  << " " << argv[0] << " --seqname folder/I%04d.png --record 1" << std::endl
61  << std::endl;
62 
63  if (error) {
64  std::cout << "Error" << std::endl
65  << " "
66  << "Unsupported parameter " << argv[error] << std::endl;
67  }
68 }
74 int main(int argc, const char *argv[])
75 {
76 #if defined(VISP_HAVE_PYLON) && defined(VISP_HAVE_THREADS)
77  try {
78  unsigned int opt_device = 0;
79  std::string opt_type("GigE");
80  std::string opt_seqname;
81  int opt_record_mode = 0;
82  bool opt_change_settings = false;
83  bool opt_display = true;
84 
85  for (int i = 1; i < argc; i++) {
86  if (std::string(argv[i]) == "--device") {
87  opt_device = std::atoi(argv[i + 1]);
88  i++;
89  }
90  if (std::string(argv[i]) == "--type") {
91  opt_type = std::string(argv[i + 1]);
92  i++;
93  }
94  else if (std::string(argv[i]) == "--seqname") {
95  opt_seqname = std::string(argv[i + 1]);
96  i++;
97  }
98  else if (std::string(argv[i]) == "--record") {
99  opt_record_mode = std::atoi(argv[i + 1]);
100  i++;
101  }
102  else if (std::string(argv[i]) == "--no-display") {
103  opt_display = false;
104  }
105  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
106  usage(argv, 0);
107  return EXIT_SUCCESS;
108  }
109  else {
110  usage(argv, i);
111  return EXIT_FAILURE;
112  }
113  }
114 
115  if ((!opt_display) && (!opt_seqname.empty())) {
116  opt_record_mode = 0;
117  }
118 
119  std::cout << "Settings : " << (opt_change_settings ? "modified" : "current") << std::endl;
120  std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
121  std::cout << "Display : " << (opt_display ? "enabled" : "disabled") << std::endl;
122 
123  std::string text_record_mode =
124  std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
125 
126  if (!opt_seqname.empty()) {
127  std::cout << text_record_mode << std::endl;
128  std::cout << "Record name: " << opt_seqname << std::endl;
129  }
130 
131  vpImage<vpRGBa> I;
132 
134 
135  vpPylonGrabber *g;
136  if (opt_type == "GigE" || opt_type == "gige") {
138  std::cout << "Opening Basler GigE camera: " << opt_device << std::endl;
139  }
140  else if (opt_type == "USB" || opt_type == "usb") {
142  std::cout << "Opening Basler USB camera: " << opt_device << std::endl;
143  }
144  else {
145  std::cout << "Error: only Basler GigE or USB cameras are supported." << std::endl;
146  return EXIT_SUCCESS;
147  }
148  g->setCameraIndex(opt_device);
149 
150  g->open(I);
151 
152  std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
153 
154  vpDisplay *d = nullptr;
155  if (opt_display) {
156 #if !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
157  std::cout << "No image viewer is available..." << std::endl;
158  opt_display = false;
159 #endif
160  }
161  if (opt_display) {
162 #ifdef VISP_HAVE_X11
163  d = new vpDisplayX(I);
164 #elif defined(VISP_HAVE_GDI)
165  d = new vpDisplayGDI(I);
166 #elif defined(HAVE_OPENCV_HIGHGUI)
167  d = new vpDisplayOpenCV(I);
168 #endif
169  }
170 
171  vpImageQueue<vpRGBa> image_queue(opt_seqname, opt_record_mode);
172  vpImageStorageWorker<vpRGBa> image_storage_worker(std::ref(image_queue));
173  std::thread image_storage_thread(&vpImageStorageWorker<vpRGBa>::run, &image_storage_worker);
174 
175  bool quit = false;
176  while (!quit) {
177  double t = vpTime::measureTimeMs();
178  g->acquire(I);
179 
181 
182  quit = image_queue.record(I);
183 
184  std::stringstream ss;
185  ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
186  vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
187  vpDisplay::flush(I);
188  }
189  image_queue.cancel();
190  image_storage_thread.join();
191 
192  if (d) {
193  delete d;
194  }
195  }
196  catch (const vpException &e) {
197  std::cout << "Catch an exception: " << e << std::endl;
198  }
199 #else
200  (void)argc;
201  (void)argv;
202 #ifndef VISP_HAVE_PYLON
203  std::cout << "Install Basler Pylon SDK, configure and build ViSP again to use this example" << std::endl;
204 #endif
205 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
206  std::cout << "This tutorial should be built with c++11 support" << std::endl;
207 #endif
208 #endif
209 }
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
Class that defines generic functionalities for display.
Definition: vpDisplay.h:173
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:59
unsigned int getWidth() const
Definition: vpImage.h:249
unsigned int getHeight() const
Definition: vpImage.h:184
Factory singleton class to create vpPylonGrabber subclass instances.
@ BASLER_GIGE
Basler GigE camera.
@ BASLER_USB
Basler USB camera.
static vpPylonFactory & instance()
Get the vpPylonFactory singleton.
vpPylonGrabber * createPylonGrabber(DeviceClass dev_class)
Create an object of vpPylonGrabber.
virtual void acquire(vpImage< unsigned char > &I)=0
virtual void open(vpImage< unsigned char > &I)=0
virtual void setCameraIndex(unsigned int index)=0
VISP_EXPORT double measureTimeMs()