forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpp-tutorial-1-depth.cpp
75 lines (64 loc) · 2.87 KB
/
cpp-tutorial-1-depth.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2015 Intel Corporation. All Rights Reserved.
/////////////////////////////////////////////////////
// librealsense tutorial #1 - Accessing depth data //
/////////////////////////////////////////////////////
// First include the librealsense C++ header file
#include <librealsense/rs.hpp>
#include <cstdio>
int main() try
{
// Create a context object. This object owns the handles to all connected realsense devices.
rs::context ctx;
printf("There are %d connected RealSense devices.\n", ctx.get_device_count());
if(ctx.get_device_count() == 0) return EXIT_FAILURE;
// This tutorial will access only a single device, but it is trivial to extend to multiple devices
rs::device * dev = ctx.get_device(0);
printf("\nUsing device 0, an %s\n", dev->get_name());
printf(" Serial number: %s\n", dev->get_serial());
printf(" Firmware version: %s\n", dev->get_firmware_version());
// Configure depth to run at VGA resolution at 30 frames per second
dev->enable_stream(rs::stream::depth, 640, 480, rs::format::z16, 30);
dev->start();
// Determine depth value corresponding to one meter
const uint16_t one_meter = static_cast<uint16_t>(1.0f / dev->get_depth_scale());
while(true)
{
// This call waits until a new coherent set of frames is available on a device
// Calls to get_frame_data(...) and get_frame_timestamp(...) on a device will return stable values until wait_for_frames(...) is called
dev->wait_for_frames();
// Retrieve depth data, which was previously configured as a 640 x 480 image of 16-bit depth values
const uint16_t * depth_frame = reinterpret_cast<const uint16_t *>(dev->get_frame_data(rs::stream::depth));
// 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
char buffer[(640/10+1)*(480/20)+1];
char * out = buffer;
int coverage[64] = {};
for(int y=0; y<480; ++y)
{
for(int x=0; x<640; ++x)
{
int depth = *depth_frame++;
if(depth > 0 && depth < one_meter) ++coverage[x/10];
}
if(y%20 == 19)
{
for(int & c : coverage)
{
*out++ = " .:nhBXWW"[c/25];
c = 0;
}
*out++ = '\n';
}
}
*out++ = 0;
printf("\n%s", buffer);
}
return EXIT_SUCCESS;
}
catch(const rs::error & e)
{
// Method calls against librealsense objects may throw exceptions of type rs::error
printf("rs::error was thrown when calling %s(%s):\n", e.get_failed_function().c_str(), e.get_failed_args().c_str());
printf(" %s\n", e.what());
return EXIT_FAILURE;
}