Skip to content

Commit 07171c5

Browse files
authored
PR #13705 from gilpazintel: add support to 3D format in rs-convert.
2 parents 61c5582 + 87c9f95 commit 07171c5

File tree

7 files changed

+147
-4
lines changed

7 files changed

+147
-4
lines changed

tools/convert/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ add_executable(${PROJECT_NAME} rs-convert.cpp
1313
converters/converter-bin.hpp
1414
converters/converter-csv.hpp
1515
converters/converter-csv.cpp
16+
converters/converter-3d-csv.hpp
17+
converters/converter-3d-csv.cpp
1618
converters/converter-ply.hpp
1719
converters/converter-png.hpp
1820
converters/converter-raw.hpp

tools/convert/converter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ void converter_base::wait_sub_workers()
5757

5858
void converter_base::wait()
5959
{
60-
_worker.join();
60+
if (_worker.joinable()) {
61+
_worker.join();
62+
}
6163
}
6264

6365
std::string converter_base::get_statistics()
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// License: Apache 2.0. See LICENSE file in root directory.
2+
// Copyright(c) 2025 Intel Corporation. All Rights Reserved.
3+
4+
5+
#include "converter-3d-csv.hpp"
6+
#include <iomanip>
7+
#include <algorithm>
8+
#include <iostream>
9+
10+
using namespace rs2::tools::converter;
11+
12+
converter_3d_csv::converter_3d_csv(const std::string& filePath, rs2_stream streamType)
13+
: _filePath(filePath)
14+
, _streamType(streamType)
15+
{
16+
}
17+
18+
void converter_3d_csv::convert_depth(rs2::depth_frame& depthframe)
19+
{
20+
if (frames_map_get_and_set(depthframe.get_profile().stream_type(), depthframe.get_profile().stream_index(), depthframe.get_frame_number())) {
21+
return;
22+
}
23+
24+
start_worker(
25+
[this, depthframe] {
26+
27+
std::stringstream filename;
28+
std::stringstream filename_3d_dist;
29+
filename << _filePath << "3d_dist"
30+
<< "_" << depthframe.get_profile().stream_name()
31+
<< "_" << std::setprecision(14) << std::fixed << depthframe.get_timestamp()
32+
<< ".csv";
33+
34+
std::stringstream metadata_file;
35+
metadata_file << _filePath
36+
<< "_" << depthframe.get_profile().stream_name()
37+
<< "_metadata_" << std::setprecision(14) << std::fixed << depthframe.get_timestamp()
38+
<< ".txt";
39+
40+
std::string filenameS = filename.str();
41+
std::string metadataS = metadata_file.str();
42+
43+
add_sub_worker(
44+
[filenameS, metadataS, depthframe] {
45+
std::ofstream fs(filenameS, std::ios::trunc);
46+
47+
if (fs) {
48+
// Write the title row
49+
fs << "i,j,x (meters),y (meters),depth (meters)\n";
50+
51+
// Get the intrinsic parameters of the depth frame
52+
rs2_intrinsics intrinsics = depthframe.get_profile().as<rs2::video_stream_profile>().get_intrinsics();
53+
54+
for (int y = 0; y < depthframe.get_height(); y++) {
55+
56+
for (int x = 0; x < depthframe.get_width(); x++) {
57+
float distance = depthframe.get_distance(x, y);
58+
59+
// Write to the 3D distance file if the distance is non-zero
60+
if (distance != 0) {
61+
float pixel[2] = { static_cast<float>(x), static_cast<float>(y) };
62+
float point[3];
63+
rs2_deproject_pixel_to_point(point, &intrinsics, pixel, distance);
64+
fs << x << "," << y << "," << point[0] << "," << point[1] << "," << point[2] << '\n';
65+
}
66+
}
67+
fs << '\n';
68+
}
69+
fs.flush();
70+
}
71+
metadata_to_txtfile(depthframe, metadataS);
72+
});
73+
wait_sub_workers();
74+
});
75+
}
76+
77+
void converter_3d_csv::convert(rs2::frame& frame)
78+
{
79+
if (!(_streamType == rs2_stream::RS2_STREAM_ANY || frame.get_profile().stream_type() == _streamType))
80+
return;
81+
82+
auto depthframe = frame.as<rs2::depth_frame>();
83+
if (depthframe)
84+
{
85+
convert_depth(depthframe);
86+
}
87+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// License: Apache 2.0. See LICENSE file in root directory.
2+
// Copyright(c) 2025 Intel Corporation. All Rights Reserved.
3+
4+
#pragma once
5+
6+
7+
#include <fstream>
8+
#include <map>
9+
#include <mutex>
10+
#include <condition_variable>
11+
#include "../converter.hpp"
12+
13+
14+
namespace rs2 {
15+
namespace tools {
16+
namespace converter {
17+
18+
class converter_3d_csv : public converter_base {
19+
protected:
20+
21+
rs2_stream _streamType;
22+
std::string _filePath;
23+
24+
public:
25+
26+
converter_3d_csv(const std::string& filePath, rs2_stream streamType = rs2_stream::RS2_STREAM_ANY);
27+
28+
void convert(rs2::frame& frame) override;
29+
30+
std::string name() const override
31+
{
32+
return "CSV 3D converter";
33+
}
34+
35+
void convert_depth(rs2::depth_frame& depthframe);
36+
37+
};
38+
} // namespace converter
39+
} // namespace tools
40+
} // namespace rs2

tools/convert/converters/converter-csv.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// License: Apache 2.0. See LICENSE file in root directory.
2-
// Copyright(c) 2021 Intel Corporation. All Rights Reserved.
2+
// Copyright(c) 2025 Intel Corporation. All Rights Reserved.
33

44

55
#include "converter-csv.hpp"
@@ -58,7 +58,7 @@ void converter_csv::convert_depth(rs2::depth_frame& depthframe)
5858
}
5959

6060
start_worker(
61-
[this, &depthframe] {
61+
[this, depthframe] {
6262

6363
std::stringstream filename;
6464
filename << _filePath

tools/convert/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Console app for converting ROS-bag files to various formats (currently supported
1111
|`-i <ros-bag-file>`|ROS-bag filename||
1212
|`-p <png-path>`|convert to PNG, set output path to png-path||
1313
|`-v <csv-path>`|convert to CSV, set output path to csv-path, supported formats: depth, color, imu, pose||
14+
|`-V <csv-path>`|convert to 3D CSV, set output path to csv-path, supported formats: depth||
1415
|`-r <raw-path>`|convert to RAW, set output path to raw-path||
1516
|`-l <ply-path>`|convert to PLY, set output path to ply-path||
1617
|`-b <bin-path>`|convert to BIN (depth matrix), set output path to bin-path||

tools/convert/rs-convert.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// License: Apache 2.0. See LICENSE file in root directory.
2-
// Copyright(c) 2018-24 Intel Corporation. All Rights Reserved.
2+
// Copyright(c) 2018-25 Intel Corporation. All Rights Reserved.
33

44
#include <iostream>
55

@@ -8,6 +8,7 @@
88
#include <common/cli.h>
99

1010
#include "converters/converter-csv.hpp"
11+
#include "converters/converter-3d-csv.hpp"
1112
#include "converters/converter-png.hpp"
1213
#include "converters/converter-raw.hpp"
1314
#include "converters/converter-ply.hpp"
@@ -29,6 +30,7 @@ int main(int argc, char** argv) try
2930
cli::value<string> inputFilename('i', "input", "ros-bag-file", "", "ROS-bag filename", cli::required );
3031
cli::value<string> outputFilenamePng('p', "output-png", "png-path", "", "output PNG file(s) path");
3132
cli::value<string> outputFilenameCsv('v', "output-csv", "csv-path", "", "output CSV (depth matrix) file(s) path");
33+
cli::value<string> outputFilename3dCsv('V', "output-3d-csv", "3d-csv-path", "", "output 3d CSV (depth matrix) file(s) path");
3234
cli::value<string> outputFilenameRaw('r', "output-raw", "raw-path", "", "output RAW file(s) path");
3335
cli::value<string> outputFilenamePly('l', "output-ply", "ply-path", "", "output PLY file(s) path");
3436
cli::value<string> outputFilenameBin('b', "output-bin", "bin-path", "", "output BIN (depth matrix) file(s) path");
@@ -49,6 +51,7 @@ int main(int argc, char** argv) try
4951
.arg( startTime )
5052
.arg( outputFilenamePng )
5153
.arg( outputFilenameCsv )
54+
.arg( outputFilename3dCsv )
5255
.arg( outputFilenameRaw )
5356
.arg( outputFilenamePly )
5457
.arg( outputFilenameBin )
@@ -72,6 +75,14 @@ int main(int argc, char** argv) try
7275
, streamType));
7376
}
7477

78+
if (outputFilename3dCsv.isSet())
79+
{
80+
converters.push_back(
81+
make_shared<rs2::tools::converter::converter_3d_csv>(
82+
outputFilename3dCsv.getValue()
83+
, streamType));
84+
}
85+
7586
if (outputFilenamePng.isSet())
7687
{
7788
converters.push_back(

0 commit comments

Comments
 (0)