Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
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 
8 #include "record_helper.h"
9 
15 int main(int argc, const char *argv[])
16 {
17 #ifdef VISP_HAVE_PYLON
18  try {
19  unsigned int opt_camera = 0;
20  std::string opt_device("GigE");
21  std::string opt_seqname;
22  int opt_record_mode = 0;
23  bool opt_change_settings = false;
24 
25  for (int i = 0; i < argc; i++) {
26  if (std::string(argv[i]) == "--camera")
27  opt_camera = (unsigned int)atoi(argv[i + 1]);
28  else if (std::string(argv[i]) == "--device")
29  opt_device = std::string(argv[i + 1]);
30  else if (std::string(argv[i]) == "--seqname")
31  opt_seqname = std::string(argv[i + 1]);
32  else if (std::string(argv[i]) == "--record")
33  opt_record_mode = std::atoi(argv[i + 1]);
34  else if (std::string(argv[i]) == "--change_settings")
35  opt_change_settings = true;
36  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
37  std::cout << "\nUsage: " << argv[0]
38  << " [--camera <0...9> (default: 0)] [--device <\"GigE\"|\"USB\" (default: GigE)>]"
39  << " [--seqname <sequence name (default: empty>] [--record <0: continuous | 1: single shot (default: 0)>]"
40  << " [--change_settings] [--help] [-h]\n"
41  << "\nExample to visualize images:\n"
42  << " " << argv[0] << " \n"
43  << " " << argv[0] << " --device GigE --camera 0\n"
44  << "\nExamples to record a sequence:\n"
45  << " " << argv[0] << " --seqname I%04d.png \n"
46  << " " << argv[0] << " --seqname folder/I%04d.png --record 0\n"
47  << "\nExamples to record single shot images:\n"
48  << " " << argv[0] << " --seqname I%04d.png --record 1\n"
49  << " " << argv[0] << " --seqname folder/I%04d.png --record 1\n"
50  << std::endl;
51  return 0;
52  }
53  }
54 
55  std::cout << "Settings : " << (opt_change_settings ? "modified" : "current") << std::endl;
56  std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
57 
58  std::string text_record_mode = std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
59 
60  if (! opt_seqname.empty()) {
61  std::cout << text_record_mode << std::endl;
62  std::cout << "Record name: " << opt_seqname << std::endl;
63  }
64 
66 
68 
69  vpPylonGrabber *g;
70  if (opt_device == "GigE" || opt_device == "gige") {
72  std::cout << "Opening Basler GigE camera: " << opt_camera << std::endl;
73  } else if (opt_device == "USB" || opt_device == "usb") {
75  std::cout << "Opening Basler USB camera: " << opt_camera << std::endl;
76  } else {
77  std::cout << "Error: only Basler GigE or USB cameras are supported." << std::endl;
78  return EXIT_SUCCESS;
79  }
80  g->setCameraIndex(opt_camera);
81 
82  g->open(I);
83 
84  std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
85 
86 #ifdef VISP_HAVE_X11
87  vpDisplayX d(I);
88 #elif defined(VISP_HAVE_GDI)
89  vpDisplayGDI d(I);
90 #elif defined(VISP_HAVE_OPENCV)
91  vpDisplayOpenCV d(I);
92 #else
93  std::cout << "No image viewer is available..." << std::endl;
94 #endif
95 
96  bool quit = false;
97  while (! quit) {
98  double t = vpTime::measureTimeMs();
99  g->acquire(I);
100 
102 
103  quit = record_helper(opt_seqname, opt_record_mode, I);
104 
105  std::stringstream ss;
106  ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
107  vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
108  vpDisplay::flush(I);
109  }
110  } catch (const vpException &e) {
111  std::cout << "Catch an exception: " << e << std::endl;
112  }
113 #else
114  (void) argc;
115  (void) argv;
116  std::cout << "Install Basler Pylon SDK, configure and build ViSP again to use this example" << std::endl;
117 #endif
118 }
virtual void open(vpImage< unsigned char > &I)=0
unsigned int getWidth() const
Definition: vpImage.h:239
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
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:151
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:88
static const vpColor red
Definition: vpColor.h:180
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:178
vpPylonGrabber * createPylonGrabber(DeviceClass dev_class)
Create an object of vpPylonGrabber.