c-tutorial-1-depth.c
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 
00004 /*************************************************\
00005 * librealsense tutorial #1 - Accessing depth data *
00006 \*************************************************/
00007 
00008 /* First include the librealsense C header file */
00009 #include <librealsense/rs.h>
00010 #include <stdlib.h>
00011 #include <stdint.h>
00012 #include <stdio.h>
00013 
00014 /* Function calls to librealsense may raise errors of type rs_error */
00015 rs_error * e = 0;
00016 void check_error()
00017 {
00018     if (e)
00019     {
00020         printf("rs_error was raised when calling %s(%s):\n", rs_get_failed_function(e), rs_get_failed_args(e));
00021         printf("    %s\n", rs_get_error_message(e));
00022         exit(EXIT_FAILURE);
00023     }
00024 }
00025 
00026 int main()
00027 {
00028     /* Create a context object. This object owns the handles to all connected realsense devices. */
00029     rs_context * ctx = rs_create_context(RS_API_VERSION, &e);
00030     check_error();
00031     printf("There are %d connected RealSense devices.\n", rs_get_device_count(ctx, &e));
00032     check_error();
00033     if (rs_get_device_count(ctx, &e) == 0) return EXIT_FAILURE;
00034 
00035     /* This tutorial will access only a single device, but it is trivial to extend to multiple devices */
00036     rs_device * dev = rs_get_device(ctx, 0, &e);
00037     check_error();
00038     printf("\nUsing device 0, an %s\n", rs_get_device_name(dev, &e));
00039     check_error();
00040     printf("    Serial number: %s\n", rs_get_device_serial(dev, &e));
00041     check_error();
00042     printf("    Firmware version: %s\n", rs_get_device_firmware_version(dev, &e));
00043     check_error();
00044 
00045     rs_stream stream_type = RS_STREAM_DEPTH;
00046     /* Configure depth to run at VGA resolution at 30 frames per second */
00047     rs_enable_stream(dev, RS_STREAM_DEPTH, 0, 0, RS_FORMAT_Z16, 30, &e);
00048     check_error();
00049     rs_start_device(dev, &e);
00050     check_error();
00051 
00052     /* Determine depth value corresponding to one meter */
00053     const uint16_t one_meter = (uint16_t)(1.0f / rs_get_device_depth_scale(dev, &e));
00054     check_error();
00055 
00056     /* retrieve actual frame size */
00057     rs_intrinsics depth_intrin;
00058     rs_get_stream_intrinsics(dev, stream_type, &depth_intrin, &e);
00059     check_error();
00060     int width = depth_intrin.width;
00061     int height = depth_intrin.height;
00062 
00063     int rows = (height / 20);
00064     int row_lenght = (width / 10);
00065     int display_size = (rows + 1) * (row_lenght + 1);
00066 
00067     char *buffer = (char*)malloc(display_size * sizeof(char));
00068     if (NULL == buffer)
00069     {
00070         printf("Failed to allocate application memory");
00071         exit(EXIT_FAILURE);
00072     }
00073     char * out;
00074 
00075     while (1)
00076     {
00077         /* This call waits until a new coherent set of frames is available on a device */
00078         rs_wait_for_frames(dev, &e);
00079 
00080         /* Retrieve depth data, configured as 16-bit depth values */
00081         const uint16_t * depth_frame = (const uint16_t *)(rs_get_frame_data(dev, RS_STREAM_DEPTH, &e));
00082 
00083         /* 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 */
00084         out = buffer;
00085         int coverage[255] = { 0 }, x, y, i;
00086         for (y = 0; y < height; ++y)
00087         {
00088             for (x = 0; x < width; ++x)
00089             {
00090                 int depth = *depth_frame++;
00091                 if (depth > 0 && depth < one_meter) ++coverage[x / 10];
00092             }
00093 
00094             if (y % 20 == 19)
00095             {
00096                 for (i = 0; i < (row_lenght); ++i)
00097                 {
00098                     *out++ = " .:nhBXWW"[coverage[i] / 25];
00099                     coverage[i] = 0;
00100                 }
00101                 *out++ = '\n';
00102             }
00103         }
00104         *out++ = 0;
00105         printf("\n%s", buffer);
00106     }
00107 
00108     rs_stop_device(dev, &e);
00109     check_error();
00110 
00111     free(buffer);
00112 
00113     return EXIT_SUCCESS;
00114 }


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