MRPT  1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://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/img/TCamera.h>
38 #include <mrpt/system/filesystem.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 
183  0, 0, 0, -90.0_deg, 0.0_deg, DEG2RAD(-90.0 - 4.6)));
184 
185  obs.leftCamera = cam_params_l;
186  obs.rightCamera = cam_params_r;
187  obs.rightCameraPose = l2r_pose;
188 
189  obs.imageLeft.setExternalStorage(sTrgImgFile_L);
190  obs.imageRight.setExternalStorage(sTrgImgFile_R);
191 
192  obs.sensorLabel = "CAMERA1";
193 
194  // save:
195  archiveFrom(f_out) << obs;
196 
197  // Copy img:
198  if (is_kitti_dataset)
199  {
200  if (!mrpt::system::copyFile(sImg_L, sTrgImg_L))
201  {
202  cerr << "Error copying file: " << sImg_L << " => " << sTrgImg_L
203  << endl;
204  return;
205  }
206  if (!mrpt::system::copyFile(sImg_R, sTrgImg_R))
207  {
208  cerr << "Error copying file: " << sImg_R << " => " << sTrgImg_R
209  << endl;
210  return;
211  }
212  }
213 
214  // cout << "Writing entry #" << i << endl;
215  }
216 
217  cout << "\nAll done!\n";
218 }
219 
220 // ------------------------------------------------------
221 // MAIN
222 // ------------------------------------------------------
223 int main(int argc, char** argv)
224 {
225  try
226  {
227  if (argc != 4 && argc != 5)
228  {
229  cerr << "Usage:\n"
230  "Karlsruhe seqs:\n"
231  " kitti_dataset2rawlog [PATH_TO_DIR_WITH_IMAGES] "
232  "[CALIB_FILE] [OUTPUT_NAME]\n"
233  "KITTI seqs:\n"
234  " kitti_dataset2rawlog [PATH_TO_IMAGE_00] "
235  "[PATH_TO_IMAGE_01] [CALIB_FILE] [OUTPUT_NAME]\n";
236  return 1;
237  }
238 
239  if (argc == 4)
240  {
241  // "Karlsruhe seqs":
242  stereo2rawlog(false, argv[1], "", argv[2], argv[3]);
243  }
244  else
245  {
246  // "KITTI":
247  stereo2rawlog(true, argv[1], argv[2], argv[3], argv[4]);
248  }
249 
250  return 0;
251  }
252  catch (const std::exception& e)
253  {
254  std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
255  return -1;
256  }
257  catch (...)
258  {
259  printf("Untyped exception!!");
260  return -1;
261  }
262 }
uint32_t nrows
Definition: TCamera.h:40
bool createDirectory(const std::string &dirName)
Creates a directory.
Definition: filesystem.cpp:161
mrpt::img::CImage imageLeft
Image from the left camera (this image will be ALWAYS present)
double fx() const
Get the value of the focal length x-value (in pixels).
Definition: TCamera.h:175
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
bool strStartsI(const std::string &str, const std::string &subStr)
Return true if "str" starts with "subStr" (case insensitive)
bool fileExists(const std::string &fileName)
Test if a given file (or directory) exists.
Definition: filesystem.cpp:128
double fy() const
Get the value of the focal length y-value (in pixels).
Definition: TCamera.h:177
A class for parsing text files, returning each non-empty and non-comment line, along its line number...
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:86
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:1571
void stereo2rawlog(bool is_kitti_dataset, const string &src_path0, const string &src_path1, const string &calib_file, const string &out_name)
STL namespace.
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT&#39;s CStream, std::istream, std::ostream, std::stringstream.
Definition: CArchive.h:592
mrpt::poses::CPose3DQuat cameraPose
The pose of the LEFT camera, relative to the robot.
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
mrpt::Clock::time_point TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
Definition: datetime.h:40
const double STEREO_FPS
double cy() const
Get the value of the principal point y-coordinate (in pixels).
Definition: TCamera.h:173
double timestampTotime_t(const mrpt::system::TTimeStamp t) noexcept
Transform from TTimeStamp to standard "time_t" (actually a double number, it can contain fractions of...
Definition: datetime.h:105
Observation class for either a pair of left+right or left+disparity images from a stereo camera...
This namespace contains representation of robot actions and observations.
Parameters for the Brown-Conrady camera lens distortion model.
Definition: TCamera.h:26
void open(const std::string &fil)
Open a file (an alternative to the constructor with a file name)
bool getNextLine(std::string &out_str)
Reads from the file and return the next (non-comment) line, as a std::string.
A class used to store a 3D pose as a translation (x,y,z) and a quaternion (qr,qx,qy,qz).
Definition: CPose3DQuat.h:45
size_type rows() const
Number of rows in the matrix.
size_type cols() const
Number of columns in the matrix.
double cx() const
Get the value of the principal point x-coordinate (in pixels).
Definition: TCamera.h:171
std::string sensorLabel
An arbitrary label that can be used to identify the sensor.
Definition: CObservation.h:62
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
const char * argv[]
mrpt::system::TTimeStamp timestamp
The associated UTC time-stamp.
Definition: CObservation.h:60
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
mrpt::poses::CPose3DQuat rightCameraPose
The pose of the right camera, relative to the left one: Note that using the conventional reference co...
std::string exception_to_str(const std::exception &e)
Builds a nice textual representation of a nested exception, which if generated using MRPT macros (THR...
Definition: exceptions.cpp:59
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:390
const int argc
A quaternion, which can represent a 3D rotation as pair , with a real part "r" and a 3D vector ...
Definition: CQuaternion.h:44
Saves data to a file and transparently compress the data using the given compression level...
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.h:91
mrpt::img::TCamera leftCamera
Parameters for the left/right cameras: individual intrinsic and distortion parameters of the cameras...
uint32_t ncols
Camera resolution.
Definition: TCamera.h:40
mrpt::img::CImage imageRight
Image from the right camera, only contains a valid image if hasImageRight == true.
void loadFromTextFile(std::istream &f)
Loads a vector/matrix from a text file, compatible with MATLAB text format.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020