MRPT  1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
11 #include <mrpt/gui.h>
18 #include <mrpt/system/CTicTac.h>
19 #include <mrpt/img/TColor.h>
20 #include <iostream>
21 
22 using namespace mrpt;
23 using namespace mrpt::hwdrivers;
24 using namespace mrpt::math;
25 using namespace mrpt::gui;
26 using namespace mrpt::maps;
27 using namespace mrpt::obs;
28 using namespace mrpt::opengl;
29 using namespace mrpt::system;
30 using namespace mrpt::img;
31 using namespace std;
32 
33 // ------------------------------------------------------
34 // Test_SwissRanger
35 // ------------------------------------------------------
36 void Test_SwissRanger()
37 {
39 
40  // Set params:
41  cam.setOpenFromUSB(true);
42 
43  cam.setSave3D(true);
44  cam.setSaveRangeImage(true);
45  cam.setSaveIntensityImage(true);
46  cam.setSaveConfidenceImage(false);
47 
48  // cam.enablePreviewWindow(true);
49 
50  // Open:
51  cam.initialize();
52 
53  if (cam.isOpen())
54  cout << "[Test_SwissRanger] Camera open, serial #"
55  << cam.getCameraSerialNumber() << " resolution: " << cam.cols()
56  << "x" << cam.rows() << " max. range: " << cam.getMaxRange()
57  << endl;
58 
59  const double aspect_ratio = cam.rows() / double(cam.cols());
60 
61  {
62  std::string ver;
63  cam.getMesaLibVersion(ver);
64  cout << "[Test_SwissRanger] Version: " << ver << "\n";
65  }
66 
68  bool there_is_obs = true, hard_error;
69 
70  mrpt::gui::CDisplayWindow3D win3D("3D camera view", 800, 600);
71 
72  win3D.setCameraAzimuthDeg(140);
73  win3D.setCameraElevationDeg(20);
74  win3D.setCameraZoom(6.0);
75  win3D.setCameraPointingToPoint(2.5, 0, 0);
76 
77  // mrpt::gui::CDisplayWindow win2D("2D range image",200,200);
78  // mrpt::gui::CDisplayWindow winInt("Intensity range image",200,200);
79  // win2D.setPos(10,10);
80  // winInt.setPos(350,10);
81  // win3D.setPos(10,290);
82  // win3D.resize(400,200);
83 
84  // mrpt::opengl::CPointCloud::Ptr gl_points =
85  // mrpt::make_aligned_shared<mrpt::opengl::CPointCloud>();
87  mrpt::make_aligned_shared<mrpt::opengl::CPointCloudColoured>();
88  gl_points->setPointSize(4.5);
89 
91  mrpt::make_aligned_shared<mrpt::opengl::CTexturedPlane>(
92  0.5, -0.5, -0.5 * aspect_ratio, 0.5 * aspect_ratio);
93  mrpt::opengl::CTexturedPlane::Ptr gl_img_intensity =
94  mrpt::make_aligned_shared<mrpt::opengl::CTexturedPlane>(
95  0.5, -0.5, -0.5 * aspect_ratio, 0.5 * aspect_ratio);
96  mrpt::opengl::CTexturedPlane::Ptr gl_img_intensity_rect =
97  mrpt::make_aligned_shared<mrpt::opengl::CTexturedPlane>(
98  0.5, -0.5, -0.5 * aspect_ratio, 0.5 * aspect_ratio);
99 
100  {
101  mrpt::opengl::COpenGLScene::Ptr& scene = win3D.get3DSceneAndLock();
102 
103  // Create the Opengl object for the point cloud:
104  scene->insert(gl_points);
105  scene->insert(mrpt::make_aligned_shared<mrpt::opengl::CGridPlaneXY>());
107 
108  const int VW_WIDTH = 200;
109  const int VW_HEIGHT = 150;
110  const int VW_GAP = 10;
111 
112  // Create the Opengl objects for the planar images, as textured planes,
113  // each in a separate viewport:
114  win3D.addTextMessage(
115  30, -10 - 1 * (VW_GAP + VW_HEIGHT), "Range data", TColorf(1, 1, 1),
117  opengl::COpenGLViewport::Ptr viewRange =
118  scene->createViewport("view2d_range");
119  scene->insert(gl_img_range, "view2d_range");
120  viewRange->setViewportPosition(
121  5, -10 - 1 * (VW_GAP + VW_HEIGHT), VW_WIDTH, VW_HEIGHT);
122  viewRange->setTransparent(true);
123  viewRange->getCamera().setOrthogonal(true);
124  viewRange->getCamera().setAzimuthDegrees(90);
125  viewRange->getCamera().setElevationDegrees(90);
126  viewRange->getCamera().setZoomDistance(1.0);
127 
128  win3D.addTextMessage(
129  30, -10 - 2 * (VW_GAP + VW_HEIGHT), "Intensity data",
132  scene->createViewport("view2d_int");
133  scene->insert(gl_img_intensity, "view2d_int");
134  viewInt->setViewportPosition(
135  5, -10 - 2 * (VW_GAP + VW_HEIGHT), VW_WIDTH, VW_HEIGHT);
136  viewInt->setTransparent(true);
137  viewInt->getCamera().setOrthogonal(true);
138  viewInt->getCamera().setAzimuthDegrees(90);
139  viewInt->getCamera().setElevationDegrees(90);
140  viewInt->getCamera().setZoomDistance(1.0);
141 
142  win3D.addTextMessage(
143  30, -10 - 3 * (VW_GAP + VW_HEIGHT), "Intensity data (undistorted)",
145  opengl::COpenGLViewport::Ptr viewIntRect =
146  scene->createViewport("view2d_intrect");
147  scene->insert(gl_img_intensity_rect, "view2d_intrect");
148  viewIntRect->setViewportPosition(
149  5, -10 - 3 * (VW_GAP + VW_HEIGHT), VW_WIDTH, VW_HEIGHT);
150  viewIntRect->setTransparent(true);
151  viewIntRect->getCamera().setOrthogonal(true);
152  viewIntRect->getCamera().setAzimuthDegrees(90);
153  viewIntRect->getCamera().setElevationDegrees(90);
154  viewIntRect->getCamera().setZoomDistance(1.0);
155 
156  win3D.unlockAccess3DScene();
157  win3D.repaint();
158  }
159 
160  CTicTac tictac;
161  size_t nImgs = 0;
162 
163  bool endLoop = false;
164 
165  while (there_is_obs && !endLoop && win3D.isOpen())
166  {
167  // Grab new observation from the camera:
168  cam.getNextObservation(obs, there_is_obs, hard_error);
169 
170  // Show ranges as 2D:
171  if (there_is_obs && obs.hasRangeImage)
172  {
174  // Normalize the image
175  CMatrixFloat range2D = obs.rangeImage;
176  range2D *= 1.0 / cam.getMaxRange();
177  img.setFromMatrix(range2D);
178 
179  win3D.get3DSceneAndLock();
180  gl_img_range->assignImage_fast(img);
181  win3D.unlockAccess3DScene();
182  }
183 
184  // Show intensity image:
185  if (there_is_obs && obs.hasIntensityImage)
186  {
187  win3D.get3DSceneAndLock();
188  gl_img_intensity->assignImage(obs.intensityImage);
189 
190  CImage undistortImg;
191  obs.intensityImage.rectifyImage(undistortImg, obs.cameraParams);
192  gl_img_intensity_rect->assignImage(undistortImg);
193  win3D.unlockAccess3DScene();
194  }
195 
196  // Show 3D points:
197  if (there_is_obs && obs.hasPoints3D)
198  {
199  // mrpt::maps::CSimplePointsMap pntsMap;
200  CColouredPointsMap pntsMap;
201  pntsMap.colorScheme.scheme =
202  CColouredPointsMap::cmFromIntensityImage;
203  pntsMap.loadFromRangeScan(obs);
204 
205  win3D.get3DSceneAndLock();
206  gl_points->loadFromPointsMap(&pntsMap);
207  win3D.unlockAccess3DScene();
208  win3D.repaint();
209  }
210 
211  nImgs++;
212  if (nImgs > 10)
213  {
214  win3D.get3DSceneAndLock();
215  win3D.addTextMessage(
216  0.01, 0.01, format("%.02f Hz", nImgs / tictac.Tac()),
217  TColorf(0, 1, 1), 100, MRPT_GLUT_BITMAP_HELVETICA_12);
218  win3D.unlockAccess3DScene();
219  nImgs = 0;
220  tictac.Tic();
221  }
222 
223  // Process possible keyboard commands:
224  // --------------------------------------
225  if (win3D.keyHit())
226  {
227  const int key = tolower(win3D.getPushedKey());
228  // cout << "key: " << key << endl;
229 
230  switch (key)
231  {
232  case 'h':
235  break;
236  case 'g':
237  cam.enableConvGray(!cam.isEnabledConvGray());
238  break;
239  case 'd':
241  break;
242  case 'f':
244  break;
245  case 27:
246  endLoop = true;
247  break;
248  }
249  }
250 
251  win3D.get3DSceneAndLock();
252  win3D.addTextMessage(
253  0.08, 0.02,
254  format(
255  "Keyboard switches: H (hist.equal: %s) | G (convGray: %s) | D "
256  "(denoise: %s) | F (medianFilter: %s)",
257  cam.isEnabledImageHistEqualization() ? "ON" : "OFF",
258  cam.isEnabledConvGray() ? "ON" : "OFF",
259  cam.isEnabledDenoiseANF() ? "ON" : "OFF",
260  cam.isEnabledMedianFilter() ? "ON" : "OFF"),
261  TColorf(0, 0, 1), 110, MRPT_GLUT_BITMAP_HELVETICA_18);
262  win3D.unlockAccess3DScene();
263 
264  std::this_thread::sleep_for(1ms);
265  }
266 }
267 
268 int main(int argc, char** argv)
269 {
270  try
271  {
273  return 0;
274  }
275  catch (std::exception& e)
276  {
277  std::cout << "EXCEPCION: " << e.what() << std::endl;
278  return -1;
279  }
280  catch (...)
281  {
282  printf("Another exception!!");
283  return -1;
284  }
285 }
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
mrpt::img::TCamera cameraParams
Projection parameters of the depth camera.
void setOpenFromUSB(bool USB)
true: open from USB, false: open from ethernet.
A class for grabing "range images" from a MESA imaging SwissRanger 3D cameras (SR-2, SR-3000, SR-4k).
A high-performance stopwatch, with typical resolution of nanoseconds.
Declares a class derived from "CObservation" that encapsules a 3D range scan measurement, as from a time-of-flight range camera or any other RGBD sensor.
double getMaxRange() const
Returns the maximum camera range, as deduced from its operating frequency.
Contains classes for various device interfaces.
STL namespace.
virtual void initialize()
Initializes the 3D camera - should be invoked after calling loadConfig()
TColourOptions colorScheme
The options employed when inserting laser scans in the map.
mrpt::math::CMatrix rangeImage
If hasRangeImage=true, a matrix of floats with the range data as captured by the camera (in meters) ...
This base provides a set of functions for maths stuff.
bool getMesaLibVersion(std::string &out_version) const
Get the version of the MESA library.
GLint GLvoid * img
Definition: glext.h:3763
size_t rows() const
Get the row count in the camera images, loaded automatically upon camera open().
mrpt::img::CImage intensityImage
If hasIntensityImage=true, a color or gray-level intensity image of the same size than "rangeImage"...
This namespace contains representation of robot actions and observations.
bool hasRangeImage
true means the field rangeImage contains valid data
A map of 2D/3D points with individual colours (RGB).
bool hasPoints3D
true means the field points3D contains valid data.
GLsizei const GLchar ** string
Definition: glext.h:4101
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void rectifyImage(CImage &out_img, const mrpt::img::TCamera &cameraParams) const
Rectify (un-distort) the image according to some camera parameters, and returns an output un-distorte...
Definition: CImage.cpp:2082
bool isOpen() const
whether the camera is open and comms work ok.
CSetOfObjects::Ptr CornerXYZ(float scale=1.0)
Returns three arrows representing a X,Y,Z 3D corner.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
virtual void loadFromRangeScan(const mrpt::obs::CObservation2DRangeScan &rangeScan, const mrpt::poses::CPose3D *robotPose=nullptr) override
See CPointsMap::loadFromRangeScan()
bool hasIntensityImage
true means the field intensityImage contains valid data
A RGB color - floats in the range [0,1].
Definition: TColor.h:77
void getNextObservation(mrpt::obs::CObservation3DRangeScan &out_obs, bool &there_is_obs, bool &hardware_error)
The main data retrieving function, to be called after calling loadConfig() and initialize().
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
void Test_SwissRanger()
unsigned int getCameraSerialNumber() const
Get the camera serial number, loaded automatically upon camera open().
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
size_t cols() const
Get the col count in the camera images, loaded automatically upon camera open().
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020