1 /*! \example tutorial-grabber-ids-ueye.cpp */
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/vpUeyeGrabber.h>
7 #include <visp3/io/vpImageStorageWorker.h>
8 
9 #define USE_COLOR // Comment to acquire gray level images
10 
11 /*!
12   Usage :
13     To get the help    : ./tutorial-grabber-ids-ueye --help
14     To set the device  : ./tutorial-grabber-ids-ueye
15  */
main(int argc,const char * argv[])16 int main(int argc, const char *argv[])
17 {
18 #if defined(VISP_HAVE_UEYE) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
19   try {
20     unsigned int opt_camera = 0;
21     std::string opt_seqname;
22     int opt_record_mode = 0;
23     std::string opt_config_file = "";
24     std::string opt_fps = "";
25     std::string opt_gain = "";
26     std::string opt_shutter = "";
27     std::string opt_color_mode = "";
28     int opt_white_balance = -1;
29     int opt_subsample = 1;
30     bool opt_verbose = false;
31 
32     for (int i = 0; i < argc; i++) {
33       if (std::string(argv[i]) == "--camera")
34         opt_camera = (unsigned int)atoi(argv[i + 1]);
35       else if (std::string(argv[i]) == "--config-file")
36         opt_config_file = std::string(argv[i + 1]);
37       else if (std::string(argv[i]) == "--fps")
38         opt_fps = std::string(argv[i + 1]);
39       else if (std::string(argv[i]) == "--gain")
40         opt_gain = std::string(argv[i + 1]);
41       else if (std::string(argv[i]) == "--shutter")
42         opt_shutter = std::string(argv[i + 1]);
43       else if (std::string(argv[i]) == "--subsample")
44         opt_subsample = std::atoi(argv[i + 1]);
45       else if (std::string(argv[i]) == "--white-balance")
46         opt_white_balance = std::atoi(argv[i + 1]);
47       else if (std::string(argv[i]) == "--color-mode")
48         opt_color_mode = std::string(argv[i + 1]);
49       else if (std::string(argv[i]) == "--seqname")
50         opt_seqname = std::string(argv[i + 1]);
51       else if (std::string(argv[i]) == "--record")
52         opt_record_mode = std::atoi(argv[i + 1]);
53       else if (std::string(argv[i]) == "--verbose" || std::string(argv[i]) == "-v")
54         opt_verbose = true;
55       else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
56         std::cout << "\nUsage: " << argv[0]
57                   << " [--camera <index> (default: 0)]"
58                   << " [--config-file <filename.ini> (default: empty)]"
59                   << " [--fps <auto | value> (default: empty)]"
60                   << " [--gain <auto | value in 0 - 100> (default: empty)]"
61                   << " [--shutter <auto | exposure value in ms> (default: empty)]"
62                   << " [--subsample <1,2,3,4,5,6,8,16> (default: 1)]"
63                   << " [--white-balance <0: disabled, 1: enabled> (default: -1)]"
64                   << " [--color-mode <mono8, rgb24, rgb32> (default: empty)]"
65                   << " [--seqname <sequence name> (default: empty)]"
66                   << " [--record <0: continuous | 1: single shot> (default: 0)]"
67                   << " [--verbose] [-v]"
68                   << " [--help] [-h]\n"
69                   << "\nExample to visualize images:\n"
70                   << "  " << argv[0] << " \n"
71                   << "\nExample to visualize images and set camera parameter from a config file:\n"
72                   << "  " << argv[0] << " --config-file UI-388xCP-C.ini\n"
73                   << "\nExamples to record a sequence:\n"
74                   << "  " << argv[0] << " --seqname I%04d.png \n"
75                   << "  " << argv[0] << " --seqname folder/I%04d.png --record 0\n"
76                   << "\nExamples to record single shot images:\n"
77                   << "  " << argv[0] << " --seqname I%04d.png --record 1\n"
78                   << "  " << argv[0] << " --seqname folder/I%04d.png --record 1\n"
79                   << std::endl;
80         return 0;
81       }
82     }
83 
84     //! [Create image]
85 #ifdef USE_COLOR
86     vpImage<vpRGBa> I;        // To acquire color images
87 #else
88     vpImage<unsigned char> I; // To acquire gray images
89 #endif
90     //! [Create image]
91 
92     //! [List camera info]
93     vpUeyeGrabber g;
94 
95     // Get info on connected cameras
96     std::vector<unsigned int> cam_ids = g.getCameraIDList();
97     std::vector<std::string> cam_models = g.getCameraModelList();
98     std::vector<std::string> cam_serials = g.getCameraSerialNumberList();
99 
100     if (! cam_ids.size()) {
101       std::cout << "No camera detected. Plug a camera and try again..." << std::endl;
102       return EXIT_FAILURE;
103     }
104     std::cout << "Found " << cam_ids.size() << " cameras :"<< std::endl;
105     for (unsigned int i = 0; i < cam_ids.size(); i++) {
106       std::cout << (opt_camera == i ? " * Camera " : "   Camera ") << i
107                 << " - ID: " << cam_ids[i]  << " Model: " << cam_models[i] << " S/N: " << cam_serials[i] << std::endl;
108     }
109     //! [List camera info]
110 
111     //! [Active camera info]
112     if (! g.setActiveCamera(opt_camera)) {
113       std::cout << "Unable to select camera " << opt_camera << std::endl;
114       return EXIT_FAILURE;
115     };
116     //! [Active camera info]
117 
118     std::cout << "Active camera is Model " << g.getActiveCameraModel() << " with S/N: " << g.getActiveCameraSerialNumber() << std::endl;
119 
120     //! [Open connection]
121     g.open(I);
122     //! [Open connection]
123 
124     if (! opt_config_file.empty()) {
125       //! [Load settings from file]
126       g.loadParameters(opt_config_file);
127       //! [Load settings from file]
128       // Since loaded parameters may affect image size, rescale image in case of
129       //! [Update image size]
130       I.resize(g.getFrameHeight(), g.getFrameWidth());
131       //! [Update image size]
132     }
133 
134     if (opt_subsample > 1) {
135       std::cout << "Subsampling factor: " << opt_subsample << std::endl;
136       g.setSubsampling(opt_subsample);
137       // Since subsampling may affect image size, rescale image in case of
138       I.resize(g.getFrameHeight(), g.getFrameWidth());
139     }
140 
141     if (! opt_gain.empty()) {
142       if (opt_gain == "auto") {
143         std::cout << "Auto gain         : " << (g.setGain(true) ? "enabled" : "N/A") << std::endl;
144       }
145       else {
146         std::cout << "Manual gain       : " << (g.setGain(false, std::atoi(opt_gain.c_str())) ? (std::string(opt_gain) + " %") : "N/A") << std::endl;
147       }
148     }
149     if (! opt_shutter.empty()) {
150       if (opt_shutter == "auto") {
151         std::cout << "Auto shutter      : " << (g.setExposure(true) ? "enabled" : "N/A") << std::endl;
152       }
153       else {
154         std::cout << "Manual shutter    : " << (g.setExposure(false, std::atof(opt_shutter.c_str())) ? (std::string(opt_shutter) + " ms") : "N/A") << std::endl;
155       }
156     }
157 
158     if (opt_white_balance > 0) {
159       bool wb = (opt_white_balance ? true : false);
160       std::cout << "Subsampling factor: " << opt_subsample << std::endl;
161       std::cout << "White balance     : " << (wb ? "auto" : "disabled") << std::endl;
162       g.setWhiteBalance(wb);
163     }
164 
165     if (! opt_color_mode.empty()) {
166       if (g.setColorMode(opt_color_mode)) {
167         std::cout << "Color mode        : " << opt_color_mode << std::endl;
168       }
169     }
170 
171     if (! opt_fps.empty()) {
172       if (opt_fps == "auto") {
173         std::cout << "Auto framerate    : " << (g.setFrameRate(true) ? "enabled" : "N/A") << std::endl;
174       }
175       else {
176         std::cout << "Manual framerate  : " << (g.setFrameRate(false, std::atof(opt_fps.c_str())) ? (std::string(opt_fps) + " Hz") : "N/A") << std::endl;
177       }
178     }
179 
180     std::cout << "Recording         : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
181 
182     std::string text_record_mode = std::string("Record mode       : ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
183 
184     if (! opt_seqname.empty()) {
185       std::cout << text_record_mode << std::endl;
186       std::cout << "Record name       : " << opt_seqname << std::endl;
187     }
188 
189     std::cout << "Config file       : " << (opt_config_file.empty() ? "empty" : opt_config_file) << std::endl;
190     std::cout << "Image size        : " << I.getWidth() << " " << I.getHeight() << std::endl;
191 
192 #ifdef VISP_HAVE_X11
193     vpDisplayX d;
194 #elif defined(VISP_HAVE_GDI)
195     vpDisplayGDI d;
196 #elif defined(VISP_HAVE_OPENCV)
197     vpDisplayOpenCV d;
198 #else
199     std::cout << "No image viewer is available..." << std::endl;
200 #endif
201 
202 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
203     d.setDownScalingFactor(vpDisplay::SCALE_AUTO);
204     d.init(I);
205 #endif
206 
207 #ifdef USE_COLOR
208     vpImageQueue<vpRGBa> image_queue(opt_seqname, opt_record_mode);
209     vpImageStorageWorker<vpRGBa> image_storage_worker(std::ref(image_queue));
210     std::thread image_storage_thread(&vpImageStorageWorker<vpRGBa>::run, &image_storage_worker);
211 #else
212     vpImageQueue<unsigned char> image_queue(opt_seqname, opt_record_mode);
213     vpImageStorageWorker<unsigned char> image_storage_worker(std::ref(image_queue));
214     std::thread image_storage_thread(&vpImageStorageWorker<unsigned char>::run, &image_storage_worker);
215 #endif
216 
217     bool quit = false;
218     double timestamp_camera = 0, timestamp_camera_prev = 0;
219     std::string timestamp_system;
220     while (! quit) {
221       g.acquire(I, &timestamp_camera, &timestamp_system);
222       double fps = g.getFramerate();
223 
224       vpDisplay::display(I);
225 
226       quit = image_queue.record(I, &timestamp_system);
227 
228       if (opt_verbose) {
229         std::cout << "System timestamp: " << timestamp_system << std::endl;
230         std::cout << "Camera timestamp diff: " << timestamp_camera - timestamp_camera_prev << std::endl;
231         timestamp_camera_prev = timestamp_camera;
232       }
233       vpDisplay::displayText(I, static_cast<int>(I.getHeight() - 40 * vpDisplay::getDownScalingFactor(I)),
234                              static_cast<int>(10 * vpDisplay::getDownScalingFactor(I)), timestamp_system, vpColor::red);
235       {
236         std::stringstream ss;
237         ss << "Camera framerate: " << fps;
238         vpDisplay::displayText(I, static_cast<int>(I.getHeight() - 60 * vpDisplay::getDownScalingFactor(I)),
239                                static_cast<int>(10 * vpDisplay::getDownScalingFactor(I)), ss.str(), vpColor::red);
240       }
241 
242       vpDisplay::flush(I);
243     }
244     image_queue.cancel();
245     image_storage_thread.join();
246   } catch (const vpException &e) {
247     std::cout << "Catch an exception: " << e << std::endl;
248   }
249 #else
250   (void) argc;
251   (void) argv;
252 #ifndef VISP_HAVE_UEYE
253   std::cout << "Install IDS uEye SDK, configure and build ViSP again to use this example" << std::endl;
254 #endif
255 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
256   std::cout << "This turorial should be built with c++11 support" << std::endl;
257 #endif
258 #endif
259 }
260