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 
10 // -----------------------------------------------------------------------------------------------------------------------
11 // For this example, the rawlog file must contain both laser data and stereo
12 // images (only the left one will be considered)
13 // It may be used with single image observations -> just employ
14 // "CObservationImage::Ptr" instead of "CObservationStereoImages::Ptr"
15 // and access to the contained "image" instead of "imageLeft".
16 // -----------------------------------------------------------------------------------------------------------------------
17 
19 #include <mrpt/system/filesystem.h>
20 #include <mrpt/system/os.h>
21 #include <mrpt/obs/CRawlog.h>
24 #include <mrpt/obs/CSensoryFrame.h>
30 #include <iostream>
31 
32 using namespace mrpt;
33 using namespace mrpt::obs;
34 using namespace mrpt::maps;
35 using namespace mrpt::gui;
36 using namespace mrpt::system;
37 using namespace mrpt::math;
38 using namespace mrpt::poses;
39 using namespace mrpt::img;
40 using namespace std;
41 
42 #include <mrpt/examples_config.h>
43 
44 // Default path, or user path passed thru command line:
45 std::string RAWLOG_FILE = MRPT_EXAMPLES_BASE_DIRECTORY
46  "../share/mrpt/datasets/localization_demo.rawlog";
47 
48 // ------------------------------------------------------
49 // TestGeometry3D
50 // ------------------------------------------------------
51 void TestLaser2Imgs()
52 {
53  // Set your rawlog file name
56  "Rawlog file does not exist: %s", RAWLOG_FILE.c_str())
57 
58  CActionCollection::Ptr action;
59  CSensoryFrame::Ptr observations;
60  size_t rawlogEntry = 0;
61  // bool end = false;
62  CDisplayWindow wind;
63 
64  // Set relative path for externally-stored images in rawlogs:
65  string rawlog_images_path = extractFileDirectory(RAWLOG_FILE);
66  rawlog_images_path += "/Images";
67  CImage::setImagesPathBase(rawlog_images_path); // Set it.
68 
69  mrpt::io::CFileGZInputStream rawlogFile(RAWLOG_FILE);
70 
71  for (;;)
72  {
73  if (os::kbhit())
74  {
75  char c = os::getch();
76  if (c == 27) break;
77  }
78 
79  // Load action/observation pair from the rawlog:
80  // --------------------------------------------------
81  cout << "Reading act/oct pair from rawlog..." << endl;
82  auto arch = mrpt::serialization::archiveFrom(rawlogFile);
84  arch, action, observations, rawlogEntry))
85  break; // file EOF
86 
87  // CAMERA............
88  // Get CObservationStereoImages
91 
92  sImgs = observations->getObservationByClass<CObservationStereoImages>();
93  if (!sImgs)
94  {
95  Img = observations->getObservationByClass<CObservationImage>();
96  if (!Img) continue;
97  }
98 
99  CPose3D cameraPose; // Get Camera Pose (B) (CPose3D)
100  CMatrixDouble33 K; // Get Calibration matrix (K)
101 
102  sImgs ? sImgs->getSensorPose(cameraPose)
103  : Img->getSensorPose(cameraPose);
104  K = sImgs ? sImgs->leftCamera.intrinsicParams
105  : Img->cameraParams.intrinsicParams;
106 
107  // LASER.............
108  // Get CObservationRange2D
109  CObservation2DRangeScan::Ptr laserScan =
110  observations->getObservationByClass<CObservation2DRangeScan>();
111  if (!laserScan) continue;
112 
113  // Get Laser Pose (A) (CPose3D)
114  CPose3D laserPose;
115  laserScan->getSensorPose(laserPose);
116 
117  if (abs(laserPose.yaw()) > DEG2RAD(90)) continue; // Only front lasers
118 
119  // Get 3D Point relative to the Laser coordinate Frame (P1) (CPoint3D)
120  CPoint3D point;
121  CSimplePointsMap mapa;
123  observations->insertObservationsInto(
124  &mapa); // <- The map contains the pose of the points (P1)
125 
126  // Get the points into the map
127  vector<float> X, Y, Z;
128  vector<float>::iterator itX, itY, itZ;
129  mapa.getAllPoints(X, Y, Z);
130 
131  unsigned int imgW =
132  sImgs ? sImgs->imageLeft.getWidth() : Img->image.getWidth();
133  unsigned int imgH =
134  sImgs ? sImgs->imageLeft.getHeight() : Img->image.getHeight();
135 
136  // unsigned int idx = 0;
137  vector<float> imgX, imgY;
138  vector<float>::iterator itImgX, itImgY;
139  imgX.resize(X.size());
140  imgY.resize(Y.size());
141 
142  CImage image;
143  image = sImgs ? sImgs->imageLeft : Img->image;
144 
145  // Get pixels in the image:
146  // Pimg = (kx,ky,k)^T = K(I|0)*P2
147  // Main loop
148  for (itX = X.begin(), itY = Y.begin(), itZ = Z.begin(),
149  itImgX = imgX.begin(), itImgY = imgY.begin();
150  itX != X.end(); itX++, itY++, itZ++, itImgX++, itImgY++)
151  {
152  // Coordinates Transformation
153  CPoint3D pLaser(*itX, *itY, *itZ);
154  CPoint3D pCamera(pLaser - cameraPose);
155 
156  if (pCamera.z() > 0)
157  {
158  *itImgX = -K(0, 0) * ((pCamera.x()) / (pCamera.z())) + K(0, 2);
159  *itImgY = -K(1, 1) * ((pCamera.y()) / (pCamera.z())) + K(1, 2);
160 
161  if (*itImgX > 0 && *itImgX<imgW&& * itImgY> 0 && *itImgY < imgH)
162  image.filledRectangle(
163  *itImgX - 1, *itImgY - 1, *itImgX + 1, *itImgY + 1,
164  TColor(255, 0, 0));
165  } // end if
166  } // end for
167 
168  action.reset();
169  observations.reset();
170 
171  wind.showImage(image);
172 
173  std::this_thread::sleep_for(50ms);
174  }; // end for
175 
177 }
178 
179 // ------------------------------------------------------
180 // MAIN
181 // ------------------------------------------------------
182 int main(int argc, char** argv)
183 {
184  try
185  {
186  if (argc > 1)
187  {
188  RAWLOG_FILE = std::string(argv[1]);
189  }
190 
191  TestLaser2Imgs();
192  return 0;
193  }
194  catch (exception& e)
195  {
196  cerr << "EXCEPTION: " << e.what() << endl;
197  return -1;
198  }
199  catch (...)
200  {
201  cerr << "Untyped exception!!";
202  return -1;
203  }
204 }
mrpt::system::os::kbhit
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
Definition: os.cpp:390
os.h
filesystem.h
mrpt::system::extractFileDirectory
std::string extractFileDirectory(const std::string &filePath)
Extract the whole path (the directory) of a filename from a complete path plus name plus extension.
Definition: filesystem.cpp:77
mrpt::obs::CRawlog::readActionObservationPair
static bool readActionObservationPair(mrpt::serialization::CArchive &inStream, CActionCollection::Ptr &action, CSensoryFrame::Ptr &observations, size_t &rawlogEntry)
Reads a consecutive pair action / observation from the rawlog opened at some input stream.
Definition: CRawlog.cpp:314
mrpt::obs::CObservation2DRangeScan
A "CObservation"-derived class that represents a 2D range scan measurement (typically from a laser sc...
Definition: CObservation2DRangeScan.h:56
RAWLOG_FILE
std::string RAWLOG_FILE
Definition: vision_stereo_rectify/test.cpp:45
mrpt::maps::CPointsMap::TInsertionOptions::minDistBetweenLaserPoints
float minDistBetweenLaserPoints
The minimum distance between points (in 3D): If two points are too close, one of them is not inserted...
Definition: CPointsMap.h:218
c
const GLubyte * c
Definition: glext.h:6313
mrpt::obs::CActionCollection
Declares a class for storing a collection of robot actions.
Definition: CActionCollection.h:28
THROW_EXCEPTION_FMT
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:43
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::maps::CPointsMap::getAllPoints
void getAllPoints(VECTOR &xs, VECTOR &ys, VECTOR &zs, size_t decimation=1) const
Returns a copy of the 2D/3D points as a std::vector of float coordinates.
Definition: CPointsMap.h:536
mrpt::poses
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CHierarchicalMapMHPartition.h:25
mrpt::system::fileExists
bool fileExists(const std::string &fileName)
Test if a given file (or directory) exists.
Definition: filesystem.cpp:127
CActionCollection.h
mrpt::obs
This namespace contains representation of robot actions and observations.
Definition: CParticleFilter.h:17
mrpt::gui::CDisplayWindow::showImage
void showImage(const mrpt::img::CImage &img)
Show a given color or grayscale image on the window.
Definition: CDisplayWindow.cpp:417
CObservationStereoImages.h
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::obs::CSensoryFrame
Declares a class for storing a "sensory frame", a set of "observations" taken by the robot approximat...
Definition: CSensoryFrame.h:54
mrpt::obs::CObservationStereoImages::Ptr
std::shared_ptr< CObservationStereoImages > Ptr
Definition: CObservationStereoImages.h:43
mrpt::img
Definition: CCanvas.h:17
mrpt::poses::CPose3D
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
TestLaser2Imgs
void TestLaser2Imgs()
Definition: vision_stereo_rectify/test.cpp:51
mrpt::obs::CObservationImage::Ptr
std::shared_ptr< CObservationImage > Ptr
Definition: CObservationImage.h:37
mrpt::img::TColor
A RGB color - 8bit.
Definition: TColor.h:22
mrpt::maps::CSimplePointsMap
A cloud of points in 2D or 3D, which can be built from a sequence of laser scans.
Definition: CSimplePointsMap.h:31
mrpt::gui::CDisplayWindow
This class creates a window as a graphical user interface (GUI) for displaying images to the user.
Definition: CDisplayWindow.h:30
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
mrpt::math::CMatrixFixedNumeric< double, 3, 3 >
mrpt::img::CImage
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
mrpt::obs::CObservation2DRangeScan::Ptr
std::shared_ptr< CObservation2DRangeScan > Ptr
Definition: CObservation2DRangeScan.h:58
CSensoryFrame.h
mrpt::serialization::archiveFrom
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT's CStream,...
Definition: CArchive.h:561
image
GLenum GLsizei GLenum GLenum const GLvoid * image
Definition: glext.h:3552
mrpt::maps::CPointsMap::insertionOptions
TInsertionOptions insertionOptions
The options used when inserting observations in the map.
Definition: CPointsMap.h:257
mrpt::poses::CPose3D::yaw
double yaw() const
Get the YAW angle (in radians)
Definition: CPose3D.h:528
CFileGZInputStream.h
mrpt::system::os::getch
int getch() noexcept
An OS-independent version of getch, which waits until a key is pushed.
Definition: os.cpp:370
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
CObservationImage.h
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::maps
Definition: CBeacon.h:24
iterator
Scalar * iterator
Definition: eigen_plugins.h:26
CArchive.h
mrpt::poses::CPoint3D
A class used to store a 3D point.
Definition: CPoint3D.h:33
CSimplePointsMap.h
CRawlog.h
CDisplayWindow.h
mrpt::obs::CObservationImage
Declares a class derived from "CObservation" that encapsules an image from a camera,...
Definition: CObservationImage.h:35
mrpt::obs::CObservationStereoImages
Observation class for either a pair of left+right or left+disparity images from a stereo camera.
Definition: CObservationStereoImages.h:41
CObservation2DRangeScan.h
mrpt::utils::CFileGZInputStream
mrpt::io::CFileGZInputStream CFileGZInputStream
Definition: utils/CFileGZInputStream.h:7
mrpt::system::pause
void pause(const std::string &msg=std::string("Press any key to continue...")) noexcept
Shows the message "Press any key to continue" (or other custom message) to the current standard outpu...
Definition: os.cpp:428
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25
mrpt::DEG2RAD
double DEG2RAD(const double x)
Degrees to radians.
Definition: core/include/mrpt/core/bits_math.h:42



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