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