Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-grabber-flycapture.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/vpFlyCaptureGrabber.h>
7
8void usage(const char *argv[], int error);
9
10void usage(const char *argv[], int error)
11{
12 std::cout << "SYNOPSIS" << std::endl
13 << " " << argv[0] << " [--change-settings]"
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 << " --change-settings" << std::endl
21 << " Force camera settings to auto shutter, auto gain and 640x480 MONO8 image" << std::endl
22 << " acquisition at 60 fps." << std::endl
23 << std::endl
24 << " --seqname <sequence name>" << std::endl
25 << " Name of the sequence of image to create (ie: /tmp/image%04d.jpg)." << std::endl
26 << " Default: empty." << std::endl
27 << std::endl
28 << " --record <mode>" << std::endl
29 << " Allowed values for mode are:" << std::endl
30 << " 0: record all the captures images (continuous mode)," << std::endl
31 << " 1: record only images selected by a user click (single shot mode)." << std::endl
32 << " Default mode: 0" << std::endl
33 << std::endl
34 << " --no-display" << std::endl
35 << " Disable displaying captured images." << std::endl
36 << " When used and sequence name specified, record mode is internally set to 1 (continuous mode)."
37 << std::endl
38 << std::endl
39 << " --help, -h" << std::endl
40 << " Print this helper message." << std::endl
41 << std::endl;
42 std::cout << "USAGE" << std::endl
43 << " Example to visualize images:" << std::endl
44 << " " << argv[0] << std::endl
45 << std::endl
46 << " Examples to record a sequence:" << std::endl
47 << " " << argv[0] << " --seqname I%04d.png" << std::endl
48 << " " << argv[0] << " --seqname folder/I%04d.png --record 0" << std::endl
49 << std::endl
50 << " Examples to record single shot images:\n"
51 << " " << argv[0] << " --seqname I%04d.png --record 1\n"
52 << " " << argv[0] << " --seqname folder/I%04d.png --record 1" << std::endl
53 << std::endl;
54
55 if (error) {
56 std::cout << "Error" << std::endl
57 << " "
58 << "Unsupported parameter " << argv[error] << std::endl;
59 }
60}
61
62int main(int argc, const char *argv[])
63{
64#if defined(VISP_HAVE_FLYCAPTURE) && defined(VISP_HAVE_THREADS)
65#ifdef ENABLE_VISP_NAMESPACE
66 using namespace VISP_NAMESPACE_NAME;
67#endif
68#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
69 std::shared_ptr<vpDisplay> display;
70#else
71 vpDisplay *display = nullptr;
72#endif
73 try {
74 std::string opt_seqname;
75 int opt_record_mode = 0;
76 bool opt_change_settings = false;
77 bool opt_display = true;
78
79 for (int i = 1; i < argc; i++) {
80 if (std::string(argv[i]) == "--change-settings") {
81 opt_change_settings = true;
82 }
83 else if (std::string(argv[i]) == "--seqname" && i + 1 < argc) {
84 opt_seqname = std::string(argv[++i]);
85 i++;
86 }
87 else if (std::string(argv[i]) == "--record" && i + 1 < argc) {
88 opt_record_mode = std::atoi(argv[++i]);
89 i++;
90 }
91 else if (std::string(argv[i]) == "--no-display") {
92 opt_display = false;
93 }
94 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
95 usage(argv, 0);
96 return EXIT_SUCCESS;
97 }
98 else {
99 usage(argv, i);
100 return EXIT_FAILURE;
101 }
102 }
103
104 if ((!opt_display) && (!opt_seqname.empty())) {
105 opt_record_mode = 0;
106 }
107
108 std::cout << "Settings : " << (opt_change_settings ? "modified" : "current") << std::endl;
109 std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
110 std::cout << "Display : " << (opt_display ? "enabled" : "disabled") << std::endl;
111
112 std::string text_record_mode =
113 std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
114
115 if (!opt_seqname.empty()) {
116 std::cout << text_record_mode << std::endl;
117 std::cout << "Record name: " << opt_seqname << std::endl;
118 }
119
120 vpImage<vpRGBa> I; // Create a gray level image container
122 vpFlyCaptureGrabber g; // Create a grabber based on FlyCapture SDK third party lib
124
126 if (opt_change_settings) {
127 try {
128 g.setShutter(true); // Turn auto shutter on
129 g.setGain(true); // Turn auto gain on
130 g.setVideoModeAndFrameRate(FlyCapture2::VIDEOMODE_640x480Y8, FlyCapture2::FRAMERATE_60);
131 }
132 catch (...) { // If settings are not available just catch exception to continue with default settings
133 std::cout << "Warning: cannot modify camera settings" << std::endl;
134 }
135 }
138 g.open(I);
140
141 std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
142
143 if (opt_display) {
144#if !(defined(VISP_HAVE_DISPLAY))
145 std::cout << "No image viewer is available..." << std::endl;
146 opt_display = false;
147#endif
148 }
149 if (opt_display) {
150#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
152#else
154#endif
155 }
156
157 vpImageQueue<vpRGBa> image_queue(opt_seqname, opt_record_mode);
158 vpImageStorageWorker<vpRGBa> image_storage_worker(std::ref(image_queue));
159 std::thread image_storage_thread(&vpImageStorageWorker<vpRGBa>::run, &image_storage_worker);
160
161 bool quit = false;
162 while (!quit) {
163 double t = vpTime::measureTimeMs();
165 g.acquire(I);
171 quit = image_queue.record(I);
173 std::stringstream ss;
174 ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
175 vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
177 }
178 image_queue.cancel();
179 image_storage_thread.join();
180 }
181 catch (const vpException &e) {
182 std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
183 }
184
185#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
186 if (display != nullptr) {
187 delete display;
188 }
189#endif
190#else
191 (void)argc;
192 (void)argv;
193#ifndef VISP_HAVE_FLYCAPTURE
194 std::cout << "Install Flycapture SDK, configure and build ViSP again to use this example" << std::endl;
195#endif
196#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
197 std::cout << "This tutorial should be built with c++11 support" << std::endl;
198#endif
199#endif
200}
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)
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
void open(vpImage< unsigned char > &I)
Definition of the vpImage class member functions.
Definition vpImage.h:131
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.
VISP_EXPORT double measureTimeMs()