Main MRPT website > C++ reference for 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 }
mrpt::hwdrivers::CSwissRanger3DCamera::isEnabledImageHistEqualization
bool isEnabledImageHistEqualization() const
Definition: CSwissRanger3DCamera.h:201
mrpt::obs::CObservation3DRangeScan::hasPoints3D
bool hasPoints3D
true means the field points3D contains valid data.
Definition: CObservation3DRangeScan.h:396
mrpt::maps::CColouredPointsMap
A map of 2D/3D points with individual colours (RGB).
Definition: CColouredPointsMap.h:29
mrpt::maps::CColouredPointsMap::colorScheme
TColourOptions colorScheme
The options employed when inserting laser scans in the map.
Definition: CColouredPointsMap.h:254
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:19
mrpt::opengl::CTexturedPlane::Ptr
std::shared_ptr< CTexturedPlane > Ptr
Definition: CTexturedPlane.h:24
mrpt::hwdrivers::CSwissRanger3DCamera::setSave3D
void setSave3D(bool save)
Definition: CSwissRanger3DCamera.h:190
mrpt::hwdrivers::CSwissRanger3DCamera::isEnabledMedianFilter
bool isEnabledMedianFilter() const
Definition: CSwissRanger3DCamera.h:211
mrpt::maps::CColouredPointsMap::TColourOptions::scheme
TColouringMethod scheme
Definition: CColouredPointsMap.h:248
mrpt::hwdrivers::CSwissRanger3DCamera::enableConvGray
void enableConvGray(bool enable)
Definition: CSwissRanger3DCamera.h:222
CPointCloudColoured.h
mrpt::hwdrivers::CSwissRanger3DCamera::getMesaLibVersion
bool getMesaLibVersion(std::string &out_version) const
Get the version of the MESA library.
Definition: CSwissRanger3DCamera.cpp:235
mrpt::hwdrivers
Contains classes for various device interfaces.
Definition: C2DRangeFinderAbstract.h:22
mrpt::hwdrivers::CSwissRanger3DCamera::getMaxRange
double getMaxRange() const
Returns the maximum camera range, as deduced from its operating frequency.
Definition: CSwissRanger3DCamera.h:172
mrpt::hwdrivers::CSwissRanger3DCamera::setOpenFromUSB
void setOpenFromUSB(bool USB)
true: open from USB, false: open from ethernet.
Definition: CSwissRanger3DCamera.h:186
CObservation3DRangeScan.h
mrpt::hwdrivers::CSwissRanger3DCamera::rows
size_t rows() const
Get the row count in the camera images, loaded automatically upon camera open().
Definition: CSwissRanger3DCamera.h:163
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
stock_objects.h
mrpt::opengl::CPointCloudColoured::Ptr
std::shared_ptr< CPointCloudColoured > Ptr
Definition: CPointCloudColoured.h:49
mrpt::opengl::MRPT_GLUT_BITMAP_HELVETICA_12
@ MRPT_GLUT_BITMAP_HELVETICA_12
Definition: opengl_fonts.h:31
mrpt::obs
This namespace contains representation of robot actions and observations.
Definition: CParticleFilter.h:17
mrpt::hwdrivers::CSwissRanger3DCamera::initialize
virtual void initialize()
Initializes the 3D camera - should be invoked after calling loadConfig()
Definition: CSwissRanger3DCamera.cpp:135
mrpt::obs::CObservation3DRangeScan::cameraParams
mrpt::img::TCamera cameraParams
Projection parameters of the depth camera.
Definition: CObservation3DRangeScan.h:719
mrpt::opengl::MRPT_GLUT_BITMAP_HELVETICA_18
@ MRPT_GLUT_BITMAP_HELVETICA_18
Definition: opengl_fonts.h:32
mrpt::hwdrivers::CSwissRanger3DCamera::setSaveIntensityImage
void setSaveIntensityImage(bool save)
Definition: CSwissRanger3DCamera.h:192
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
mrpt::math::CMatrixTemplateNumeric
A matrix of dynamic size.
Definition: CMatrixTemplateNumeric.h:37
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::img
Definition: CCanvas.h:17
mrpt::obs::CObservation3DRangeScan::intensityImage
mrpt::img::CImage intensityImage
If hasIntensityImage=true, a color or gray-level intensity image of the same size than "rangeImage".
Definition: CObservation3DRangeScan.h:502
mrpt::obs::CObservation3DRangeScan
Declares a class derived from "CObservation" that encapsules a 3D range scan measurement,...
Definition: CObservation3DRangeScan.h:224
mrpt::hwdrivers::CSwissRanger3DCamera::enableDenoiseANF
void enableDenoiseANF(bool enable)
Definition: CSwissRanger3DCamera.h:228
mrpt::format
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
mrpt::hwdrivers::CSwissRanger3DCamera::isOpen
bool isOpen() const
whether the camera is open and comms work ok.
Definition: CSwissRanger3DCamera.cpp:250
mrpt::obs::CObservation3DRangeScan::hasIntensityImage
bool hasIntensityImage
true means the field intensityImage contains valid data
Definition: CObservation3DRangeScan.h:499
mrpt::img::TColorf
A RGB color - floats in the range [0,1].
Definition: TColor.h:79
mrpt::obs::CObservation3DRangeScan::hasRangeImage
bool hasRangeImage
true means the field rangeImage contains valid data
Definition: CObservation3DRangeScan.h:441
mrpt::hwdrivers::CSwissRanger3DCamera::enableMedianFilter
void enableMedianFilter(bool enable)
Definition: CSwissRanger3DCamera.h:206
mrpt::hwdrivers::CSwissRanger3DCamera::setSaveConfidenceImage
void setSaveConfidenceImage(bool save)
Definition: CSwissRanger3DCamera.h:196
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
mrpt::hwdrivers::CSwissRanger3DCamera::isEnabledConvGray
bool isEnabledConvGray() const
Definition: CSwissRanger3DCamera.h:227
mrpt::hwdrivers::CSwissRanger3DCamera::isEnabledDenoiseANF
bool isEnabledDenoiseANF() const
Definition: CSwissRanger3DCamera.h:233
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
mrpt::hwdrivers::CSwissRanger3DCamera::getCameraSerialNumber
unsigned int getCameraSerialNumber() const
Get the camera serial number, loaded automatically upon camera open().
Definition: CSwissRanger3DCamera.h:169
mrpt::img::CImage
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
mrpt::opengl::stock_objects::CornerXYZ
CSetOfObjects::Ptr CornerXYZ(float scale=1.0)
Returns three arrows representing a X,Y,Z 3D corner.
Definition: StockObjects.cpp:209
mrpt::hwdrivers::CSwissRanger3DCamera::enableImageHistEqualization
void enableImageHistEqualization(bool enable)
Definition: CSwissRanger3DCamera.h:197
mrpt::obs::CObservation3DRangeScan::rangeImage
mrpt::math::CMatrix rangeImage
If hasRangeImage=true, a matrix of floats with the range data as captured by the camera (in meters)
Definition: CObservation3DRangeScan.h:444
img
GLint GLvoid * img
Definition: glext.h:3763
CTicTac.h
mrpt::maps::CColouredPointsMap::loadFromRangeScan
virtual void loadFromRangeScan(const mrpt::obs::CObservation2DRangeScan &rangeScan, const mrpt::poses::CPose3D *robotPose=nullptr) override
See CPointsMap::loadFromRangeScan()
Definition: CColouredPointsMap.cpp:1075
mrpt::hwdrivers::CSwissRanger3DCamera::setSaveRangeImage
void setSaveRangeImage(bool save)
Definition: CSwissRanger3DCamera.h:191
mrpt::hwdrivers::CSwissRanger3DCamera::cols
size_t cols() const
Get the col count in the camera images, loaded automatically upon camera open().
Definition: CSwissRanger3DCamera.h:166
mrpt::hwdrivers::CSwissRanger3DCamera
A class for grabing "range images" from a MESA imaging SwissRanger 3D cameras (SR-2,...
Definition: CSwissRanger3DCamera.h:112
mrpt::hwdrivers::CSwissRanger3DCamera::getNextObservation
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().
Definition: CSwissRanger3DCamera.cpp:364
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
gui.h
mrpt::img::CImage::rectifyImage
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
CColouredPointsMap.h
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::opengl::COpenGLScene::Ptr
std::shared_ptr< COpenGLScene > Ptr
Definition: COpenGLScene.h:61
mrpt::maps
Definition: CBeacon.h:24
Test_SwissRanger
void Test_SwissRanger()
Definition: vision_stereo_rectify/test.cpp:36
CGridPlaneXY.h
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
mrpt::opengl::COpenGLViewport::Ptr
std::shared_ptr< COpenGLViewport > Ptr
Definition: COpenGLViewport.h:63
mrpt::gui::CDisplayWindow3D
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
Definition: CDisplayWindow3D.h:117
TColor.h
CTexturedPlane.h
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25
CSwissRanger3DCamera.h



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST