rs-save-to-disk.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015-2017 Intel Corporation. All Rights Reserved.
3 
4 #include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
5 
6 #include <fstream> // File IO
7 #include <iostream> // Terminal IO
8 #include <sstream> // Stringstreams
9 
10 // 3rd party header for writing png files
11 #define STB_IMAGE_WRITE_IMPLEMENTATION
12 #include "stb_image_write.h"
13 
14 // Helper function for writing metadata to disk as a csv file
15 void metadata_to_csv(const rs2::frame& frm, const std::string& filename);
16 
17 // This sample captures 30 frames and writes the last frame to disk.
18 // It can be useful for debugging an embedded system with no display.
19 int main(int argc, char * argv[]) try
20 {
21  // Declare depth colorizer for pretty visualization of depth data
22  rs2::colorizer color_map;
23 
24  // Declare RealSense pipeline, encapsulating the actual device and sensors
26  // Start streaming with default recommended configuration
27  pipe.start();
28 
29  // Capture 30 frames to give autoexposure, etc. a chance to settle
30  for (auto i = 0; i < 30; ++i) pipe.wait_for_frames();
31 
32  // Wait for the next set of frames from the camera. Now that autoexposure, etc.
33  // has settled, we will write these to disk
34  for (auto&& frame : pipe.wait_for_frames())
35  {
36  // We can only save video frames as pngs, so we skip the rest
37  if (auto vf = frame.as<rs2::video_frame>())
38  {
39  auto stream = frame.get_profile().stream_type();
40  // Use the colorizer to get an rgb image for the depth stream
41  if (vf.is<rs2::depth_frame>()) vf = color_map.process(frame);
42 
43  // Write images to disk
44  std::stringstream png_file;
45  png_file << "rs-save-to-disk-output-" << vf.get_profile().stream_name() << ".png";
46  stbi_write_png(png_file.str().c_str(), vf.get_width(), vf.get_height(),
47  vf.get_bytes_per_pixel(), vf.get_data(), vf.get_stride_in_bytes());
48  std::cout << "Saved " << png_file.str() << std::endl;
49 
50  // Record per-frame metadata for UVC streams
51  std::stringstream csv_file;
52  csv_file << "rs-save-to-disk-output-" << vf.get_profile().stream_name()
53  << "-metadata.csv";
54  metadata_to_csv(vf, csv_file.str());
55  }
56  }
57 
58  return EXIT_SUCCESS;
59 }
60 catch(const rs2::error & e)
61 {
62  std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
63  return EXIT_FAILURE;
64 }
65 catch(const std::exception & e)
66 {
67  std::cerr << e.what() << std::endl;
68  return EXIT_FAILURE;
69 }
70 
72 {
73  std::ofstream csv;
74 
75  csv.open(filename);
76 
77  // std::cout << "Writing metadata to " << filename << endl;
78  csv << "Stream," << rs2_stream_to_string(frm.get_profile().stream_type()) << "\nMetadata Attribute,Value\n";
79 
80  // Record all the available metadata attributes
81  for (size_t i = 0; i < RS2_FRAME_METADATA_COUNT; i++)
82  {
84  {
87  }
88  }
89 
90  csv.close();
91 }
frameset wait_for_frames(unsigned int timeout_ms=RS2_DEFAULT_TIMEOUT) const
const char * rs2_frame_metadata_to_string(rs2_frame_metadata_value metadata)
Definition: rs.cpp:1275
rs2_metadata_type get_frame_metadata(rs2_frame_metadata_value frame_metadata) const
Definition: rs_frame.hpp:497
stream_profile get_profile() const
Definition: rs_frame.hpp:557
bool supports_frame_metadata(rs2_frame_metadata_value frame_metadata) const
Definition: rs_frame.hpp:509
std::string stream_name() const
Definition: rs_frame.hpp:113
GLsizei const GLchar *const * string
GLuint GLuint stream
Definition: glext.h:1790
e
Definition: rmse.py:177
const std::string & get_failed_args() const
Definition: rs_types.hpp:117
rs2::frame process(rs2::frame frame) const override
std::ostream & cout()
void metadata_to_csv(const rs2::frame &frm, const std::string &filename)
const char * rs2_stream_to_string(rs2_stream stream)
Definition: rs.cpp:1262
STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes)
std::ostream & cerr()
int i
pipeline_profile start()
rs2_stream stream_type() const
Definition: rs_frame.hpp:39
int main(int argc, char *argv[])
const std::string & get_failed_function() const
Definition: rs_types.hpp:112
rs2_frame_metadata_value
Per-Frame-Metadata is the set of read-only properties that might be exposed for each individual frame...
Definition: rs_frame.h:29


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:40