rs-depth.c
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2017 Intel Corporation. All Rights Reserved.
3 
4 /* Include the librealsense C header files */
5 #include <librealsense2/rs.h>
9 #include "example.h"
10 
11 #include <stdlib.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 
15 
17 // These parameters are reconfigurable //
19 #define STREAM RS2_STREAM_DEPTH // rs2_stream is a types of data provided by RealSense device //
20 #define FORMAT RS2_FORMAT_Z16 // rs2_format identifies how binary data is encoded within a frame //
21 #define WIDTH 640 // Defines the number of columns for each frame or zero for auto resolve//
22 #define HEIGHT 0 // Defines the number of lines for each frame or zero for auto resolve //
23 #define FPS 30 // Defines the rate of frames per second //
24 #define STREAM_INDEX 0 // Defines the stream index, used for multiple streams of the same type //
25 #define HEIGHT_RATIO 20 // Defines the height ratio between the original frame to the new frame //
26 #define WIDTH_RATIO 10 // Defines the width ratio between the original frame to the new frame //
27 
29 // The number of meters represented by a single depth unit
30 float get_depth_unit_value(const rs2_device* const dev)
31 {
32  rs2_error* e = 0;
33  rs2_sensor_list* sensor_list = rs2_query_sensors(dev, &e);
34  check_error(e);
35 
36  int num_of_sensors = rs2_get_sensors_count(sensor_list, &e);
37  check_error(e);
38 
39  float depth_scale = 0;
40  int is_depth_sensor_found = 0;
41  int i;
42  for (i = 0; i < num_of_sensors; ++i)
43  {
44  rs2_sensor* sensor = rs2_create_sensor(sensor_list, i, &e);
45  check_error(e);
46 
47  // Check if the given sensor can be extended to depth sensor interface
48  is_depth_sensor_found = rs2_is_sensor_extendable_to(sensor, RS2_EXTENSION_DEPTH_SENSOR, &e);
49  check_error(e);
50 
51  if (1 == is_depth_sensor_found)
52  {
53  depth_scale = rs2_get_option((const rs2_options*)sensor, RS2_OPTION_DEPTH_UNITS, &e);
54  check_error(e);
55  rs2_delete_sensor(sensor);
56  break;
57  }
58  rs2_delete_sensor(sensor);
59  }
60  rs2_delete_sensor_list(sensor_list);
61 
62  if (0 == is_depth_sensor_found)
63  {
64  printf("Depth sensor not found!\n");
65  exit(EXIT_FAILURE);
66  }
67 
68  return depth_scale;
69 }
70 
71 
72 int main()
73 {
74  rs2_error* e = 0;
75 
76  // Create a context object. This object owns the handles to all connected realsense devices.
77  // The returned object should be released with rs2_delete_context(...)
79  check_error(e);
80 
81  /* Get a list of all the connected devices. */
82  // The returned object should be released with rs2_delete_device_list(...)
83  rs2_device_list* device_list = rs2_query_devices(ctx, &e);
84  check_error(e);
85 
86  int dev_count = rs2_get_device_count(device_list, &e);
87  check_error(e);
88  printf("There are %d connected RealSense devices.\n", dev_count);
89  if (0 == dev_count)
90  return EXIT_FAILURE;
91 
92  // Get the first connected device
93  // The returned object should be released with rs2_delete_device(...)
94  rs2_device* dev = rs2_create_device(device_list, 0, &e);
95  check_error(e);
96 
97  print_device_info(dev);
98 
99  /* Determine depth value corresponding to one meter */
100  uint16_t one_meter = (uint16_t)(1.0f / get_depth_unit_value(dev));
101 
102  // Create a pipeline to configure, start and stop camera streaming
103  // The returned object should be released with rs2_delete_pipeline(...)
105  check_error(e);
106 
107  // Create a config instance, used to specify hardware configuration
108  // The retunred object should be released with rs2_delete_config(...)
110  check_error(e);
111 
112  // Request a specific configuration
114  check_error(e);
115 
116  // Start the pipeline streaming
117  // The retunred object should be released with rs2_delete_pipeline_profile(...)
119  if (e)
120  {
121  printf("The connected device doesn't support depth streaming!\n");
122  exit(EXIT_FAILURE);
123  }
124 
125  rs2_stream_profile_list* stream_profile_list = rs2_pipeline_profile_get_streams(pipeline_profile, &e);
126  if (e)
127  {
128  printf("Failed to create stream profile list!\n");
129  exit(EXIT_FAILURE);
130  }
131 
132  rs2_stream_profile* stream_profile = (rs2_stream_profile*)rs2_get_stream_profile(stream_profile_list, 0, &e);
133  if (e)
134  {
135  printf("Failed to create stream profile!\n");
136  exit(EXIT_FAILURE);
137  }
138 
139  rs2_stream stream; rs2_format format; int index; int unique_id; int framerate;
140  rs2_get_stream_profile_data(stream_profile, &stream, &format, &index, &unique_id, &framerate, &e);
141  if (e)
142  {
143  printf("Failed to get stream profile data!\n");
144  exit(EXIT_FAILURE);
145  }
146 
147  int width; int height;
148  rs2_get_video_stream_resolution(stream_profile, &width, &height, &e);
149  if (e)
150  {
151  printf("Failed to get video stream resolution data!\n");
152  exit(EXIT_FAILURE);
153  }
154  int rows = height / HEIGHT_RATIO;
155  int row_length = width / WIDTH_RATIO;
156  int display_size = (rows + 1) * (row_length + 1);
157  int buffer_size = display_size * sizeof(char);
158 
159  char* buffer = calloc(display_size, sizeof(char));
160  char* out = NULL;
161 
162  while (1)
163  {
164  // This call waits until a new composite_frame is available
165  // composite_frame holds a set of frames. It is used to prevent frame drops
166  // The returned object should be released with rs2_release_frame(...)
168  check_error(e);
169 
170  // Returns the number of frames embedded within the composite frame
171  int num_of_frames = rs2_embedded_frames_count(frames, &e);
172  check_error(e);
173 
174  int i;
175  for (i = 0; i < num_of_frames; ++i)
176  {
177  // The retunred object should be released with rs2_release_frame(...)
178  rs2_frame* frame = rs2_extract_frame(frames, i, &e);
179  check_error(e);
180 
181  // Check if the given frame can be extended to depth frame interface
182  // Accept only depth frames and skip other frames
184  {
185  rs2_release_frame(frame);
186  continue;
187  }
188 
189  /* Retrieve depth data, configured as 16-bit depth values */
190  const uint16_t* depth_frame_data = (const uint16_t*)(rs2_get_frame_data(frame, &e));
191  check_error(e);
192 
193  /* Print a simple text-based representation of the image, by breaking it into 10x5 pixel regions and approximating the coverage of pixels within one meter */
194  out = buffer;
195  int x, y, i;
196  int* coverage = calloc(row_length, sizeof(int));
197 
198  for (y = 0; y < height; ++y)
199  {
200  for (x = 0; x < width; ++x)
201  {
202  // Create a depth histogram to each row
203  int coverage_index = x / WIDTH_RATIO;
204  int depth = *depth_frame_data++;
205  if (depth > 0 && depth < one_meter)
206  ++coverage[coverage_index];
207  }
208 
209  if ((y % HEIGHT_RATIO) == (HEIGHT_RATIO-1))
210  {
211  for (i = 0; i < (row_length); ++i)
212  {
213  static const char* pixels = " .:nhBXWW";
214  int pixel_index = (coverage[i] / (HEIGHT_RATIO * WIDTH_RATIO / sizeof(pixels)));
215  *out++ = pixels[pixel_index];
216  coverage[i] = 0;
217  }
218  *out++ = '\n';
219  }
220  }
221  *out++ = 0;
222  printf("\n%s", buffer);
223 
224  free(coverage);
225  rs2_release_frame(frame);
226  }
227 
228  rs2_release_frame(frames);
229  }
230 
231  // Stop the pipeline streaming
232  rs2_pipeline_stop(pipeline, &e);
233  check_error(e);
234 
235  // Release resources
236  free(buffer);
237  rs2_delete_pipeline_profile(pipeline_profile);
238  rs2_delete_stream_profiles_list(stream_profile_list);
239  rs2_delete_stream_profile(stream_profile);
240  rs2_delete_config(config);
241  rs2_delete_pipeline(pipeline);
242  rs2_delete_device(dev);
243  rs2_delete_device_list(device_list);
244  rs2_delete_context(ctx);
245 
246  return EXIT_SUCCESS;
247 }
int rs2_get_sensors_count(const rs2_sensor_list *info_list, rs2_error **error)
Definition: rs.cpp:267
void rs2_config_enable_stream(rs2_config *config, rs2_stream stream, int index, int width, int height, rs2_format format, int framerate, rs2_error **error)
Definition: rs.cpp:1928
void print_device_info(rs2_device *dev)
Definition: example.h:19
#define WIDTH_RATIO
Definition: rs-depth.c:26
float rs2_get_option(const rs2_options *options, rs2_option option, rs2_error **error)
Definition: rs.cpp:628
GLint y
Exposes RealSense processing-block functionality for C compilers.
#define RS2_API_VERSION
Definition: rs.h:41
int main()
Definition: rs-depth.c:72
rs2_device_list * rs2_query_devices(const rs2_context *context, rs2_error **error)
Definition: rs.cpp:208
#define STREAM_INDEX
Definition: rs-depth.c:24
float get_depth_unit_value(const rs2_device *const dev)
Definition: rs-depth.c:30
int rs2_is_frame_extendable_to(const rs2_frame *frame, rs2_extension extension_type, rs2_error **error)
Definition: rs.cpp:1461
void rs2_get_video_stream_resolution(const rs2_stream_profile *mode, int *width, int *height, rs2_error **error)
Definition: rs.cpp:466
rs2_sensor * rs2_create_sensor(const rs2_sensor_list *list, int index, rs2_error **error)
Definition: rs.cpp:308
void rs2_delete_device(rs2_device *device)
Definition: rs.cpp:301
rs2_context * rs2_create_context(int api_version, rs2_error **error)
Creates RealSense context that is required for the rest of the API.
Definition: rs.cpp:163
#define HEIGHT_RATIO
Definition: rs-depth.c:25
rs2_config * rs2_create_config(rs2_error **error)
Definition: rs.cpp:1914
void rs2_delete_context(rs2_context *context)
Frees the relevant context object.
Definition: rs.cpp:171
rs2_pipeline * rs2_create_pipeline(rs2_context *ctx, rs2_error **error)
Definition: rs.cpp:1756
#define FPS
Definition: rs-depth.c:23
rs2_device * rs2_create_device(const rs2_device_list *info_list, int index, rs2_error **error)
Definition: rs.cpp:289
GLint GLint GLsizei GLsizei GLsizei depth
unsigned short uint16_t
Definition: stdint.h:79
GLuint GLuint stream
Definition: glext.h:1790
void check_error(rs2_error *e)
Definition: example.h:9
e
Definition: rmse.py:177
GLenum GLfloat * buffer
Exposes librealsense functionality for C compilers.
GLuint index
GLdouble f
rs2_stream_profile_list * rs2_pipeline_profile_get_streams(rs2_pipeline_profile *profile, rs2_error **error)
Definition: rs.cpp:1898
rs2_frame * rs2_pipeline_wait_for_frames(rs2_pipeline *pipe, unsigned int timeout_ms, rs2_error **error)
Definition: rs.cpp:1774
void rs2_get_stream_profile_data(const rs2_stream_profile *mode, rs2_stream *stream, rs2_format *format, int *index, int *unique_id, int *framerate, rs2_error **error)
Definition: rs.cpp:484
Exposes RealSense frame functionality for C compilers.
#define FORMAT
Definition: rs-depth.c:20
int rs2_embedded_frames_count(rs2_frame *composite, rs2_error **error)
Definition: rs.cpp:2144
GLdouble x
void rs2_delete_sensor(rs2_sensor *sensor)
Definition: rs.cpp:320
void rs2_delete_stream_profile(rs2_stream_profile *mode)
Definition: rs.cpp:512
GLint GLsizei GLsizei height
GLint GLint GLsizei GLint GLenum format
const rs2_stream_profile * rs2_get_stream_profile(const rs2_stream_profile_list *list, int index, rs2_error **error)
Definition: rs.cpp:351
Definition: api.h:28
#define STREAM
Definition: rs-depth.c:19
rs2_sensor_list * rs2_query_sensors(const rs2_device *device, rs2_error **error)
Definition: rs.cpp:236
rs2_format
A stream&#39;s format identifies how binary data is encoded within a frame.
Definition: rs_sensor.h:59
#define RS2_DEFAULT_TIMEOUT
Definition: rs_config.h:13
GLint GLint GLsizei GLint GLenum GLenum const void * pixels
Exposes sensor options functionality for C compilers.
rs2_stream
Streams are different types of data provided by RealSense devices.
Definition: rs_sensor.h:42
static const textual_icon exit
Definition: model-views.h:254
rs2_pipeline_profile * rs2_pipeline_start_with_config(rs2_pipeline *pipe, rs2_config *config, rs2_error **error)
Definition: rs.cpp:1834
int rs2_is_sensor_extendable_to(const rs2_sensor *sensor, rs2_extension extension, rs2_error **error)
Definition: rs.cpp:1394
void rs2_delete_stream_profiles_list(rs2_stream_profile_list *list)
Definition: rs.cpp:367
void rs2_pipeline_stop(rs2_pipeline *pipe, rs2_error **error)
Definition: rs.cpp:1766
#define WIDTH
Definition: rs-depth.c:21
void rs2_delete_config(rs2_config *config)
Definition: rs.cpp:1920
void rs2_delete_sensor_list(rs2_sensor_list *info_list)
Definition: rs.cpp:282
#define NULL
Definition: tinycthread.c:47
int i
void rs2_release_frame(rs2_frame *frame)
Definition: rs.cpp:993
rs2_frame * rs2_extract_frame(rs2_frame *composite, int index, rs2_error **error)
Definition: rs.cpp:2114
void rs2_delete_device_list(rs2_device_list *info_list)
Definition: rs.cpp:275
void rs2_delete_pipeline_profile(rs2_pipeline_profile *profile)
Definition: rs.cpp:1905
int rs2_get_device_count(const rs2_device_list *info_list, rs2_error **error)
Definition: rs.cpp:259
#define HEIGHT
Definition: rs-depth.c:22
void rs2_delete_pipeline(rs2_pipeline *pipe)
Definition: rs.cpp:1819
struct rs2_frame rs2_frame
Definition: rs_types.h:261
GLint GLsizei width
const void * rs2_get_frame_data(const rs2_frame *frame, rs2_error **error)
Definition: rs.cpp:940


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