Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
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
9void 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}
74int main(int argc, const char *argv[])
75{
76#if defined(VISP_HAVE_PYLON) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
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 } else if (std::string(argv[i]) == "--seqname") {
94 opt_seqname = std::string(argv[i + 1]);
95 i++;
96 } else if (std::string(argv[i]) == "--record") {
97 opt_record_mode = std::atoi(argv[i + 1]);
98 i++;
99 } else if (std::string(argv[i]) == "--no-display") {
100 opt_display = false;
101 } else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
102 usage(argv, 0);
103 return EXIT_SUCCESS;
104 } else {
105 usage(argv, i);
106 return EXIT_FAILURE;
107 }
108 }
109
110 if ((!opt_display) && (!opt_seqname.empty())) {
111 opt_record_mode = 0;
112 }
113
114 std::cout << "Settings : " << (opt_change_settings ? "modified" : "current") << std::endl;
115 std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
116 std::cout << "Display : " << (opt_display ? "enabled" : "disabled") << std::endl;
117
118 std::string text_record_mode =
119 std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
120
121 if (!opt_seqname.empty()) {
122 std::cout << text_record_mode << std::endl;
123 std::cout << "Record name: " << opt_seqname << std::endl;
124 }
125
127
129
131 if (opt_type == "GigE" || opt_type == "gige") {
132 g = factory.createPylonGrabber(vpPylonFactory::BASLER_GIGE);
133 std::cout << "Opening Basler GigE camera: " << opt_device << std::endl;
134 } else if (opt_type == "USB" || opt_type == "usb") {
135 g = factory.createPylonGrabber(vpPylonFactory::BASLER_USB);
136 std::cout << "Opening Basler USB camera: " << opt_device << std::endl;
137 } else {
138 std::cout << "Error: only Basler GigE or USB cameras are supported." << std::endl;
139 return EXIT_SUCCESS;
140 }
141 g->setCameraIndex(opt_device);
142
143 g->open(I);
144
145 std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
146
147 vpDisplay *d = NULL;
148 if (opt_display) {
149#if !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
150 std::cout << "No image viewer is available..." << std::endl;
151 opt_display = false;
152#endif
153 }
154 if (opt_display) {
155#ifdef VISP_HAVE_X11
156 d = new vpDisplayX(I);
157#elif defined(VISP_HAVE_GDI)
158 d = new vpDisplayGDI(I);
159#elif defined(HAVE_OPENCV_HIGHGUI)
160 d = new vpDisplayOpenCV(I);
161#endif
162 }
163
164 vpImageQueue<vpRGBa> image_queue(opt_seqname, opt_record_mode);
165 vpImageStorageWorker<vpRGBa> image_storage_worker(std::ref(image_queue));
166 std::thread image_storage_thread(&vpImageStorageWorker<vpRGBa>::run, &image_storage_worker);
167
168 bool quit = false;
169 while (!quit) {
170 double t = vpTime::measureTimeMs();
171 g->acquire(I);
172
174
175 quit = image_queue.record(I);
176
177 std::stringstream ss;
178 ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
179 vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
181 }
182 image_queue.cancel();
183 image_storage_thread.join();
184
185 if (d) {
186 delete d;
187 }
188 } catch (const vpException &e) {
189 std::cout << "Catch an exception: " << e << std::endl;
190 }
191#else
192 (void)argc;
193 (void)argv;
194#ifndef VISP_HAVE_PYLON
195 std::cout << "Install Basler Pylon SDK, configure and build ViSP again to use this example" << std::endl;
196#endif
197#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
198 std::cout << "This tutorial should be built with c++11 support" << std::endl;
199#endif
200#endif
201}
static const vpColor red
Definition vpColor.h:211
Display for windows using GDI (available on any windows 32 platform).
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:132
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
Definition of the vpImage class member functions.
Definition vpImage.h:135
unsigned int getWidth() const
Definition vpImage.h:242
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.
virtual void setCameraIndex(unsigned int index)=0
VISP_EXPORT double measureTimeMs()