Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-grabber-ids-ueye.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/vpUeyeGrabber.h>
7
8#define USE_COLOR // Comment to acquire gray level images
9
10void usage(const char *argv[], int error)
11{
12 std::cout << "SYNOPSIS" << std::endl
13 << " " << argv[0] << " [--device <index>]"
14 << " [--config-file <filename.ini>]"
15 << " [--fps <auto|fps value like 6|15|30|60>]"
16 << " [--gain <auto|value in 0 - 100>]"
17 << " [--shutter <auto|exposure value in ms>]"
18 << " [--subsample <factor>]"
19 << " [--white-balance <value>]"
20 << " [--color-mode <mode>]"
21 << " [--seqname <sequence name>]"
22 << " [--record <mode>]"
23 << " [--no-display]"
24 << " [--verbose] [-v]"
25 << " [--help] [-h]" << std::endl
26 << std::endl;
27 std::cout << "DESCRIPTION" << std::endl
28 << " --device <index>" << std::endl
29 << " Camera device index. Set 0 to dial with the first camera," << std::endl
30 << " and 1 to dial with the second camera attached to the computer." << std::endl
31 << " Default: 0" << std::endl
32 << std::endl
33 << " --config-file <filename.ini>" << std::endl
34 << " Camera config file." << std::endl
35 << " Default: empty." << std::endl
36 << std::endl
37 << " --fps <auto|fps value like 6|15|30|60>" << std::endl
38 << " \"Auto\" or a frames per second value." << std::endl
39 << " Default: current setting." << std::endl
40 << std::endl
41 << " --gain <auto|value in 0 - 100>" << std::endl
42 << " \"Auto\" or manual gain with a value in 0 - 100." << std::endl
43 << " Default: current setting." << std::endl
44 << std::endl
45 << " --shutter <auto|manu>" << std::endl
46 << " \"Auto\" or manual shutter." << std::endl
47 << " Default: current setting." << std::endl
48 << std::endl
49 << " --subsample <factor>" << std::endl
50 << " Subsample factor to reduce image size alog rows and columns." << std::endl
51 << " Default: 1." << std::endl
52 << std::endl
53 << " --white-balance <value>" << std::endl
54 << " Possible values are 0 (disabled) or 1 (enabled)." << std::endl
55 << " Default: current setting." << std::endl
56 << std::endl
57 << " --color-mode <mode>" << std::endl
58 << " Possible modes are: mono8, rgb24, rgb32, bayer8." << std::endl
59 << " Default: current setting." << std::endl
60 << std::endl
61 << " --seqname <sequence name>" << std::endl
62 << " Name of the sequence of image to create (ie: /tmp/image%04d.jpg)." << std::endl
63 << " Default: empty." << std::endl
64 << std::endl
65 << " --record <mode>" << std::endl
66 << " Allowed values for mode are:" << std::endl
67 << " 0: record all the captures images (continuous mode)," << std::endl
68 << " 1: record only images selected by a user click (single shot mode)." << std::endl
69 << " Default mode: 0" << std::endl
70 << std::endl
71 << " --no-display" << std::endl
72 << " Disable displaying captured images." << std::endl
73 << " When used and sequence name specified, record mode is internally set to 1 (continuous mode)."
74 << std::endl
75 << std::endl
76 << " --verbose, -v" << std::endl
77 << " Enable extra printings." << std::endl
78 << std::endl
79 << " --help, -h" << std::endl
80 << " Print this helper message." << std::endl
81 << std::endl;
82 std::cout << "USAGE" << std::endl
83 << " Example to visualize images:" << std::endl
84 << " " << argv[0] << std::endl
85 << std::endl
86 << " Examples to record a sequence of images:" << std::endl
87 << " " << argv[0] << " --seqname I%04d.png" << std::endl
88 << " " << argv[0] << " --seqname folder/I%04d.png --record 0" << std::endl
89 << std::endl
90 << " Examples to record single shot images:\n"
91 << " " << argv[0] << " --seqname I%04d.png --record 1\n"
92 << " " << argv[0] << " --seqname folder/I%04d.png --record 1" << std::endl
93 << std::endl;
94
95 if (error) {
96 std::cout << "Error" << std::endl
97 << " "
98 << "Unsupported parameter " << argv[error] << std::endl;
99 }
100}
101
107int main(int argc, const char *argv[])
108{
109#if defined(VISP_HAVE_UEYE) && defined(VISP_HAVE_THREADS)
110#ifdef ENABLE_VISP_NAMESPACE
111 using namespace VISP_NAMESPACE_NAME;
112#endif
113#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
114 std::shared_ptr<vpDisplay> display;
115#else
116 vpDisplay *display = nullptr;
117#endif
118 try {
119 unsigned int opt_device = 0;
120 std::string opt_seqname;
121 int opt_record_mode = 0;
122 std::string opt_config_file = "";
123 std::string opt_fps = "";
124 std::string opt_gain = "";
125 std::string opt_shutter = "";
126 std::string opt_color_mode = "";
127 int opt_white_balance = -1;
128 int opt_subsample = 1;
129 bool opt_verbose = false;
130 bool opt_display = true;
131
132 for (int i = 1; i < argc; i++) {
133 if (std::string(argv[i]) == "--device" && i + 1 < argc) {
134 opt_device = static_cast<unsigned int>(std::atoi(argv[++i]));
135 i++;
136 }
137 else if (std::string(argv[i]) == "--config-file" && i + 1 < argc) {
138 opt_config_file = std::string(argv[++i]);
139 i++;
140 }
141 else if (std::string(argv[i]) == "--fps" && i + 1 < argc) {
142 opt_fps = std::string(argv[++i]);
143 i++;
144 }
145 else if (std::string(argv[i]) == "--gain" && i + 1 < argc) {
146 opt_gain = std::string(argv[++i]);
147 i++;
148 }
149 else if (std::string(argv[i]) == "--shutter" && i + 1 < argc) {
150 opt_shutter = std::string(argv[++i]);
151 i++;
152 }
153 else if (std::string(argv[i]) == "--subsample" && i + 1 < argc) {
154 opt_subsample = std::atoi(argv[++i]);
155 i++;
156 }
157 else if (std::string(argv[i]) == "--white-balance" && i + 1 < argc) {
158 opt_white_balance = std::atoi(argv[++i]);
159 i++;
160 }
161 else if (std::string(argv[i]) == "--color-mode" && i + 1 < argc) {
162 opt_color_mode = std::string(argv[++i]);
163 i++;
164 }
165 else if (std::string(argv[i]) == "--seqname" && i + 1 < argc) {
166 opt_seqname = std::string(argv[++i]);
167 i++;
168 }
169 else if (std::string(argv[i]) == "--record" && i + 1 < argc) {
170 opt_record_mode = std::atoi(argv[++i]);
171 i++;
172 }
173 else if (std::string(argv[i]) == "--verbose" || std::string(argv[i]) == "-v") {
174 opt_verbose = true;
175 }
176 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
177 usage(argv, 0);
178 return EXIT_SUCCESS;
179 }
180 else {
181 usage(argv, i);
182 return EXIT_FAILURE;
183 }
184 }
185
186 if ((!opt_display) && (!opt_seqname.empty())) {
187 opt_record_mode = 0;
188 }
189
191#ifdef USE_COLOR
192 vpImage<vpRGBa> I; // To acquire color images
193#else
194 vpImage<unsigned char> I; // To acquire gray images
195#endif
197
200
201 // Get info on connected cameras
202 std::vector<unsigned int> cam_ids = g.getCameraIDList();
203 std::vector<std::string> cam_models = g.getCameraModelList();
204 std::vector<std::string> cam_serials = g.getCameraSerialNumberList();
205
206 if (!cam_ids.size()) {
207 std::cout << "No camera detected. Plug a camera and try again..." << std::endl;
208 return EXIT_FAILURE;
209 }
210 std::cout << "Found " << cam_ids.size() << " cameras :" << std::endl;
211 for (unsigned int i = 0; i < cam_ids.size(); i++) {
212 std::cout << (opt_device == i ? " * Camera " : " Camera ") << i << " - ID: " << cam_ids[i]
213 << " Model: " << cam_models[i] << " S/N: " << cam_serials[i] << std::endl;
214 }
216
218 if (!g.setActiveCamera(opt_device)) {
219 std::cout << "Unable to select camera " << opt_device << std::endl;
220 return EXIT_FAILURE;
221 };
223
224 std::cout << "Active camera is Model " << g.getActiveCameraModel()
225 << " with S/N: " << g.getActiveCameraSerialNumber() << std::endl;
226
228 g.open(I);
230
231 if (!opt_config_file.empty()) {
233 g.loadParameters(opt_config_file);
235 // Since loaded parameters may affect image size, rescale image in case of
237 I.resize(g.getFrameHeight(), g.getFrameWidth());
239 }
240
241 if (opt_subsample > 1) {
242 std::cout << "Subsampling factor: " << opt_subsample << std::endl;
243 g.setSubsampling(opt_subsample);
244 // Since subsampling may affect image size, rescale image in case of
245 I.resize(g.getFrameHeight(), g.getFrameWidth());
246 }
247
248 if (!opt_gain.empty()) {
249 if (opt_gain == "auto") {
250 std::cout << "Auto gain : " << (g.setGain(true) ? "enabled" : "N/A") << std::endl;
251 }
252 else {
253 std::cout << "Manual gain : "
254 << (g.setGain(false, std::atoi(opt_gain.c_str())) ? (std::string(opt_gain) + " %") : "N/A")
255 << std::endl;
256 }
257 }
258 if (!opt_shutter.empty()) {
259 if (opt_shutter == "auto") {
260 std::cout << "Auto shutter : " << (g.setExposure(true) ? "enabled" : "N/A") << std::endl;
261 }
262 else {
263 std::cout << "Manual shutter : "
264 << (g.setExposure(false, std::atof(opt_shutter.c_str())) ? (std::string(opt_shutter) + " ms") : "N/A")
265 << std::endl;
266 }
267 }
268
269 if (opt_white_balance > 0) {
270 bool wb = (opt_white_balance ? true : false);
271 std::cout << "Subsampling factor: " << opt_subsample << std::endl;
272 std::cout << "White balance : " << (wb ? "auto" : "disabled") << std::endl;
273 g.setWhiteBalance(wb);
274 }
275
276 if (!opt_color_mode.empty()) {
277 if (g.setColorMode(opt_color_mode)) {
278 std::cout << "Color mode : " << opt_color_mode << std::endl;
279 }
280 }
281
282 if (!opt_fps.empty()) {
283 if (opt_fps == "auto") {
284 std::cout << "Auto framerate : " << (g.setFrameRate(true) ? "enabled" : "N/A") << std::endl;
285 }
286 else {
287 std::cout << "Manual framerate : "
288 << (g.setFrameRate(false, std::atof(opt_fps.c_str())) ? (std::string(opt_fps) + " Hz") : "N/A")
289 << std::endl;
290 }
291 }
292
293 std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
294 std::cout << "Display : " << (opt_display ? "enabled" : "disabled") << std::endl;
295
296 std::string text_record_mode =
297 std::string("Record mode : ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
298
299 if (!opt_seqname.empty()) {
300 std::cout << text_record_mode << std::endl;
301 std::cout << "Record name : " << opt_seqname << std::endl;
302 }
303
304 std::cout << "Config file : " << (opt_config_file.empty() ? "empty" : opt_config_file) << std::endl;
305 std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
306
307 if (opt_display) {
308#if !(defined(VISP_HAVE_DISPLAY))
309 std::cout << "No image viewer is available..." << std::endl;
310 opt_display = false;
311#else
312#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
314#else
316#endif
317 d->setDownScalingFactor(vpDisplay::SCALE_AUTO);
318 d->init(I);
319#endif
320 }
321
322#ifdef USE_COLOR
323 vpImageQueue<vpRGBa> image_queue(opt_seqname, opt_record_mode);
324 vpImageStorageWorker<vpRGBa> image_storage_worker(std::ref(image_queue));
325 std::thread image_storage_thread(&vpImageStorageWorker<vpRGBa>::run, &image_storage_worker);
326#else
327 vpImageQueue<unsigned char> image_queue(opt_seqname, opt_record_mode);
328 vpImageStorageWorker<unsigned char> image_storage_worker(std::ref(image_queue));
329 std::thread image_storage_thread(&vpImageStorageWorker<unsigned char>::run, &image_storage_worker);
330#endif
331
332 bool quit = false;
333 double timestamp_camera = 0, timestamp_camera_prev = 0;
334 std::string timestamp_system;
335 while (!quit) {
336 g.acquire(I, &timestamp_camera, &timestamp_system);
337 double fps = g.getFramerate();
338
340
341 quit = image_queue.record(I, &timestamp_system);
342
343 if (opt_verbose) {
344 std::cout << "System timestamp: " << timestamp_system << std::endl;
345 std::cout << "Camera timestamp diff: " << timestamp_camera - timestamp_camera_prev << std::endl;
346 timestamp_camera_prev = timestamp_camera;
347 }
348 vpDisplay::displayText(I, static_cast<int>(I.getHeight() - 40 * vpDisplay::getDownScalingFactor(I)),
349 static_cast<int>(10 * vpDisplay::getDownScalingFactor(I)), timestamp_system, vpColor::red);
350 {
351 std::stringstream ss;
352 ss << "Camera framerate: " << fps;
353 vpDisplay::displayText(I, static_cast<int>(I.getHeight() - 60 * vpDisplay::getDownScalingFactor(I)),
354 static_cast<int>(10 * vpDisplay::getDownScalingFactor(I)), ss.str(), vpColor::red);
355 }
356
358 }
359 image_queue.cancel();
360 image_storage_thread.join();
361 }
362 catch (const vpException &e) {
363 std::cout << "Catch an exception: " << e << std::endl;
364 }
365
366#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
367 if (display != nullptr) {
368 delete display;
369 }
370#endif
371#else
372 (void)argc;
373 (void)argv;
374#ifndef VISP_HAVE_UEYE
375 std::cout << "Install IDS uEye SDK, configure and build ViSP again to use this example" << std::endl;
376#endif
377#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
378 std::cout << "This tutorial should be built with c++11 support" << std::endl;
379#endif
380#endif
381}
static const vpColor red
Definition vpColor.h:198
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
unsigned int getDownScalingFactor()
Definition vpDisplay.h:218
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
Definition of the vpImage class member functions.
Definition vpImage.h:131
void open(vpImage< unsigned char > &I)
std::vector< std::string > getCameraSerialNumberList() const
bool setExposure(bool auto_exposure, double exposure_ms=-1)
bool setFrameRate(bool auto_frame_rate, double manual_frame_rate_hz=-1)
void setWhiteBalance(bool auto_wb)
bool setGain(bool auto_gain, int master_gain=-1, bool gain_boost=false)
void acquire(vpImage< unsigned char > &I, double *timestamp_camera=nullptr, std::string *timestamp_system=nullptr)
double getFramerate() const
void loadParameters(const std::string &filename)
std::vector< std::string > getCameraModelList() const
std::vector< unsigned int > getCameraIDList() const
void setSubsampling(int factor)
unsigned int getFrameHeight() const
unsigned int getFrameWidth() const
bool setActiveCamera(unsigned int cam_index)
bool setColorMode(const std::string &color_mode)
std::string getActiveCameraModel() const
std::string getActiveCameraSerialNumber() const
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.