cpp-tutorial-1-depth.cpp
Go to the documentation of this file.
00001 // License: Apache 2.0. See LICENSE file in root directory.
00002 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
00003 
00005 // librealsense tutorial #1 - Accessing depth data //
00007 
00008 // First include the librealsense C++ header file
00009 #include <librealsense/rs.hpp>
00010 #include <cstdio>
00011 
00012 int main() try
00013 {
00014     // Create a context object. This object owns the handles to all connected realsense devices.
00015     rs::context ctx;
00016     printf("There are %d connected RealSense devices.\n", ctx.get_device_count());
00017     if(ctx.get_device_count() == 0) return EXIT_FAILURE;
00018 
00019     // This tutorial will access only a single device, but it is trivial to extend to multiple devices
00020     rs::device * dev = ctx.get_device(0);
00021     printf("\nUsing device 0, an %s\n", dev->get_name());
00022     printf("    Serial number: %s\n", dev->get_serial());
00023     printf("    Firmware version: %s\n", dev->get_firmware_version());
00024 
00025     // Configure depth to run at VGA resolution at 30 frames per second
00026     dev->enable_stream(rs::stream::depth, 640, 480, rs::format::z16, 30);
00027 
00028     dev->start();
00029 
00030     // Determine depth value corresponding to one meter
00031     const uint16_t one_meter = static_cast<uint16_t>(1.0f / dev->get_depth_scale());
00032 
00033     while(true)
00034     {
00035         // This call waits until a new coherent set of frames is available on a device
00036         // Calls to get_frame_data(...) and get_frame_timestamp(...) on a device will return stable values until wait_for_frames(...) is called
00037         dev->wait_for_frames();
00038 
00039         // Retrieve depth data, which was previously configured as a 640 x 480 image of 16-bit depth values
00040         const uint16_t * depth_frame = reinterpret_cast<const uint16_t *>(dev->get_frame_data(rs::stream::depth));
00041 
00042         // 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
00043         char buffer[(640/10+1)*(480/20)+1];
00044         char * out = buffer;
00045         int coverage[64] = {};
00046         for(int y=0; y<480; ++y)
00047         {
00048             for(int x=0; x<640; ++x)
00049             {
00050                 int depth = *depth_frame++;
00051                 if(depth > 0 && depth < one_meter) ++coverage[x/10];
00052             }
00053 
00054             if(y%20 == 19)
00055             {
00056                 for(int & c : coverage)
00057                 {
00058                     *out++ = " .:nhBXWW"[c/25];
00059                     c = 0;
00060                 }
00061                 *out++ = '\n';
00062             }
00063         }
00064         *out++ = 0;
00065         printf("\n%s", buffer);
00066     }
00067     
00068     return EXIT_SUCCESS;
00069 }
00070 catch(const rs::error & e)
00071 {
00072     // Method calls against librealsense objects may throw exceptions of type rs::error
00073     printf("rs::error was thrown when calling %s(%s):\n", e.get_failed_function().c_str(), e.get_failed_args().c_str());
00074     printf("    %s\n", e.what());
00075     return EXIT_FAILURE;
00076 }


librealsense
Author(s): Sergey Dorodnicov , Mark Horn , Reagan Lopez
autogenerated on Tue Jun 25 2019 19:54:38