c-tutorial-1-depth.c
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 
4 /*************************************************\
5 * librealsense tutorial #1 - Accessing depth data *
6 \*************************************************/
7 
8 /* First include the librealsense C header file */
9 #include <librealsense/rs.h>
10 #include <stdlib.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 
14 /* Function calls to librealsense may raise errors of type rs_error */
15 rs_error * e = 0;
17 {
18  if (e)
19  {
20  printf("rs_error was raised when calling %s(%s):\n", rs_get_failed_function(e), rs_get_failed_args(e));
21  printf(" %s\n", rs_get_error_message(e));
22  exit(EXIT_FAILURE);
23  }
24 }
25 
26 int main()
27 {
28  /* Create a context object. This object owns the handles to all connected realsense devices. */
30  check_error();
31  printf("There are %d connected RealSense devices.\n", rs_get_device_count(ctx, &e));
32  check_error();
33  if (rs_get_device_count(ctx, &e) == 0) return EXIT_FAILURE;
34 
35  /* This tutorial will access only a single device, but it is trivial to extend to multiple devices */
36  rs_device * dev = rs_get_device(ctx, 0, &e);
37  check_error();
38  printf("\nUsing device 0, an %s\n", rs_get_device_name(dev, &e));
39  check_error();
40  printf(" Serial number: %s\n", rs_get_device_serial(dev, &e));
41  check_error();
42  printf(" Firmware version: %s\n", rs_get_device_firmware_version(dev, &e));
43  check_error();
44 
45  rs_stream stream_type = RS_STREAM_DEPTH;
46  /* Configure depth to run at VGA resolution at 30 frames per second */
47  rs_enable_stream(dev, RS_STREAM_DEPTH, 0, 0, RS_FORMAT_Z16, 30, &e);
48  check_error();
49  rs_start_device(dev, &e);
50  check_error();
51 
52  /* Determine depth value corresponding to one meter */
53  const uint16_t one_meter = (uint16_t)(1.0f / rs_get_device_depth_scale(dev, &e));
54  check_error();
55 
56  /* retrieve actual frame size */
57  rs_intrinsics depth_intrin;
58  rs_get_stream_intrinsics(dev, stream_type, &depth_intrin, &e);
59  check_error();
60  int width = depth_intrin.width;
61  int height = depth_intrin.height;
62 
63  int rows = (height / 20);
64  int row_lenght = (width / 10);
65  int display_size = (rows + 1) * (row_lenght + 1);
66 
67  char *buffer = (char*)malloc(display_size * sizeof(char));
68  if (NULL == buffer)
69  {
70  printf("Failed to allocate application memory");
71  exit(EXIT_FAILURE);
72  }
73  char * out;
74 
75  while (1)
76  {
77  /* This call waits until a new coherent set of frames is available on a device */
78  rs_wait_for_frames(dev, &e);
79 
80  /* Retrieve depth data, configured as 16-bit depth values */
81  const uint16_t * depth_frame = (const uint16_t *)(rs_get_frame_data(dev, RS_STREAM_DEPTH, &e));
82 
83  /* Print a simple text-based representation of the image, by breaking it into 10x5 pixel regions and and approximating the coverage of pixels within one meter */
84  out = buffer;
85  int coverage[255] = { 0 }, x, y, i;
86  for (y = 0; y < height; ++y)
87  {
88  for (x = 0; x < width; ++x)
89  {
90  int depth = *depth_frame++;
91  if (depth > 0 && depth < one_meter) ++coverage[x / 10];
92  }
93 
94  if (y % 20 == 19)
95  {
96  for (i = 0; i < (row_lenght); ++i)
97  {
98  *out++ = " .:nhBXWW"[coverage[i] / 25];
99  coverage[i] = 0;
100  }
101  *out++ = '\n';
102  }
103  }
104  *out++ = 0;
105  printf("\n%s", buffer);
106  }
107 
108  rs_stop_device(dev, &e);
109  check_error();
110 
111  free(buffer);
112 
113  return EXIT_SUCCESS;
114 }
#define RS_API_VERSION
Definition: rs.h:28
void rs_get_stream_intrinsics(const rs_device *device, rs_stream stream, rs_intrinsics *intrin, rs_error **error)
Retrieves intrinsic camera parameters for a specific stream.
Definition: rs.cpp:289
Definition: rs.cpp:16
rs_error * e
int rs_get_device_count(const rs_context *context, rs_error **error)
Determines number of connected devices.
Definition: rs.cpp:113
GLint GLint GLsizei GLsizei height
Definition: glext.h:112
GLint GLint GLint GLint GLint GLint y
Definition: glext.h:114
const void * rs_get_frame_data(const rs_device *device, rs_stream stream, rs_error **error)
Retrieves the contents of the latest frame on a stream.
Definition: rs.cpp:490
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glext.h:112
const char * rs_get_device_name(const rs_device *device, rs_error **error)
Retrieves human-readable device model string.
Definition: rs.cpp:128
const char * rs_get_error_message(const rs_error *error)
Returns static pointer to error message.
Definition: rs.cpp:751
float rs_get_device_depth_scale(const rs_device *device, rs_error **error)
Retrieves mapping between the units of the depth image and meters.
Definition: rs.cpp:420
Exposes librealsense functionality for C compilers.
GLuint buffer
Definition: glext.h:528
void check_error()
const char * rs_get_failed_function(const rs_error *error)
Returns static pointer to name of a failing function in case of error.
Definition: rs.cpp:749
rs_context * rs_create_context(int api_version, rs_error **error)
Creates RealSense context that is required for the rest of the API.
Definition: rs.cpp:75
GLfloat f
Definition: glext.h:1868
const char * rs_get_device_firmware_version(const rs_device *device, rs_error **error)
Retrieves the version of the firmware currently installed on the device.
Definition: rs.cpp:156
void rs_enable_stream(rs_device *device, rs_stream stream, int width, int height, rs_format format, int framerate, rs_error **error)
Enables a specific stream and requests specific properties.
Definition: rs.cpp:220
const char * rs_get_failed_args(const rs_error *error)
Returns static pointer to arguments of a failing function in case of error.
Definition: rs.cpp:750
auto ctx
int height
Definition: rs.h:303
int width
Definition: rs.h:302
Video stream intrinsics.
Definition: rs.h:300
int main()
rs_stream
Streams are different types of data provided by RealSense devices.
Definition: rs.h:33
GLint GLint GLsizei width
Definition: glext.h:112
rs_device * dev
rs_device * rs_get_device(rs_context *context, int index, rs_error **error)
Retrieves connected device by index.
Definition: rs.cpp:120
const char * rs_get_device_serial(const rs_device *device, rs_error **error)
Retrieves unique serial number of the device.
Definition: rs.cpp:135
void rs_wait_for_frames(rs_device *device, rs_error **error)
Blocks until new frames are available.
Definition: rs.cpp:428
void rs_start_device(rs_device *device, rs_error **error)
Begins streaming on all enabled streams for this device.
Definition: rs.cpp:383
void rs_stop_device(rs_device *device, rs_error **error)
Ends data acquisition for the specified source providers.
Definition: rs.cpp:398
GLint GLint GLint GLint GLint x
Definition: glext.h:114


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