cpp-tutorial-1-depth.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 // librealsense tutorial #1 - Accessing depth data //
7 
8 // First include the librealsense C++ header file
9 #include <librealsense/rs.hpp>
10 #include <cstdio>
11 
12 int main() try
13 {
14  // Create a context object. This object owns the handles to all connected realsense devices.
16  printf("There are %d connected RealSense devices.\n", ctx.get_device_count());
17  if(ctx.get_device_count() == 0) return EXIT_FAILURE;
18 
19  // This tutorial will access only a single device, but it is trivial to extend to multiple devices
20  rs::device * dev = ctx.get_device(0);
21  printf("\nUsing device 0, an %s\n", dev->get_name());
22  printf(" Serial number: %s\n", dev->get_serial());
23  printf(" Firmware version: %s\n", dev->get_firmware_version());
24 
25  // Configure depth to run at VGA resolution at 30 frames per second
26  dev->enable_stream(rs::stream::depth, 640, 480, rs::format::z16, 30);
27 
28  dev->start();
29 
30  // Determine depth value corresponding to one meter
31  const uint16_t one_meter = static_cast<uint16_t>(1.0f / dev->get_depth_scale());
32 
33  while(true)
34  {
35  // This call waits until a new coherent set of frames is available on a device
36  // Calls to get_frame_data(...) and get_frame_timestamp(...) on a device will return stable values until wait_for_frames(...) is called
37  dev->wait_for_frames();
38 
39  // Retrieve depth data, which was previously configured as a 640 x 480 image of 16-bit depth values
40  const uint16_t * depth_frame = reinterpret_cast<const uint16_t *>(dev->get_frame_data(rs::stream::depth));
41 
42  // Print a simple text-based representation of the image, by breaking it into 10x20 pixel regions and and approximating the coverage of pixels within one meter
43  char buffer[(640/10+1)*(480/20)+1];
44  char * out = buffer;
45  int coverage[64] = {};
46  for(int y=0; y<480; ++y)
47  {
48  for(int x=0; x<640; ++x)
49  {
50  int depth = *depth_frame++;
51  if(depth > 0 && depth < one_meter) ++coverage[x/10];
52  }
53 
54  if(y%20 == 19)
55  {
56  for(int & c : coverage)
57  {
58  *out++ = " .:nhBXWW"[c/25];
59  c = 0;
60  }
61  *out++ = '\n';
62  }
63  }
64  *out++ = 0;
65  printf("\n%s", buffer);
66  }
67 
68  return EXIT_SUCCESS;
69 }
70 catch(const rs::error & e)
71 {
72  // Method calls against librealsense objects may throw exceptions of type rs::error
73  printf("rs::error was thrown when calling %s(%s):\n", e.get_failed_function().c_str(), e.get_failed_args().c_str());
74  printf(" %s\n", e.what());
75  return EXIT_FAILURE;
76 }
Provides convenience methods relating to devices.
Definition: rs.hpp:567
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
const std::string & get_failed_args() const
Definition: rs.hpp:315
rs_error * e
const char * get_firmware_version() const
Retrieves version of firmware currently installed on device.
Definition: rs.hpp:608
GLint GLint GLint GLint GLint GLint y
Definition: glext.h:114
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glext.h:112
Exposes librealsense functionality for C++ compilers.
const void * get_frame_data(stream stream) const
Retrieves contents of latest frame on a stream.
Definition: rs.hpp:1050
GLuint buffer
Definition: glext.h:528
const GLubyte * c
Definition: glext.h:11542
Context.
Definition: rs.hpp:319
const char * get_name() const
Retrieves human-readable device model string.
Definition: rs.hpp:578
void start(rs::source source=rs::source::video)
Begins streaming on all enabled streams for this device.
Definition: rs.hpp:879
auto ctx
int main()
const char * get_serial() const
Retrieves unique serial number of device.
Definition: rs.hpp:588
rs_device * dev
float get_depth_scale() const
Retrieves mapping between units of depth image and meters.
Definition: rs.hpp:653
device * get_device(int index)
Definition: rs.hpp:354
const std::string & get_failed_function() const
Definition: rs.hpp:314
GLint GLint GLint GLint GLint x
Definition: glext.h:114
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