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  kitti_dataset2rawlog
12  A small tool to translate the KITTI datasets and
13  the karlsruhe sequences
14  http://www.cvlibs.net/datasets/karlsruhe_sequences/
15 
16  into MRPT rawlog binary format, ready to be parsed
17  by RawLogViewer or user programs.
18 
19 Usage:
20  * Karlsruhe seqs:
21  kitti_dataset2rawlog [PATH_TO_DIR_WITH_IMAGES] [CALIB_FILE]
22 [OUTPUT_NAME]
23 
24  * KITTI seqs:
25  kitti_dataset2rawlog [PATH_TO_IMAGE_00] [PATH_TO_IMAGE_01] [CALIB_FILE]
26 [OUTPUT_NAME]
27 
28 Output files:
29  - OUTPUT_NAME.rawlog: The output rawlog file.
30 
31  ------------------------------------------------------ */
32 
33 #include <mrpt/system/filesystem.h>
36 #include <mrpt/img/TCamera.h>
40 #include <iostream>
41 
42 using namespace std;
43 using namespace mrpt;
44 using namespace mrpt::obs;
45 using namespace mrpt::serialization;
46 
47 const double STEREO_FPS = 10.0;
48 
49 void stereo2rawlog(
50  bool is_kitti_dataset, const string& src_path0, const string& src_path1,
51  const string& calib_file, const string& out_name)
52 {
53  /* Camera params:
54  Left (1):
55  6.790081e+02 0.000000e+00 6.598034e+02 0.000000e+00
56  0.000000e+00 6.790081e+02 1.865724e+02 0.000000e+00
57  0.000000e+00 0.000000e+00 1.000000e+00 0.000000e+00
58 
59  Right (2):
60 
61  6.790081e+02 0.000000e+00 6.598034e+02 -3.887481e+02
62  0.000000e+00 6.790081e+02 1.865724e+02 0.000000e+00
63  0.000000e+00 0.000000e+00 1.000000e+00 0.000000e+00
64 
65  base = 3.887481e+02 / 6.790081e+02 = 0.57252351 m
66  */
67 
68  // Parse calib file:
69  mrpt::math::CMatrixDouble P1_roi, P2_roi;
70  bool p1_ok = false, p2_ok = false;
71 
72  const string name_P0 = is_kitti_dataset ? "P0:" : "P1_roi:";
73  const string name_P1 = is_kitti_dataset ? "P1:" : "P2_roi:";
74 
75  std::istringstream ss;
77  fp.open(calib_file);
78  while (fp.getNextLine(ss))
79  {
80  string sStart;
81  ss >> sStart;
82  if (mrpt::system::strStartsI(sStart, name_P0))
83  {
84  P1_roi.loadFromTextFile(ss);
85  ASSERT_(P1_roi.cols() == 12 && P1_roi.rows() == 1);
86  p1_ok = true;
87  }
88  if (mrpt::system::strStartsI(sStart, name_P1))
89  {
90  P2_roi.loadFromTextFile(ss);
91  ASSERT_(P2_roi.cols() == 12 && P2_roi.rows() == 1);
92  p2_ok = true;
93  }
94  }
95 
96  if (!p1_ok || !p2_ok)
97  throw std::runtime_error("Couldn't load P*_ROI calib matrices!");
98 
99  mrpt::img::TCamera cam_params_l;
100  cam_params_l.ncols = 1344;
101  cam_params_l.nrows = 391;
102  cam_params_l.fx(P2_roi(0, 0));
103  cam_params_l.fy(P2_roi(0, 5));
104  cam_params_l.cx(P2_roi(0, 2));
105  cam_params_l.cy(P2_roi(0, 6));
106 
107  mrpt::img::TCamera cam_params_r = cam_params_l;
108 
109  // base = -P2_roi(1,4)/P2_roi(1,1)
110  const double baseline = -P2_roi(0, 3) / P2_roi(0, 0);
111  const mrpt::poses::CPose3DQuat l2r_pose(
112  baseline, 0.0, 0.0, mrpt::math::CQuaternionDouble());
113 
114  cout << "baseline: " << baseline << endl;
115 
116  // Create rawlog file ----------------------------------------------
117  const string out_rawlog_fil = out_name + string(".rawlog");
118  const string out_imgs_dir = out_name + string("_Images");
119  cout << "Creating rawlog: " << out_rawlog_fil << endl;
120  mrpt::io::CFileGZOutputStream f_out(out_rawlog_fil);
121 
122  if (is_kitti_dataset)
123  {
124  cout << "Creating imgs dir: " << out_imgs_dir << endl;
125  mrpt::system::createDirectory(out_imgs_dir);
126  }
127 
129 
130  // For each image:
131  for (int i = 0;; i++)
132  {
133  string sImgFile_L, sImgFile_R;
134  string sImg_L, sImg_R;
135  string sTrgImgFile_L, sTrgImgFile_R;
136  string sTrgImg_L, sTrgImg_R; // If !="", will copy the file.
137 
138  if (is_kitti_dataset)
139  {
140  // 0000000000.png
141  // 1234567890
142  sImgFile_L = mrpt::format("%010i.png", i);
143  sImgFile_R = mrpt::format("%010i.png", i);
144  sImg_L =
145  mrpt::format("%s/%s", src_path0.c_str(), sImgFile_L.c_str());
146  sImg_R =
147  mrpt::format("%s/%s", src_path1.c_str(), sImgFile_R.c_str());
148 
149  sTrgImgFile_L = mrpt::format("I0_%s", sImgFile_L.c_str());
150  sTrgImgFile_R = mrpt::format("I1_%s", sImgFile_R.c_str());
151 
152  sTrgImg_L = mrpt::format(
153  "%s/%s", out_imgs_dir.c_str(), sTrgImgFile_L.c_str());
154  sTrgImg_R = mrpt::format(
155  "%s/%s", out_imgs_dir.c_str(), sTrgImgFile_R.c_str());
156  }
157  else
158  {
159  // I1_000000.png
160  // I2_002570.png
161  sImgFile_L = mrpt::format("I1_%06i.png", i);
162  sImgFile_R = mrpt::format("I2_%06i.png", i);
163  sTrgImgFile_L = sImgFile_L;
164  sTrgImgFile_R = sImgFile_R;
165  sImg_L =
166  mrpt::format("%s/%s", src_path0.c_str(), sImgFile_L.c_str());
167  sImg_R =
168  mrpt::format("%s/%s", src_path0.c_str(), sImgFile_R.c_str());
169  }
170 
171  if (!mrpt::system::fileExists(sImg_L) ||
172  !mrpt::system::fileExists(sImg_R))
173  {
174  cout << "Couldn't detect image pair " << sImg_L << " | " << sImg_R
175  << " -> ending.\n";
176  break;
177  }
178 
184  0, 0, 0, DEG2RAD(-90.0), DEG2RAD(0.0), DEG2RAD(-90.0 - 4.6)));
185 
186  obs.leftCamera = cam_params_l;
187  obs.rightCamera = cam_params_r;
188  obs.rightCameraPose = l2r_pose;
189 
190  obs.imageLeft.setExternalStorage(sTrgImgFile_L);
191  obs.imageRight.setExternalStorage(sTrgImgFile_R);
192 
193  obs.sensorLabel = "CAMERA1";
194 
195  // save:
196  archiveFrom(f_out) << obs;
197 
198  // Copy img:
199  if (is_kitti_dataset)
200  {
201  if (!mrpt::system::copyFile(sImg_L, sTrgImg_L))
202  {
203  cerr << "Error copying file: " << sImg_L << " => " << sTrgImg_L
204  << endl;
205  return;
206  }
207  if (!mrpt::system::copyFile(sImg_R, sTrgImg_R))
208  {
209  cerr << "Error copying file: " << sImg_R << " => " << sTrgImg_R
210  << endl;
211  return;
212  }
213  }
214 
215  // cout << "Writing entry #" << i << endl;
216  }
217 
218  cout << "\nAll done!\n";
219 }
220 
221 // ------------------------------------------------------
222 // MAIN
223 // ------------------------------------------------------
224 int main(int argc, char** argv)
225 {
226  try
227  {
228  if (argc != 4 && argc != 5)
229  {
230  cerr << "Usage:\n"
231  "Karlsruhe seqs:\n"
232  " kitti_dataset2rawlog [PATH_TO_DIR_WITH_IMAGES] "
233  "[CALIB_FILE] [OUTPUT_NAME]\n"
234  "KITTI seqs:\n"
235  " kitti_dataset2rawlog [PATH_TO_IMAGE_00] "
236  "[PATH_TO_IMAGE_01] [CALIB_FILE] [OUTPUT_NAME]\n";
237  return 1;
238  }
239 
240  if (argc == 4)
241  {
242  // "Karlsruhe seqs":
243  stereo2rawlog(false, argv[1], "", argv[2], argv[3]);
244  }
245  else
246  {
247  // "KITTI":
248  stereo2rawlog(true, argv[1], argv[2], argv[3], argv[4]);
249  }
250 
251  return 0;
252  }
253  catch (std::exception& e)
254  {
255  std::cout << "MRPT exception caught: " << e.what() << std::endl;
256  return -1;
257  }
258  catch (...)
259  {
260  printf("Untyped exception!!");
261  return -1;
262  }
263 }
DEG2RAD
#define DEG2RAD
Definition: core/include/mrpt/core/bits_math.h:59
filesystem.h
mrpt::obs::CObservation::sensorLabel
std::string sensorLabel
An arbitrary label that can be used to identify the sensor.
Definition: CObservation.h:62
mrpt::io::CTextFileLinesParser::open
void open(const std::string &fil)
Open a file (an alternative to the constructor with a file name)
Definition: CTextFileLinesParser.cpp:25
mrpt::system::copyFile
bool copyFile(const std::string &sourceFile, const std::string &targetFile, std::string *outErrStr=nullptr, bool copyAttribs=true)
Copies file sourceFile to targetFile.
Definition: filesystem.cpp:387
mrpt::system::time_tToTimestamp
mrpt::system::TTimeStamp time_tToTimestamp(const double t)
Transform from standard "time_t" (actually a double number, it can contain fractions of seconds) to T...
Definition: datetime.cpp:50
mrpt::system::strStartsI
bool strStartsI(const std::string &str, const std::string &subStr)
Return true if "str" starts with "subStr" (case insensitive)
Definition: string_utils.cpp:321
string_utils.h
mrpt::img::TCamera::fx
double fx() const
Get the value of the focal length x-value (in pixels).
Definition: TCamera.h:165
mrpt::obs::CObservationStereoImages::rightCamera
mrpt::img::TCamera rightCamera
Definition: CObservationStereoImages.h:93
CTextFileLinesParser.h
mrpt::img::TCamera::ncols
uint32_t ncols
Camera resolution.
Definition: TCamera.h:41
CFileGZOutputStream.h
mrpt::system::now
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:75
stereo2rawlog
void stereo2rawlog(bool is_kitti_dataset, const string &src_path0, const string &src_path1, const string &calib_file, const string &out_name)
Definition: vision_stereo_rectify/test.cpp:49
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::poses::CPose3DQuat
A class used to store a 3D pose as a translation (x,y,z) and a quaternion (qr,qx,qy,...
Definition: CPose3DQuat.h:48
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
mrpt::system::fileExists
bool fileExists(const std::string &fileName)
Test if a given file (or directory) exists.
Definition: filesystem.cpp:127
mrpt::obs::CObservationStereoImages::imageRight
mrpt::img::CImage imageRight
Image from the right camera, only contains a valid image if hasImageRight == true.
Definition: CObservationStereoImages.h:72
mrpt::img::TCamera::cy
double cy() const
Get the value of the principal point y-coordinate (in pixels).
Definition: TCamera.h:163
mrpt::obs
This namespace contains representation of robot actions and observations.
Definition: CParticleFilter.h:17
TCamera.h
CObservationStereoImages.h
mrpt::obs::CObservation::timestamp
mrpt::system::TTimeStamp timestamp
The associated UTC time-stamp.
Definition: CObservation.h:60
mrpt::system::TTimeStamp
uint64_t TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1,...
Definition: datetime.h:31
mrpt::math::CMatrixTemplateNumeric< double >
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::img::TCamera::fy
double fy() const
Get the value of the focal length y-value (in pixels).
Definition: TCamera.h:167
mrpt::io::CTextFileLinesParser::getNextLine
bool getNextLine(std::string &out_str)
Reads from the file and return the next (non-comment) line, as a std::string.
Definition: CTextFileLinesParser.cpp:44
STEREO_FPS
const double STEREO_FPS
Definition: vision_stereo_rectify/test.cpp:47
mrpt::obs::CObservationStereoImages::leftCamera
mrpt::img::TCamera leftCamera
Parameters for the left/right cameras: individual intrinsic and distortion parameters of the cameras.
Definition: CObservationStereoImages.h:93
mrpt::obs::CObservationStereoImages::imageLeft
mrpt::img::CImage imageLeft
Image from the left camera (this image will be ALWAYS present)
Definition: CObservationStereoImages.h:68
mrpt::img::TCamera::cx
double cx() const
Get the value of the principal point x-coordinate (in pixels).
Definition: TCamera.h:161
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::img::TCamera::nrows
uint32_t nrows
Definition: TCamera.h:41
mrpt::io::CFileGZOutputStream
Saves data to a file and transparently compress the data using the given compression level.
Definition: io/CFileGZOutputStream.h:26
mrpt::poses::CPose3D
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
mrpt::system::timestampTotime_t
double timestampTotime_t(const mrpt::system::TTimeStamp t)
Transform from TTimeStamp to standard "time_t" (actually a double number, it can contain fractions of...
Definition: datetime.cpp:56
mrpt::img::TCamera
Structure to hold the parameters of a pinhole camera model.
Definition: TCamera.h:29
mrpt::serialization
Definition: aligned_serialization.h:14
mrpt::math::CQuaternion
A quaternion, which can represent a 3D rotation as pair , with a real part "r" and a 3D vector ,...
Definition: CQuaternion.h:46
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
mrpt::img::CImage::setExternalStorage
void setExternalStorage(const std::string &fileName) noexcept
By using this method the image is marked as referenced to an external file, which will be loaded only...
Definition: CImage.cpp:1887
mrpt::obs::CObservationStereoImages::cameraPose
mrpt::poses::CPose3DQuat cameraPose
The pose of the LEFT camera, relative to the robot.
Definition: CObservationStereoImages.h:96
mrpt::io::CTextFileLinesParser
A class for parsing text files, returning each non-empty and non-comment line, along its line number.
Definition: CTextFileLinesParser.h:26
string
GLsizei const GLchar ** string
Definition: glext.h:4101
CArchive.h
mrpt::system::createDirectory
bool createDirectory(const std::string &dirName)
Creates a directory.
Definition: filesystem.cpp:158
mrpt::obs::CObservationStereoImages::rightCameraPose
mrpt::poses::CPose3DQuat rightCameraPose
The pose of the right camera, relative to the left one: Note that using the conventional reference co...
Definition: CObservationStereoImages.h:104
mrpt::obs::CObservationStereoImages
Observation class for either a pair of left+right or left+disparity images from a stereo camera.
Definition: CObservationStereoImages.h:41



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