ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
RGBDVideoReader.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include <FileSystem.h>
11 #include <Helper.h>
12 #include <IJsonConvertibleIO.h>
13 #include <ImageIO.h>
14 
15 #include <string>
16 
18 
19 namespace cloudViewer {
20 namespace t {
21 namespace io {
22 
23 std::string RGBDVideoReader::ToString() const {
24  if (IsOpened()) {
25  return fmt::format(
26  "RGBDVideoReader reading file {} at position {}us / {}us",
28  GetMetadata().stream_length_usec_);
29  } else {
30  return "RGBDVideoReader: No open file.";
31  }
32 }
33 
34 void RGBDVideoReader::SaveFrames(const std::string &frame_path,
35  uint64_t start_time,
36  uint64_t end_time) {
37  if (!IsOpened()) {
38  utility::LogError("Null file handler. Please call Open().");
39  }
41  fmt::format("{}/color", frame_path));
43  fmt::format("{}/depth", frame_path));
44  if (!success) {
46  "Could not create color or depth subfolder in {} or they "
47  "already exist.",
48  frame_path);
49  }
51  fmt::format("{}/intrinsic.json", frame_path), GetMetadata());
52  SeekTimestamp(start_time);
53  int idx = 0;
54  cloudViewer::geometry::Image im_color, im_depth;
55  for (auto tim_rgbd = NextFrame(); !IsEOF() && GetTimestamp() < end_time;
56  ++idx, tim_rgbd = NextFrame())
57 #pragma omp parallel sections
58  {
59 #pragma omp section
60  {
61  im_color = tim_rgbd.color_.ToLegacy();
62  auto color_file =
63  fmt::format("{0}/color/{1:05d}.jpg", frame_path, idx);
64  cloudViewer::io::WriteImage(color_file, im_color);
65  utility::LogDebug("Written color image to {}", color_file);
66  }
67 #pragma omp section
68  {
69  im_depth = tim_rgbd.depth_.ToLegacy();
70  auto depth_file =
71  fmt::format("{0}/depth/{1:05d}.png", frame_path, idx);
72  cloudViewer::io::WriteImage(depth_file, im_depth);
73  utility::LogDebug("Written depth image to {}", depth_file);
74  }
75  }
76  utility::LogInfo("Written {} depth and color images to {}/{{depth,color}}/",
77  idx, frame_path);
78 }
79 
80 std::unique_ptr<RGBDVideoReader> RGBDVideoReader::Create(
81  const std::string &filename) {
82 #ifdef BUILD_LIBREALSENSE
83  if (utility::ToLower(filename).compare(filename.length() - 4, 4, ".bag") ==
84  0) {
85  auto reader = std::make_unique<RSBagReader>();
86  reader->Open(filename);
87  return reader;
88  } else {
89  utility::LogError("Unsupported file format for {}.", filename);
90  }
91 #endif
93  "Build with -DBUILD_LIBREALSENSE=ON to use RealSense bag file.");
94 }
95 
96 } // namespace io
97 } // namespace t
98 } // namespace cloudViewer
Compare compare
std::string filename
filament::Texture::InternalFormat format
The Image class stores image with customizable width, height, num of channels and bytes per channel.
Definition: Image.h:33
static std::unique_ptr< RGBDVideoReader > Create(const std::string &filename)
Factory function to create object based on RGBD video file type.
virtual RGBDVideoMetadata & GetMetadata()=0
Get reference to the metadata of the RGBD video playback.
virtual bool IsEOF() const =0
Check if the RGBD video file is all read.
virtual void SaveFrames(const std::string &frame_path, uint64_t start_time_us=0, uint64_t end_time_us=UINT64_MAX)
virtual bool SeekTimestamp(uint64_t timestamp)=0
Seek to the timestamp (in us).
virtual t::geometry::RGBDImage NextFrame()=0
Get next frame from the RGBD video playback and returns the RGBD object.
virtual std::string ToString() const
Text description.
virtual uint64_t GetTimestamp() const =0
Get current timestamp (in us).
virtual bool IsOpened() const =0
Check If the RGBD video file is opened.
virtual std::string GetFilename() const =0
Return filename being read.
#define LogInfo(...)
Definition: Logging.h:81
#define LogError(...)
Definition: Logging.h:60
#define LogDebug(...)
Definition: Logging.h:90
Helper functions for the ml ops.
bool WriteImage(const std::string &filename, const geometry::Image &image, int quality=kCloudViewerImageIODefaultQuality)
bool WriteIJsonConvertibleToJSON(const std::string &filename, const cloudViewer::utility::IJsonConvertible &object)
bool MakeDirectoryHierarchy(const std::string &directory)
Definition: FileSystem.cpp:499
std::string ToLower(const std::string &s)
Convert string to the lower case.
Definition: Helper.cpp:242
Generic file read and write utility for python interface.