Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-grabber-realsense.cpp
1
2#include <visp3/core/vpConfig.h>
3#include <visp3/core/vpImage.h>
4#include <visp3/core/vpXmlParserCamera.h>
5#include <visp3/gui/vpDisplayFactory.h>
6#include <visp3/io/vpImageStorageWorker.h>
7#include <visp3/sensor/vpRealSense2.h>
8
9void usage(const char *argv[], int error)
10{
11 std::cout << "SYNOPSIS" << std::endl
12 << " " << argv[0] << " [--fps <6|15|30|60>]"
13 << " [--width <image width>]"
14 << " [--height <image height>]"
15 << " [--seqname <sequence name>]"
16 << " [--save-distortion]"
17 << " [--record <mode>]"
18 << " [--no-display]"
19 << " [--help] [-h]" << std::endl
20 << std::endl;
21 std::cout << "DESCRIPTION" << std::endl
22 << " --fps <6|15|30|60>" << std::endl
23 << " Frames per second." << std::endl
24 << " Default: 30." << std::endl
25 << std::endl
26 << " --width <image width>" << std::endl
27 << " Default: 640." << std::endl
28 << std::endl
29 << " --height <image height>" << std::endl
30 << " Default: 480." << std::endl
31 << std::endl
32 << " --seqname <sequence name>" << std::endl
33 << " Name of the sequence of image to create (ie: /tmp/image%04d.jpg)." << std::endl
34 << " Default: empty." << std::endl
35 << std::endl
36 << " --save-distortion" << std::endl
37 << " Flag to save the distortion coefficient parameters in the XML file." << std::endl
38 << std::endl
39 << " --record <mode>" << std::endl
40 << " Allowed values for mode are:" << std::endl
41 << " 0: record all the captures images (continuous mode)," << std::endl
42 << " 1: record only images selected by a user click (single shot mode)." << std::endl
43 << " Default mode: 0" << std::endl
44 << std::endl
45 << " --no-display" << std::endl
46 << " Disable displaying captured images." << std::endl
47 << " When used and sequence name specified, record mode is internally set to 1 (continuous mode)."
48 << std::endl
49 << " --help, -h" << std::endl
50 << " Print this helper message." << std::endl
51 << std::endl;
52 std::cout << "USAGE" << std::endl
53 << " Example to visualize images:" << std::endl
54 << " " << argv[0] << std::endl
55 << std::endl
56 << " Examples to record a sequence of successive images in 640x480 resolution:" << std::endl
57 << " " << argv[0] << " --seqname I%04d.png" << std::endl
58 << " " << argv[0] << " --seqname folder/I%04d.png --record 0" << std::endl
59 << std::endl
60 << " Examples to record single shot 640x480 images:\n"
61 << " " << argv[0] << " --seqname I%04d.png --record 1\n"
62 << " " << argv[0] << " --seqname folder/I%04d.png --record 1" << std::endl
63 << std::endl
64 << " Examples to record single shot 1280x720 images:\n"
65 << " " << argv[0] << " --seqname I%04d.png --record 1 --width 1280 --height 720" << std::endl
66 << std::endl;
67
68 if (error) {
69 std::cout << "Error" << std::endl
70 << " "
71 << "Unsupported parameter " << argv[error] << std::endl;
72 }
73}
74
78int main(int argc, const char *argv[])
79{
80#if defined(VISP_HAVE_REALSENSE2) && defined(VISP_HAVE_THREADS)
81#ifdef ENABLE_VISP_NAMESPACE
82 using namespace VISP_NAMESPACE_NAME;
83#endif
84#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
85 std::shared_ptr<vpDisplay> display;
86#else
87 vpDisplay *display = nullptr;
88#endif
89 try {
90 std::string opt_seqname;
91 int opt_record_mode = 0;
92 int opt_fps = 30;
93 bool opt_display = true;
94 unsigned int opt_width = 640;
95 unsigned int opt_height = 480;
96 bool save_distortion = false;
97
98 for (int i = 1; i < argc; i++) {
99 if (std::string(argv[i]) == "--fps" && i + 1 < argc) {
100 opt_fps = std::atoi(argv[++i]);
101 }
102 else if (std::string(argv[i]) == "--seqname" && i + 1 < argc) {
103 opt_seqname = std::string(argv[++i]);
104 }
105 else if (std::string(argv[i]) == "--width" && i + 1 < argc) {
106 opt_width = std::atoi(argv[++i]);
107 }
108 else if (std::string(argv[i]) == "--height" && i + 1 < argc) {
109 opt_height = std::atoi(argv[++i]);
110 }
111 else if (std::string(argv[i]) == "--record" && i + 1 < argc) {
112 opt_record_mode = std::atoi(argv[++i]);
113 }
114 else if (std::string(argv[i]) == "--no-display") {
115 opt_display = false;
116 }
117 else if (std::string(argv[i]) == "--save-distortion") {
118 save_distortion = true;
119 }
120 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
121 usage(argv, 0);
122 return EXIT_SUCCESS;
123 }
124 else {
125 usage(argv, i);
126 return EXIT_FAILURE;
127 }
128 }
129
130 if ((!opt_display) && (!opt_seqname.empty())) {
131 opt_record_mode = 0;
132 }
133
134 if (opt_fps != 6 && opt_fps != 15 && opt_fps != 30 && opt_fps != 60) {
135 opt_fps = 30; // Default
136 }
137 std::cout << "Resolution : " << opt_width << " x " << opt_height << std::endl;
138 std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
139 std::cout << "Framerate : " << opt_fps << std::endl;
140 std::cout << "Display : " << (opt_display ? "enabled" : "disabled") << std::endl;
141
142 std::string text_record_mode =
143 std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
144
145 if (!opt_seqname.empty()) {
146 std::cout << text_record_mode << std::endl;
147 std::cout << "Record name: " << opt_seqname << std::endl;
148 }
150
151 std::cout << "SDK : Realsense 2" << std::endl;
152 vpRealSense2 g;
153 rs2::config config;
154 config.disable_stream(RS2_STREAM_DEPTH);
155 config.disable_stream(RS2_STREAM_INFRARED);
156 config.enable_stream(RS2_STREAM_COLOR, opt_width, opt_height, RS2_FORMAT_RGBA8, opt_fps);
157 g.open(config);
158
159 g.acquire(I);
160
161 std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
162
163 vpCameraParameters cam = g.getCameraParameters(RS2_STREAM_COLOR,
165 );
166
167#if defined(VISP_HAVE_PUGIXML)
168 if (!opt_seqname.empty()) {
169 vpXmlParserCamera p;
170 std::string output_folder = vpIoTools::getParent(opt_seqname);
171 if (!vpIoTools::checkDirectory(output_folder)) {
172 try {
173 std::cout << "Create output folder: " << output_folder << std::endl;
174 vpIoTools::makeDirectory(output_folder);
175 }
176 catch (const vpException &e) {
177 std::cout << e.getStringMessage();
178 return EXIT_FAILURE;
179 }
180 }
181 std::string cam_filename = output_folder + "/camera.xml";
182
183 std::cout << "Save camera intrinsics in: " << cam_filename << std::endl;
184 if (p.save(cam, cam_filename, "camera") != vpXmlParserCamera::SEQUENCE_OK) {
185 std::cout << "Cannot save camera parameters in " << cam_filename << std::endl;
186 }
187 }
188#else
189 std::cout << "Warning: Unable to save camera parameters in xml since pugixml 3rdparty is not enabled" << std::endl;
190#endif
191
192 if (opt_display) {
193#if !(defined(VISP_HAVE_DISPLAY))
194 std::cout << "No image viewer is available..." << std::endl;
195 opt_display = false;
196#else
197#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
199#else
201#endif
202#endif
203 }
204
205 vpImageQueue<vpRGBa> image_queue(opt_seqname, opt_record_mode);
206 vpImageStorageWorker<vpRGBa> image_storage_worker(std::ref(image_queue));
207 std::thread image_storage_thread(&vpImageStorageWorker<vpRGBa>::run, &image_storage_worker);
208
209 bool quit = false;
210 while (!quit) {
211 double t = vpTime::measureTimeMs();
212 g.acquire(I);
213
215
216 quit = image_queue.record(I);
217
218 std::stringstream ss;
219 ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
220 vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
222 }
223 image_queue.cancel();
224 image_storage_thread.join();
225 }
226 catch (const vpException &e) {
227 std::cout << "Catch an exception: " << e << std::endl;
228 }
229#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
230 if (display != nullptr) {
231 delete display;
232 }
233#endif
234#else
235 (void)argc;
236 (void)argv;
237#if !defined(VISP_HAVE_REALSENSE2)
238 std::cout << "Install librealsense version > 2.31.0, configure and build ViSP again to use this example" << std::endl;
239#endif
240#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
241 std::cout << "This tutorial should be built with c++11 support" << std::endl;
242#endif
243#endif
244}
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithDistortion
Perspective projection with distortion model.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
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
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()