1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
/* +------------------------------------------------------------------------+
| Mobile Robot Programming Toolkit (MRPT) |
| https://www.mrpt.org/ |
| |
| Copyright (c) 2005-2023, Individual contributors, see AUTHORS file |
| See: https://www.mrpt.org/Authors - All rights reserved. |
| Released under BSD License. See: https://www.mrpt.org/License |
+------------------------------------------------------------------------+ */
/* ------------------------------------------------------
rgbd2rawlog
A small tool to translate RGB+D datasets from TUM:
http://cvpr.in.tum.de/data/datasets/rgbd-dataset/
into MRPT rawlog binary format, ready to be parsed
by RawLogViewer or user programs.
Usage:
rgbd_dataset2rawlog [PATH_TO_RGBD_DATASET] [OUTPUT_NAME]
Where expected input files are:
- PATH_TO_RGBD_DATASET/accelerometer.txt
- PATH_TO_RGBD_DATASET/depth.txt
- PATH_TO_RGBD_DATASET/rgb.txt
- PATH_TO_RGBD_DATASET/rgb/... (Images, 3x8u)
- PATH_TO_RGBD_DATASET/depth/... (Images, 1x16u)
Output files:
- OUTPUT_NAME.rawlog: The output rawlog file.
- OUTPUT_NAME_Images/...: External RGB images.
------------------------------------------------------ */
#include <mrpt/io/CFileGZOutputStream.h>
#include <mrpt/io/CTextFileLinesParser.h>
#include <mrpt/obs/CObservation3DRangeScan.h>
#include <mrpt/obs/CObservationIMU.h>
#include <mrpt/serialization/CArchive.h>
#include <mrpt/system/filesystem.h>
#include <iostream>
using namespace std;
using namespace mrpt;
using namespace mrpt::obs;
using namespace mrpt::serialization;
const double KINECT_FPS = 30.0;
// Based on:
// http://stackoverflow.com/questions/218488/finding-best-matching-key-for-a-given-key-in-a-sorted-stl-container
template <class CONTAINER>
typename CONTAINER::const_iterator find_closest(
const CONTAINER& data, const typename CONTAINER::key_type& searchkey)
{
typename CONTAINER::const_iterator upper = data.lower_bound(searchkey);
if (upper == data.begin() || upper->first == searchkey) return upper;
typename CONTAINER::const_iterator lower = upper;
--lower;
if (upper == data.end() ||
(searchkey - lower->first) < (upper->first - searchkey))
return lower;
return upper;
}
void rgbd2rawlog(const string& src_path, const string& out_name)
{
const string in_fil_acc = src_path + string("/accelerometer.txt");
const string in_fil_depth =
mrpt::system::fileExists(src_path + string("/ir.txt"))
? src_path + string("/ir.txt")
: src_path + string("/depth.txt");
const string in_fil_rgb = src_path + string("/rgb.txt");
// ASSERT_FILE_EXISTS_(in_fil_acc)
// ASSERT_FILE_EXISTS_(in_fil_depth)
// ASSERT_FILE_EXISTS_(in_fil_rgb)
// make a list with RGB & DEPTH files ------------------------
map<double, string> list_rgb, list_depth;
map<double, vector<double>> list_acc;
std::istringstream line;
if (mrpt::system::fileExists(in_fil_rgb))
{
mrpt::io::CTextFileLinesParser fparser(in_fil_rgb);
while (fparser.getNextLine(line))
{
double tim;
string f;
if (line >> tim >> f) list_rgb[tim] = f;
}
}
if (mrpt::system::fileExists(in_fil_depth))
{
mrpt::io::CTextFileLinesParser fparser(in_fil_depth);
while (fparser.getNextLine(line))
{
double tim;
string f;
if (line >> tim >> f) list_depth[tim] = f;
}
}
if (mrpt::system::fileExists(in_fil_acc))
{
mrpt::io::CTextFileLinesParser fparser(in_fil_acc);
while (fparser.getNextLine(line))
{
double tim;
double ax, ay, az;
if (line >> tim >> ax >> ay >> az)
{
vector<double>& v = list_acc[tim];
v.resize(3);
v[0] = ax;
v[1] = ay;
v[2] = az;
}
}
}
cout << "Parsed: " << list_depth.size() << " / " << list_rgb.size() << " / "
<< list_acc.size() << " depth/rgb/acc entries.\n";
const bool only_ir = list_depth.size() > 10 && list_rgb.empty();
const bool only_rgb = list_depth.empty() && list_rgb.size() > 10;
// Create output directory for images ------------------------------
const string out_img_dir = out_name + string("_Images");
cout << "Creating images directory: " << out_img_dir << endl;
mrpt::system::createDirectory(out_img_dir);
// Create rawlog file ----------------------------------------------
const string out_rawlog_fil = out_name + string(".rawlog");
cout << "Creating rawlog: " << out_rawlog_fil << endl;
mrpt::io::CFileGZOutputStream f_out(out_rawlog_fil);
// Fill out the common field to all entries:
CObservation3DRangeScan obs;
obs.sensorLabel = "KINECT";
obs.range_is_depth = true; // Kinect style: ranges are actually depth
// values, not Euclidean distances.
// Range & RGB images are already registered and warped to remove
// distortion:
const double FOCAL = 525.0;
obs.cameraParams.nrows = 480;
obs.cameraParams.ncols = 640;
obs.cameraParams.fx(FOCAL);
obs.cameraParams.fy(FOCAL);
obs.cameraParams.cx(319.5);
obs.cameraParams.cy(239.5);
obs.cameraParamsIntensity = obs.cameraParams;
obs.relativePoseIntensityWRTDepth = mrpt::poses::CPose3D(
0, 0, 0, mrpt::DEG2RAD(-90), 0,
mrpt::DEG2RAD(-90)); // No translation between rgb & range
// cameras, and rotation only due to XYZ
// axes conventions.
CObservationIMU obs_imu;
obs_imu.sensorLabel = "KINECT_ACC";
obs_imu.dataIsPresent[IMU_X_ACC] = true;
obs_imu.dataIsPresent[IMU_Y_ACC] = true;
obs_imu.dataIsPresent[IMU_Z_ACC] = true;
// Go thru the data:
unsigned int counter = 0;
if (!only_ir && !only_rgb)
{
for (map<double, string>::const_iterator it_list_rgb = list_rgb.begin();
it_list_rgb != list_rgb.end(); ++it_list_rgb)
{
cout << "Processing image " << counter << "\r";
cout.flush();
counter++;
// This is not the most efficient solution in the world, but...come
// on! it's just a rawlog converter tool!
map<double, string>::const_iterator it_list_depth =
find_closest(list_depth, it_list_rgb->first);
const double At =
std::abs(it_list_rgb->first - it_list_depth->first);
if (At > (1. / KINECT_FPS) * .5)
{
cout << "\nWarning: Discarding observation for too separated "
"RGB/D timestamps: "
<< At * 1e3 << " ms\n";
}
else
{
// OK, we accept this RGB-DEPTH pair:
const double avrg_time =
.5 * (it_list_rgb->first + it_list_depth->first);
obs.timestamp = mrpt::system::time_tToTimestamp(avrg_time);
// RGB img:
obs.hasIntensityImage = true;
obs.intensityImage.loadFromFile(
src_path + string("/") + it_list_rgb->second);
const string sRGBfile =
mrpt::format("%.06f_rgb.png", avrg_time);
obs.intensityImage.saveToFile(
out_img_dir + string("/") + sRGBfile);
obs.intensityImage.setExternalStorage(sRGBfile);
// Depth:
obs.hasRangeImage = true;
obs.rangeImage_forceResetExternalStorage();
mrpt::img::CImage depth_img;
if (!depth_img.loadFromFile(
src_path + string("/") + it_list_depth->second))
throw std::runtime_error(
string("Error loading depth image!: ") +
it_list_depth->second);
const unsigned int w = depth_img.getWidth();
const unsigned int h = depth_img.getHeight();
obs.rangeImage_setSize(h, w);
for (unsigned int row = 0; row < h; row++)
{
const uint16_t* ptr = depth_img.ptrLine<uint16_t>(row);
for (unsigned int col = 0; col < w; col++)
obs.rangeImage(row, col) = (*ptr++) * (1.f / 5000);
}
const string sDepthfile =
mrpt::format("%.06f_depth.bin", avrg_time);
obs.rangeImage_convertToExternalStorage(
sDepthfile, out_img_dir + string("/"));
// save:
archiveFrom(f_out) << obs;
// Search for acc data:
map<double, vector<double>>::const_iterator it_list_acc =
find_closest(list_acc, avrg_time);
const double At_acc =
std::abs(it_list_rgb->first - it_list_acc->first);
if (At_acc < 1e-2)
{
obs_imu.timestamp =
mrpt::system::time_tToTimestamp(avrg_time);
obs_imu.rawMeasurements[IMU_X_ACC] = it_list_acc->second[0];
obs_imu.rawMeasurements[IMU_Y_ACC] = it_list_acc->second[1];
obs_imu.rawMeasurements[IMU_Z_ACC] = it_list_acc->second[2];
archiveFrom(f_out) << obs_imu;
}
}
}
}
else if (only_ir)
{
for (map<double, string>::const_iterator it_list_depth =
list_depth.begin();
it_list_depth != list_depth.end(); ++it_list_depth)
{
cout << "Processing image " << counter << "\r";
cout.flush();
counter++;
const double avrg_time = it_list_depth->first;
obs.timestamp =
mrpt::system::time_tToTimestamp(it_list_depth->first);
// Depth:
obs.hasRangeImage = true;
obs.rangeImage_forceResetExternalStorage();
mrpt::img::CImage depth_img;
if (!depth_img.loadFromFile(
src_path + string("/") + it_list_depth->second))
throw std::runtime_error(
string("Error loading depth image!: ") +
it_list_depth->second);
const unsigned int w = depth_img.getWidth();
const unsigned int h = depth_img.getHeight();
obs.rangeImage_setSize(h, w);
for (unsigned int row = 0; row < h; row++)
{
const uint16_t* ptr = depth_img.ptrLine<uint16_t>(row);
for (unsigned int col = 0; col < w; col++)
obs.rangeImage(row, col) = (*ptr++) * (1.f / 5000);
}
const string sDepthfile =
mrpt::format("%.06f_depth.bin", avrg_time);
obs.rangeImage_convertToExternalStorage(
sDepthfile, out_img_dir + string("/"));
// save:
archiveFrom(f_out) << obs;
}
}
else if (only_rgb)
{
for (map<double, string>::const_iterator it_list_rgb = list_rgb.begin();
it_list_rgb != list_rgb.end(); ++it_list_rgb)
{
cout << "Processing image " << counter << "\r";
cout.flush();
counter++;
const double avrg_time = it_list_rgb->first;
obs.timestamp = mrpt::system::time_tToTimestamp(it_list_rgb->first);
// RGB img:
obs.hasIntensityImage = true;
obs.intensityImage.loadFromFile(
src_path + string("/") + it_list_rgb->second);
const string sRGBfile = mrpt::format("%.06f_rgb.png", avrg_time);
obs.intensityImage.saveToFile(out_img_dir + string("/") + sRGBfile);
obs.intensityImage.setExternalStorage(sRGBfile);
// save:
archiveFrom(f_out) << obs;
}
}
cout << "\nAll done!\n";
}
// ------------------------------------------------------
// MAIN
// ------------------------------------------------------
int main(int argc, char** argv)
{
try
{
if (argc != 3)
{
cerr << "Usage: " << argv[0]
<< " [PATH_TO_RGBD_DATASET] [OUTPUT_NAME]\n";
return 1;
}
rgbd2rawlog(argv[1], argv[2]);
return 0;
}
catch (const std::exception& e)
{
std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
return -1;
}
}
|