cpp-headless.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 // cpp-headless  //
00007 
00008 // This sample captures 30 frames and writes the last frame to disk.
00009 // It can be useful for debugging an embedded system with no display.
00010 
00011 #include <librealsense/rs.hpp>
00012 
00013 #include <cstdio>
00014 #include <stdint.h>
00015 #include <vector>
00016 #include <map>
00017 #include <limits>
00018 #include <iostream>
00019 
00020 #define STB_IMAGE_WRITE_IMPLEMENTATION
00021 #include "third_party/stb_image_write.h"
00022 
00023 void normalize_depth_to_rgb(uint8_t rgb_image[], const uint16_t depth_image[], int width, int height)
00024 {
00025     for (int i = 0; i < width * height; ++i)
00026     {
00027         if (auto d = depth_image[i])
00028         {
00029             uint8_t v = d * 255 / std::numeric_limits<uint16_t>::max();
00030             rgb_image[i*3 + 0] = 255 - v;
00031             rgb_image[i*3 + 1] = 255 - v;
00032             rgb_image[i*3 + 2] = 255 - v;
00033         }
00034         else
00035         {
00036             rgb_image[i*3 + 0] = 0;
00037             rgb_image[i*3 + 1] = 0;
00038             rgb_image[i*3 + 2] = 0;
00039         }
00040     }
00041 }
00042 
00043 std::map<rs::stream,int> components_map =
00044 {
00045     { rs::stream::depth,     3  },      // RGB
00046     { rs::stream::color,     3  },
00047     { rs::stream::infrared , 1  },      // Monochromatic
00048     { rs::stream::infrared2, 1  },
00049     { rs::stream::fisheye,   1  }
00050 };
00051 
00052 struct stream_record
00053 {
00054     stream_record(void): frame_data(nullptr) {};
00055     stream_record(rs::stream value): stream(value), frame_data(nullptr) {};
00056     ~stream_record() { frame_data = nullptr;}
00057     rs::stream          stream;
00058     rs::intrinsics      intrinsics;
00059     unsigned char   *   frame_data;
00060 };
00061 
00062 
00063 int main() try
00064 {
00065     rs::log_to_console(rs::log_severity::warn);
00066     //rs::log_to_file(rs::log_severity::debug, "librealsense.log");
00067 
00068     rs::context ctx;
00069     printf("There are %d connected RealSense devices.\n", ctx.get_device_count());
00070     if(ctx.get_device_count() == 0) return EXIT_FAILURE;
00071 
00072     rs::device * dev = ctx.get_device(0);
00073     printf("\nUsing device 0, an %s\n", dev->get_name());
00074     printf("    Serial number: %s\n", dev->get_serial());
00075     printf("    Firmware version: %s\n", dev->get_firmware_version());
00076 
00077     std::vector<stream_record> supported_streams;
00078 
00079     for (int i=(int)rs::capabilities::depth; i <=(int)rs::capabilities::fish_eye; i++)
00080         if (dev->supports((rs::capabilities)i))
00081             supported_streams.push_back(stream_record((rs::stream)i));
00082 
00083     for (auto & stream_record : supported_streams)
00084         dev->enable_stream(stream_record.stream, rs::preset::best_quality);
00085 
00086     /* activate video streaming */
00087     dev->start();
00088 
00089     /* retrieve actual frame size for each enabled stream*/
00090     for (auto & stream_record : supported_streams)
00091         stream_record.intrinsics = dev->get_stream_intrinsics(stream_record.stream);
00092 
00093     /* Capture 30 frames to give autoexposure, etc. a chance to settle */
00094     for (int i = 0; i < 30; ++i) dev->wait_for_frames();
00095 
00096     /* Retrieve data from all the enabled streams */
00097     for (auto & stream_record : supported_streams)
00098         stream_record.frame_data = const_cast<uint8_t *>((const uint8_t*)dev->get_frame_data(stream_record.stream));
00099 
00100     /* Transform Depth range map into color map */
00101     stream_record depth = supported_streams[(int)rs::stream::depth];
00102     std::vector<uint8_t> coloredDepth(depth.intrinsics.width * depth.intrinsics.height * components_map[depth.stream]);
00103 
00104     /* Encode depth data into color image */
00105     normalize_depth_to_rgb(coloredDepth.data(), (const uint16_t *)depth.frame_data, depth.intrinsics.width, depth.intrinsics.height);
00106 
00107     /* Update captured data */
00108     supported_streams[(int)rs::stream::depth].frame_data = coloredDepth.data();
00109 
00110     /* Store captured frames into current directory */
00111     for (auto & captured : supported_streams)
00112     {
00113         std::stringstream ss;
00114         ss << "cpp-headless-output-" << captured.stream << ".png";
00115 
00116         std::cout << "Writing " << ss.str().data() << ", " << captured.intrinsics.width << " x " << captured.intrinsics.height << " pixels"   << std::endl;
00117 
00118         stbi_write_png(ss.str().data(),
00119             captured.intrinsics.width,captured.intrinsics.height,
00120             components_map[captured.stream],
00121             captured.frame_data,
00122             captured.intrinsics.width * components_map[captured.stream] );
00123     }
00124 
00125     printf("wrote frames to current working directory.\n");
00126     return EXIT_SUCCESS;
00127 }
00128 catch(const rs::error & e)
00129 {
00130     std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n    " << e.what() << std::endl;
00131     return EXIT_FAILURE;
00132 }
00133 catch(const std::exception & e)
00134 {
00135     std::cerr << e.what() << std::endl;
00136     return EXIT_FAILURE;
00137 }


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