Visual Servoing Platform  version 3.6.1 under development (2024-05-02)
testRealSense2_T265_undistort.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Image acquisition with RealSense T265 sensor and librealsense2 and
33  * undistorting it using vpImageTools
34  *
35 *****************************************************************************/
36 
43 #include <iostream>
44 
45 #include <visp3/core/vpImageConvert.h>
46 #include <visp3/core/vpImageTools.h>
47 #include <visp3/gui/vpDisplayGDI.h>
48 #include <visp3/gui/vpDisplayX.h>
49 #include <visp3/sensor/vpRealSense2.h>
50 
51 #if defined(VISP_HAVE_REALSENSE2) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) && \
52  (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)) && (RS2_API_VERSION > ((2 * 10000) + (31 * 100) + 0))
53 
54 int main()
55 {
56  try {
57  vpCameraParameters cam_left;
58  unsigned int display_scale = 2;
59  vpRealSense2 rs;
60  std::string product_line = rs.getProductLine();
61  std::cout << "Product line: " << product_line << std::endl;
62 
63  if (product_line != "T200") {
64  std::cout << "This example doesn't support devices that are not part of T200 product line family !" << std::endl;
65  return EXIT_SUCCESS;
66  }
67  int cam_index = 1; // Left fisheye camera
68  // Both streams should be enabled.
69  // Note: It is not currently possible to enable only one
70  rs2::config config;
71  config.enable_stream(RS2_STREAM_FISHEYE, 1, RS2_FORMAT_Y8);
72  config.enable_stream(RS2_STREAM_FISHEYE, 2, RS2_FORMAT_Y8);
73  rs.open(config);
74  cam_left =
76 
77  vpImage<unsigned char> I((unsigned int)rs.getIntrinsics(RS2_STREAM_FISHEYE).height,
78  (unsigned int)rs.getIntrinsics(RS2_STREAM_FISHEYE).width);
79 
80  vpImage<unsigned char> I_undist((unsigned int)rs.getIntrinsics(RS2_STREAM_FISHEYE).height,
81  (unsigned int)rs.getIntrinsics(RS2_STREAM_FISHEYE).width);
82 
83  std::cout << "Left fisheye camera parameters: " << cam_left << std::endl;
84 
85 #if defined(VISP_HAVE_X11)
86  vpDisplayX d;
87  vpDisplayX d_undist;
88 #elif defined(VISP_HAVE_GDI)
89  vpDisplayGDI d;
90  vpDisplayGDI d_undist;
91 #endif
92 
93 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
94  d.setDownScalingFactor(display_scale);
95  d_undist.setDownScalingFactor(display_scale);
96  d.init(I, 10, 10, "Left image");
97  d_undist.init(I_undist, I.getWidth() / display_scale + 80, 10, "Undistorted image");
98 #endif
99 
100  vpArray2D<int> mapU, mapV;
101  vpArray2D<float> mapDu, mapDv;
102 
103  vpImageTools::initUndistortMap(cam_left, I.getWidth(), I.getHeight(), mapU, mapV, mapDu, mapDv);
104 
105  while (true) {
106  double t = vpTime::measureTimeMs();
107 
108  rs.acquire(&I, nullptr, nullptr); // Acquire only left image
109 
111 
112  vpImageTools::undistort(I, mapU, mapV, mapDu, mapDv, I_undist);
113  vpDisplay::display(I_undist);
114 
115  vpDisplay::displayText(I, 15 * display_scale, 15 * display_scale, "Click to quit", vpColor::red);
116  vpDisplay::displayText(I_undist, 15 * display_scale, 15 * display_scale, "Click to quit", vpColor::red);
117 
118  if (vpDisplay::getClick(I, false) || vpDisplay::getClick(I_undist, false))
119  break;
120 
121  vpDisplay::flush(I);
122  vpDisplay::flush(I_undist);
123 
124  std::cout << "Loop time: " << vpTime::measureTimeMs() - t << std::endl;
125  }
126 
127  }
128  catch (const vpException &e) {
129  std::cerr << "RealSense error " << e.what() << std::endl;
130  }
131  catch (const std::exception &e) {
132  std::cerr << e.what() << std::endl;
133  }
134 
135  return EXIT_SUCCESS;
136 }
137 #else
138 int main()
139 {
140 #if !defined(VISP_HAVE_REALSENSE2)
141  std::cout << "You do not realsense2 SDK functionality enabled..." << std::endl;
142  std::cout << "Tip:" << std::endl;
143  std::cout << "- Install librealsense2, configure again ViSP using cmake and build again this example" << std::endl;
144  return EXIT_SUCCESS;
145 #elif !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI))
146  std::cout << "You don't have X11 or GDI display capabilities" << std::endl;
147 #elif !(RS2_API_VERSION > ((2 * 10000) + (31 * 100) + 0))
148  std::cout << "Install librealsense version > 2.31.0" << std::endl;
149 #endif
150  return EXIT_SUCCESS;
151 }
152 #endif
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:126
Generic class defining intrinsic camera parameters.
@ ProjWithKannalaBrandtDistortion
Projection with Kannala-Brandt distortion model.
static const vpColor red
Definition: vpColor.h:211
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="") vp_override
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
virtual void setDownScalingFactor(unsigned int scale)
Definition: vpDisplay.cpp:227
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:59
const char * what() const
Definition: vpException.cpp:70
static void initUndistortMap(const vpCameraParameters &cam, unsigned int width, unsigned int height, vpArray2D< int > &mapU, vpArray2D< int > &mapV, vpArray2D< float > &mapDu, vpArray2D< float > &mapDv)
static void undistort(const vpImage< Type > &I, const vpCameraParameters &cam, vpImage< Type > &newI, unsigned int nThreads=2)
Definition: vpImageTools.h:654
unsigned int getWidth() const
Definition: vpImage.h:245
unsigned int getHeight() const
Definition: vpImage.h:184
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())
rs2_intrinsics getIntrinsics(const rs2_stream &stream, int index=-1) const
std::string getProductLine()
VISP_EXPORT double measureTimeMs()