8#include <RobotsIO/Utils/FileToDepth.h>
12#include <opencv2/opencv.hpp>
13#include <opencv2/core/eigen.hpp>
18std::pair<bool, Eigen::MatrixXf> RobotsIO::Utils::file_to_depth(
const std::string& file_name)
20 const std::string log_name =
"RobotsIO::Utils::file_to_depth";
23 auto dot_position = file_name.find_last_of(
'.');
24 if (dot_position == std::string::npos)
26 std::cout << log_name <<
"Error: invalid file extension in provided file name " + file_name << std::endl;
27 return std::make_pair(
false, MatrixXf());
29 std::string format = file_name.substr(dot_position);
31 if (format ==
".float")
35 if ((in = std::fopen(file_name.c_str(),
"rb")) ==
nullptr)
37 std::cout << log_name <<
"Error: cannot open file " + file_name << std::endl;
38 return std::make_pair(
false, MatrixXf());
43 if (std::fread(dims,
sizeof(dims), 1, in) != 1)
45 std::cout << log_name <<
"Error: cannot load depth size for frame " + file_name << std::endl;
49 return std::make_pair(
false, MatrixXf());
53 float float_image_raw[dims[0] * dims[1]];
54 if (std::fread(float_image_raw,
sizeof(
float), dims[0] * dims[1], in) != dims[0] * dims[1])
56 std::cout << log_name <<
"Error: cannot load depth data for frame " + file_name << std::endl;
60 return std::make_pair(
false, MatrixXf());
64 MatrixXf float_image(dims[1], dims[0]);
65 float_image = Map<Matrix<float, -1, -1, RowMajor>>(float_image_raw, dims[1], dims[0]);
69 return std::make_pair(
true, float_image);
71 else if (format ==
".png")
73 cv::Mat image = cv::imread(file_name, cv::IMREAD_UNCHANGED);
77 std::cout << log_name <<
"Error: cannot load depth data for frame " + file_name << std::endl;
79 return std::make_pair(
false, MatrixXf());
82 MatrixXf float_image(image.rows, image.cols);
83 cv::cv2eigen(image, float_image);
86 float depth_scale = 0.1 / 1000.0;
87 float_image *= depth_scale;
89 return std::make_pair(
true, float_image);
92 std::cout << log_name <<
"Error: not supported file extension in provided file name " + file_name << std::endl;
93 return std::make_pair(
false, MatrixXf());