cpp-headless.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 
5 // cpp-headless //
7 
8 // This sample captures 30 frames and writes the last frame to disk.
9 // It can be useful for debugging an embedded system with no display.
10 
11 #include <librealsense/rs.hpp>
12 
13 #include <cstdio>
14 #include <stdint.h>
15 #include <vector>
16 #include <map>
17 #include <limits>
18 #include <iostream>
19 
20 #define STB_IMAGE_WRITE_IMPLEMENTATION
22 
23 void normalize_depth_to_rgb(uint8_t rgb_image[], const uint16_t depth_image[], int width, int height)
24 {
25  for (int i = 0; i < width * height; ++i)
26  {
27  if (auto d = depth_image[i])
28  {
29  uint8_t v = d * 255 / std::numeric_limits<uint16_t>::max();
30  rgb_image[i*3 + 0] = 255 - v;
31  rgb_image[i*3 + 1] = 255 - v;
32  rgb_image[i*3 + 2] = 255 - v;
33  }
34  else
35  {
36  rgb_image[i*3 + 0] = 0;
37  rgb_image[i*3 + 1] = 0;
38  rgb_image[i*3 + 2] = 0;
39  }
40  }
41 }
42 
43 std::map<rs::stream,int> components_map =
44 {
45  { rs::stream::depth, 3 }, // RGB
46  { rs::stream::color, 3 },
47  { rs::stream::infrared , 1 }, // Monochromatic
48  { rs::stream::infrared2, 1 },
49  { rs::stream::fisheye, 1 }
50 };
51 
53 {
54  stream_record(void): frame_data(nullptr) {};
56  ~stream_record() { frame_data = nullptr;}
59  unsigned char * frame_data;
60 };
61 
62 
63 int main() try
64 {
66  //rs::log_to_file(rs::log_severity::debug, "librealsense.log");
67 
69  printf("There are %d connected RealSense devices.\n", ctx.get_device_count());
70  if(ctx.get_device_count() == 0) return EXIT_FAILURE;
71 
72  rs::device * dev = ctx.get_device(0);
73  printf("\nUsing device 0, an %s\n", dev->get_name());
74  printf(" Serial number: %s\n", dev->get_serial());
75  printf(" Firmware version: %s\n", dev->get_firmware_version());
76 
77  std::vector<stream_record> supported_streams;
78 
79  for (int i=(int)rs::capabilities::depth; i <=(int)rs::capabilities::fish_eye; i++)
80  if (dev->supports((rs::capabilities)i))
81  supported_streams.push_back(stream_record((rs::stream)i));
82 
83  for (auto & stream_record : supported_streams)
85 
86  /* activate video streaming */
87  dev->start();
88 
89  /* retrieve actual frame size for each enabled stream*/
90  for (auto & stream_record : supported_streams)
92 
93  /* Capture 30 frames to give autoexposure, etc. a chance to settle */
94  for (int i = 0; i < 30; ++i) dev->wait_for_frames();
95 
96  /* Retrieve data from all the enabled streams */
97  for (auto & stream_record : supported_streams)
98  stream_record.frame_data = const_cast<uint8_t *>((const uint8_t*)dev->get_frame_data(stream_record.stream));
99 
100  /* Transform Depth range map into color map */
101  stream_record depth = supported_streams[(int)rs::stream::depth];
102  std::vector<uint8_t> coloredDepth(depth.intrinsics.width * depth.intrinsics.height * components_map[depth.stream]);
103 
104  /* Encode depth data into color image */
105  normalize_depth_to_rgb(coloredDepth.data(), (const uint16_t *)depth.frame_data, depth.intrinsics.width, depth.intrinsics.height);
106 
107  /* Update captured data */
108  supported_streams[(int)rs::stream::depth].frame_data = coloredDepth.data();
109 
110  /* Store captured frames into current directory */
111  for (auto & captured : supported_streams)
112  {
113  std::stringstream ss;
114  ss << "cpp-headless-output-" << captured.stream << ".png";
115 
116  std::cout << "Writing " << ss.str().data() << ", " << captured.intrinsics.width << " x " << captured.intrinsics.height << " pixels" << std::endl;
117 
118  stbi_write_png(ss.str().data(),
119  captured.intrinsics.width,captured.intrinsics.height,
120  components_map[captured.stream],
121  captured.frame_data,
122  captured.intrinsics.width * components_map[captured.stream] );
123  }
124 
125  printf("wrote frames to current working directory.\n");
126  return EXIT_SUCCESS;
127 }
128 catch(const rs::error & e)
129 {
130  std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
131  return EXIT_FAILURE;
132 }
133 catch(const std::exception & e)
134 {
135  std::cerr << e.what() << std::endl;
136  return EXIT_FAILURE;
137 }
void log_to_console(log_severity min_severity)
Definition: rs.hpp:1104
Provides convenience methods relating to devices.
Definition: rs.hpp:567
intrinsics get_stream_intrinsics(stream stream) const
Retrieves intrinsic camera parameters for specific stream.
Definition: rs.hpp:788
void enable_stream(stream stream, int width, int height, format format, int framerate, output_buffer_format output_buffer_type=output_buffer_format::continous)
Enables specific stream and requests specific properties.
Definition: rs.hpp:704
unsigned char * frame_data
const std::string & get_failed_args() const
Definition: rs.hpp:315
rs_error * e
GLint GLint GLsizei GLsizei height
Definition: glext.h:112
const char * get_firmware_version() const
Retrieves version of firmware currently installed on device.
Definition: rs.hpp:608
Video stream intrinsics.
Definition: rs.hpp:252
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glext.h:112
Exposes librealsense functionality for C++ compilers.
std::map< rs::stream, int > components_map
void normalize_depth_to_rgb(uint8_t rgb_image[], const uint16_t depth_image[], int width, int height)
GLuint GLuint stream
Definition: glext.h:1774
const void * get_frame_data(stream stream) const
Retrieves contents of latest frame on a stream.
Definition: rs.hpp:1050
rs::stream stream
stream
Streams are different types of data provided by RealSense devices.
Definition: rs.hpp:24
Context.
Definition: rs.hpp:319
const char * get_name() const
Retrieves human-readable device model string.
Definition: rs.hpp:578
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
void start(rs::source source=rs::source::video)
Begins streaming on all enabled streams for this device.
Definition: rs.hpp:879
auto ctx
GLsizei const GLfloat * value
Definition: glext.h:693
int height
Definition: rs.h:303
const char * get_serial() const
Retrieves unique serial number of device.
Definition: rs.hpp:588
const GLdouble * v
Definition: glext.h:232
int width
Definition: rs.h:302
rs::intrinsics intrinsics
STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes)
stream_record(rs::stream value)
GLint GLint GLsizei width
Definition: glext.h:112
rs_device * dev
int main()
bool supports(capabilities capability) const
Determines device capabilities.
Definition: rs.hpp:1005
device * get_device(int index)
Definition: rs.hpp:354
const std::string & get_failed_function() const
Definition: rs.hpp:314
capabilities
Specifies various capabilities of a RealSense device.
Definition: rs.hpp:169
void wait_for_frames()
Blocks until new frames are available.
Definition: rs.hpp:985
int get_device_count() const
Definition: rs.hpp:343


librealsense
Author(s): Sergey Dorodnicov , Mark Horn , Reagan Lopez
autogenerated on Fri Mar 13 2020 03:16:17