RobotsIO
Loading...
Searching...
No Matches
DepthToFile.cpp
1/*
2 * Copyright (C) 2020 Istituto Italiano di Tecnologia (IIT)
3 *
4 * This software may be modified and distributed under the terms of the
5 * BSD 3-Clause license. See the accompanying LICENSE file for details.
6 */
7
8#include <RobotsIO/Utils/DepthToFile.h>
9
10#include <opencv2/core/eigen.hpp>
11
12#include <iostream>
13
14using namespace Eigen;
15
16
17bool RobotsIO::Utils::depth_to_file(const std::string& output_path, const cv::Mat& depth)
18{
19 const std::string log_name = "RobotsIO::Utils::depth_to_file";
20
21 std::FILE* out;
22
23 if ((out = std::fopen(output_path.c_str(), "wb")) == nullptr)
24 {
25 std::cout << log_name << "Error: cannot open output file " + output_path << std::endl;
26 return false;
27 }
28
29 /* Write image size. */
30 std::size_t dims[2];
31 dims[0] = depth.cols;
32 dims[1] = depth.rows;
33 if (std::fwrite(dims, sizeof(dims), 1, out) != 1)
34 {
35 std::cout << log_name << "Error: cannot write image size to " + output_path << std::endl;
36
37 fclose(out);
38
39 return false;
40 }
41
42 if (std::fwrite(depth.data, sizeof(float), dims[0] * dims[1], out) != dims[0] * dims[1])
43 {
44 std::cout << log_name << "Error: cannot write image data to " + output_path << std::endl;
45
46 fclose(out);
47
48 return false;
49 }
50
51 std::fclose(out);
52
53 return true;
54}
55
56
57bool RobotsIO::Utils::depth_to_file(const std::string& output_path, const MatrixXf& depth)
58{
59 cv::Mat depth_cv;
60 cv::eigen2cv(depth, depth_cv);
61 return RobotsIO::Utils::depth_to_file(output_path, depth_cv);
62}