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