1 /*! \example tutorial-grabber-basler-pylon.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/vpPylonFactory.h>
7 #include <visp3/io/vpImageStorageWorker.h>
8 
9 /*!
10   Usage :
11     To get the help    : ./tutorial-grabber-basler-pylon --help
12     To set the device  : ./tutorial-grabber-basler-pylon --device GigE --camera 1
13  */
main(int argc,const char * argv[])14 int main(int argc, const char *argv[])
15 {
16 #if defined(VISP_HAVE_PYLON) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
17   try {
18     unsigned int opt_camera = 0;
19     std::string opt_device("GigE");
20     std::string opt_seqname;
21     int opt_record_mode = 0;
22     bool opt_change_settings = false;
23 
24     for (int i = 0; i < argc; i++) {
25       if (std::string(argv[i]) == "--camera")
26         opt_camera = (unsigned int)atoi(argv[i + 1]);
27       else if (std::string(argv[i]) == "--device")
28         opt_device = std::string(argv[i + 1]);
29       else if (std::string(argv[i]) == "--seqname")
30         opt_seqname = std::string(argv[i + 1]);
31       else if (std::string(argv[i]) == "--record")
32         opt_record_mode = std::atoi(argv[i + 1]);
33       else if (std::string(argv[i]) == "--change_settings")
34         opt_change_settings = true;
35       else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
36         std::cout << "\nUsage: " << argv[0]
37                   << " [--camera <0...9> (default: 0)] [--device <\"GigE\"|\"USB\" (default: GigE)>]"
38                   << " [--seqname <sequence name (default: empty)>] [--record <0: continuous | 1: single shot (default: 0)>]"
39                   << " [--change_settings] [--help] [-h]\n"
40                   << "\nExample to visualize images:\n"
41                   << "  " << argv[0] << " \n"
42                   << "  " << argv[0] << " --device GigE --camera 0\n"
43                   << "\nExamples to record a sequence:\n"
44                   << "  " << argv[0] << " --seqname I%04d.png \n"
45                   << "  " << argv[0] << " --seqname folder/I%04d.png --record 0\n"
46                   << "\nExamples to record single shot images:\n"
47                   << "  " << argv[0] << " --seqname I%04d.png --record 1\n"
48                   << "  " << argv[0] << " --seqname folder/I%04d.png --record 1\n"
49                   << std::endl;
50         return 0;
51       }
52     }
53 
54     std::cout << "Settings   : " << (opt_change_settings ? "modified" : "current") << std::endl;
55     std::cout << "Recording  : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
56 
57     std::string text_record_mode = std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
58 
59     if (! opt_seqname.empty()) {
60       std::cout << text_record_mode << std::endl;
61       std::cout << "Record name: " << opt_seqname << std::endl;
62     }
63 
64     vpImage<unsigned char> I;
65 
66     vpPylonFactory &factory = vpPylonFactory::instance();
67 
68     vpPylonGrabber *g;
69     if (opt_device == "GigE" || opt_device == "gige") {
70       g = factory.createPylonGrabber(vpPylonFactory::BASLER_GIGE);
71       std::cout << "Opening Basler GigE camera: " << opt_camera << std::endl;
72     } else if (opt_device == "USB" || opt_device == "usb") {
73       g = factory.createPylonGrabber(vpPylonFactory::BASLER_USB);
74       std::cout << "Opening Basler USB camera: " << opt_camera << std::endl;
75     } else {
76       std::cout << "Error: only Basler GigE or USB cameras are supported." << std::endl;
77       return EXIT_SUCCESS;
78     }
79     g->setCameraIndex(opt_camera);
80 
81     g->open(I);
82 
83     std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
84 
85 #ifdef VISP_HAVE_X11
86     vpDisplayX d(I);
87 #elif defined(VISP_HAVE_GDI)
88     vpDisplayGDI d(I);
89 #elif defined(VISP_HAVE_OPENCV)
90     vpDisplayOpenCV d(I);
91 #else
92     std::cout << "No image viewer is available..." << std::endl;
93 #endif
94 
95     vpImageQueue<unsigned char> image_queue(opt_seqname, opt_record_mode);
96     vpImageStorageWorker<unsigned char> image_storage_worker(std::ref(image_queue));
97     std::thread image_storage_thread(&vpImageStorageWorker<unsigned char>::run, &image_storage_worker);
98 
99     bool quit = false;
100     while (! quit) {
101       double t = vpTime::measureTimeMs();
102       g->acquire(I);
103 
104       vpDisplay::display(I);
105 
106       quit = image_queue.record(I);
107 
108       std::stringstream ss;
109       ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
110       vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
111       vpDisplay::flush(I);
112     }
113     image_queue.cancel();
114     image_storage_thread.join();
115   } catch (const vpException &e) {
116     std::cout << "Catch an exception: " << e << std::endl;
117   }
118 #else
119   (void) argc;
120   (void) argv;
121 #ifndef VISP_HAVE_PYLON
122   std::cout << "Install Basler Pylon SDK, configure and build ViSP again to use this example" << std::endl;
123 #endif
124 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
125   std::cout << "This turorial should be built with c++11 support" << std::endl;
126 #endif
127 #endif
128 }
129