Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-grabber-realsense-T265.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/vpRealSense2.h>
7
8void usage(const char *argv[], int error)
9{
10 std::cout << "SYNOPSIS" << std::endl
11 << " " << argv[0] << " [--fps <6|15|30|60>]"
12 << " [--record <mode>]"
13 << " [--no-display]"
14 << " [--help] [-h]" << std::endl
15 << std::endl;
16 std::cout << "DESCRIPTION" << std::endl
17 << " --fps <6|15|30|60>" << std::endl
18 << " Frames per second." << std::endl
19 << " Default: 30." << std::endl
20 << std::endl
21 << " --record <mode>" << std::endl
22 << " Allowed values for mode are:" << std::endl
23 << " 0: record all the captures images (continuous mode)," << std::endl
24 << " 1: record only images selected by a user click (single shot mode)." << std::endl
25 << " Default mode: 0" << std::endl
26 << std::endl
27 << " --no-display" << std::endl
28 << " Disable displaying captured images." << std::endl
29 << " When used and sequence name specified, record mode is internally set to 1 (continuous mode)."
30 << std::endl
31 << std::endl
32 << " --help, -h" << std::endl
33 << " Print this helper message." << std::endl
34 << std::endl;
35 std::cout << "USAGE" << std::endl
36 << " Example to visualize images:" << std::endl
37 << " " << argv[0] << std::endl
38 << std::endl
39 << " Example to record a sequence of images:" << std::endl
40 << " " << argv[0] << " --record 0" << std::endl
41 << std::endl
42 << " Example to record single shot images:\n"
43 << " " << argv[0] << " --record 1" << std::endl
44 << std::endl;
45
46 if (error) {
47 std::cout << "Error" << std::endl
48 << " "
49 << "Unsupported parameter " << argv[error] << std::endl;
50 }
51}
52
56int main(int argc, const char *argv[])
57{
58#if defined(VISP_HAVE_REALSENSE2) && (RS2_API_VERSION > ((2 * 10000) + (31 * 100) + 0)) && defined(VISP_HAVE_THREADS)
59#ifdef ENABLE_VISP_NAMESPACE
60 using namespace VISP_NAMESPACE_NAME;
61#endif
62#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
63 std::shared_ptr<vpDisplay> display_left;
64 std::shared_ptr<vpDisplay> display_right;
65#else
66 vpDisplay *display_left = nullptr;
67 vpDisplay *display_right = nullptr;
68#endif
69 try {
70 std::string opt_seqname_left = "left-%04d.png", opt_seqname_right = "right-%04d.png";
71 int opt_record_mode = 0;
72 int opt_fps = 30;
73 bool opt_display = true;
74
75 for (int i = 1; i < argc; i++) {
76 if (std::string(argv[i]) == "--fps" && i + 1 < argc) {
77 opt_fps = std::atoi(argv[++i]);
78 }
79 else if (std::string(argv[i]) == "--record" && i + 1 < argc) {
80 opt_record_mode = std::atoi(argv[++i]);
81 }
82 else if (std::string(argv[i]) == "--no-display") {
83 opt_display = false;
84 }
85 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
86 usage(argv, 0);
87 return EXIT_SUCCESS;
88 }
89 else {
90 usage(argv, i);
91 return EXIT_FAILURE;
92 }
93 }
94
95 if (!opt_display) {
96 opt_record_mode = 0;
97 }
98
99 std::cout << "Framerate : " << opt_fps << std::endl;
100 std::cout << "Display : " << (opt_display ? "enabled" : "disabled") << std::endl;
101
102 std::string text_record_mode =
103 std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
104
105 std::cout << text_record_mode << std::endl;
106 std::cout << "Left record name: " << opt_seqname_left << std::endl;
107 std::cout << "Right record name: " << opt_seqname_right << std::endl;
108
109 vpImage<unsigned char> I_left, I_right;
110
111 vpRealSense2 g;
112 rs2::config config;
113 config.enable_stream(RS2_STREAM_FISHEYE, 1);
114 config.enable_stream(RS2_STREAM_FISHEYE, 2);
115 g.open(config);
116
117 g.acquire(&I_left, &I_right);
118
119 std::cout << "Image size : " << I_left.getWidth() << " " << I_right.getHeight() << std::endl;
120
121 if (opt_display) {
122#if !(defined(VISP_HAVE_DISPLAY))
123 std::cout << "No image viewer is available..." << std::endl;
124 opt_display = false;
125#else
126#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
127 display_left = vpDisplayFactory::createDisplay(I_left, 10, 10, "Left image");
128 display_right = vpDisplayFactory::createDisplay(I_right, I_left.getWidth(), 10, "Right image");
129#else
130 display_left = vpDisplayFactory::allocateDisplay(I_left, 10, 10, "Left image");
131 display_right = vpDisplayFactory::allocateDisplay(I_right, I_left.getWidth(), 10, "Right image");
132#endif
133#endif
134 }
135
136 vpImageQueue<unsigned char> image_queue_left(opt_seqname_left, opt_record_mode);
137 vpImageQueue<unsigned char> image_queue_right(opt_seqname_right, opt_record_mode);
138 vpImageStorageWorker<unsigned char> image_left_storage_worker(std::ref(image_queue_left));
139 vpImageStorageWorker<unsigned char> image_right_storage_worker(std::ref(image_queue_right));
140 std::thread image_left_storage_thread(&vpImageStorageWorker<unsigned char>::run, &image_left_storage_worker);
141 std::thread image_right_storage_thread(&vpImageStorageWorker<unsigned char>::run, &image_right_storage_worker);
142
143 bool quit = false;
144 while (!quit) {
145 double t = vpTime::measureTimeMs();
146
147 g.acquire(&I_left, &I_right);
148
149 vpDisplay::display(I_left);
150 vpDisplay::display(I_right);
151
152 quit = image_queue_left.record(I_left);
153 quit |= image_queue_right.record(I_right, nullptr, image_queue_left.getRecordingTrigger(), true);
154
155 std::stringstream ss;
156 ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
157 vpDisplay::displayText(I_left, I_left.getHeight() - 20, 10, ss.str(), vpColor::red);
158 vpDisplay::flush(I_left);
159 vpDisplay::flush(I_right);
160 }
161 image_queue_left.cancel();
162 image_queue_right.cancel();
163 image_left_storage_thread.join();
164 image_right_storage_thread.join();
165 }
166 catch (const vpException &e) {
167 std::cout << "Catch an exception: " << e << std::endl;
168 }
169
170#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
171 if (display_left != nullptr) {
172 delete display_left;
173 }
174 if (display_right != nullptr) {
175 delete display_right;
176 }
177#endif
178#else
179 (void)argc;
180 (void)argv;
181#if !(defined(VISP_HAVE_REALSENSE2) && (RS2_API_VERSION > ((2 * 10000) + (31 * 100) + 0)))
182 std::cout << "Install librealsense version > 2.31.0, configure and build ViSP again to use this example" << std::endl;
183#endif
184#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
185 std::cout << "This tutorial should be built with c++11 support" << std::endl;
186#endif
187#endif
188}
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
unsigned int getWidth() const
Definition vpImage.h:242
unsigned int getHeight() const
Definition vpImage.h:181
void acquire(vpImage< unsigned char > &grey, double *ts=nullptr)
bool open(const rs2::config &cfg=rs2::config())
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()