Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-pose-from-points-realsense-T265.cpp
1
2#include <visp3/core/vpConfig.h>
3#ifdef VISP_HAVE_MODULE_SENSOR
4#include <visp3/sensor/vpRealSense2.h>
5#endif
6#include <visp3/gui/vpDisplayFactory.h>
7
8#include "pose_helper.h"
9
10int main(int argc, char **argv)
11{
12#if defined(VISP_HAVE_DISPLAY) && \
13 defined(VISP_HAVE_REALSENSE2) && (RS2_API_VERSION > ((2 * 10000) + (31 * 100) + 0))
14#ifdef ENABLE_VISP_NAMESPACE
15 using namespace VISP_NAMESPACE_NAME;
16#endif
17#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
18 std::shared_ptr<vpDisplay> display;
19#else
20 vpDisplay *display = nullptr;
21#endif
22
23 try {
24 double opt_square_width = 0.12;
25 int opt_camera_index = 1; // camera index: 1. Left, 2.Right
26
27 for (int i = 1; i < argc; i++) {
28 if (std::string(argv[i]) == "--camera-index" && i + 1 < argc) {
29 opt_camera_index = atoi(argv[++i]);
30 }
31 else if (std::string(argv[i]) == "--square-width" && i + 1 < argc) {
32 opt_square_width = atoi(argv[++i]);
33 }
34 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
35 std::cout << "\nUsage: " << argv[0]
36 << " [--camera-index <1.Left | 2.Right> (default: 1)]"
37 << " [--square-width <square width in meter (default: 0.12)]"
38 << " [--help] [-h]\n"
39 << std::endl
40 << "\nExample using right camera and square size 0.1:\n"
41 << " " << argv[0] << "--camera-index 2 --square-width 0.1\n"
42 << std::endl;
43 return EXIT_SUCCESS;
44 }
45 }
46
48
50 std::cout << "Use Realsense 2 grabber" << std::endl;
52 rs2::config config;
53 config.enable_stream(RS2_STREAM_FISHEYE, 1);
54 config.enable_stream(RS2_STREAM_FISHEYE, 2);
55
56 g.open(config);
57 if (opt_camera_index == 1) // Left camera
58 g.acquire(&I, nullptr, nullptr);
59 else
60 g.acquire(nullptr, &I, nullptr);
61
62 std::cout << "Read camera parameters from Realsense device" << std::endl;
63 // Parameters of our camera
65 RS2_STREAM_FISHEYE, vpCameraParameters::ProjWithKannalaBrandtDistortion, opt_camera_index);
67
68 std::cout << "Square width : " << opt_square_width << std::endl;
69 std::cout << cam << std::endl;
70
71 // The pose container
73
74 std::vector<vpDot2> dot(4);
75 std::vector<vpPoint> point; // 3D coordinates of the points
76 std::vector<vpImagePoint> ip; // 2D coordinates of the points in pixels
77 double L = opt_square_width / 2.;
78 point.push_back(vpPoint(-L, -L, 0));
79 point.push_back(vpPoint(L, -L, 0));
80 point.push_back(vpPoint(L, L, 0));
81 point.push_back(vpPoint(-L, L, 0));
82
83#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
85#else
87#endif
88
89 bool quit = false;
90 bool apply_cv = false; // apply computer vision
91 bool init_cv = true; // initialize tracking and pose computation
92
93 while (!quit) {
94 double t_begin = vpTime::measureTimeMs();
95
96 if (opt_camera_index == 1)
97 g.acquire(&I, nullptr, nullptr);
98 else
99 g.acquire(nullptr, &I, nullptr);
100
102 if (apply_cv) {
103 try {
104 ip = track(I, dot, init_cv);
105 computePose(point, ip, cam, init_cv, cMo);
106 vpDisplay::displayFrame(I, cMo, cam, opt_square_width, vpColor::none, 3);
107 if (init_cv)
108 init_cv = false; // turn off the computer vision initialisation specific stuff
109
110 { // Display estimated pose in [m] and [deg]
111 vpPoseVector pose(cMo);
112 std::stringstream ss;
113 ss << "Translation: " << std::setprecision(5) << pose[0] << " " << pose[1] << " " << pose[2] << " [m]";
114 vpDisplay::displayText(I, 60, 20, ss.str(), vpColor::red);
115 ss.str(""); // erase ss
116 ss << "Rotation tu: " << std::setprecision(4) << vpMath::deg(pose[3]) << " " << vpMath::deg(pose[4]) << " "
117 << vpMath::deg(pose[5]) << " [deg]";
118 vpDisplay::displayText(I, 80, 20, ss.str(), vpColor::red);
119 }
120 }
121 catch (...) {
122 std::cout << "Computer vision failure." << std::endl;
123 apply_cv = false;
124 init_cv = true;
125 }
126 }
127 vpDisplay::displayText(I, 20, 20, "Right click: quit", vpColor::red);
128 if (apply_cv) {
129 vpDisplay::displayText(I, 40, 20, "Computer vision in progress...", vpColor::red);
130 }
131 else {
132 vpDisplay::displayText(I, 40, 20, "Left click : start", vpColor::red);
133 }
135 if (vpDisplay::getClick(I, button, false)) {
136 if (button == vpMouseButton::button3) {
137 quit = true;
138 }
139 else if (button == vpMouseButton::button1) {
140 apply_cv = true;
141 }
142 }
143 {
144 std::stringstream ss;
145 ss << "Time: " << vpTime::measureTimeMs() - t_begin << " ms";
146 vpDisplay::displayText(I, 20, I.getWidth() - 100, ss.str(), vpColor::red);
147 }
149 }
150 }
151 catch (const vpException &e) {
152 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
153 }
154
155#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
156 if (display != nullptr) {
157 delete display;
158 }
159#endif
160#elif !defined(VISP_HAVE_REALSENSE2)
161 (void)argc;
162 (void)argv;
163 std::cout << "You do not realsense2 SDK functionality enabled..." << std::endl;
164 std::cout << "Tip:" << std::endl;
165 std::cout << "- Install librealsense2, configure again ViSP using cmake and build again this example" << std::endl;
166 return EXIT_SUCCESS;
167#elif !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
168 (void)argc;
169 (void)argv;
170 std::cout << "Install a 3rd party dedicated to image display (X11, GDI, OpenCV), configure and build ViSP again to "
171 "use this example"
172 << std::endl;
173#elif !(RS2_API_VERSION > ((2 * 10000) + (31 * 100) + 0))
174 (void)argc;
175 (void)argv;
176 std::cout << "Install librealsense version > 2.31.0" << std::endl;
177#endif
178}
Generic class defining intrinsic camera parameters.
@ ProjWithKannalaBrandtDistortion
Projection with Kannala-Brandt distortion model.
static const vpColor red
Definition vpColor.h:198
static const vpColor none
Definition vpColor.h:210
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color=vpColor::none, unsigned int thickness=1, const vpImagePoint &offset=vpImagePoint(0, 0), const std::string &frameName="", const vpColor &textColor=vpColor::black, const vpImagePoint &textOffset=vpImagePoint(15, 15))
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
Implementation of an homogeneous matrix and operations on such kind of matrices.
Definition of the vpImage class member functions.
Definition vpImage.h:131
static double deg(double rad)
Definition vpMath.h:119
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition vpPoint.h:79
Implementation of a pose vector and operations on poses.
vpCameraParameters getCameraParameters(const rs2_stream &stream, vpCameraParameters::vpCameraParametersProjType type=vpCameraParameters::perspectiveProjWithDistortion, int index=-1) const
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()