5

I'm trying to tune my depth camera (RealSense D435) to get better/different depth data. Therefore i want to experiment with the different pre sets from intel's adviced parameter configuration as explained in the intel SDK. Intel advices to use the pre sets and to not try to tune and fiddle with the 50+ paramaters yourself.

As much as i'd like to ignore that comment of intel, i am unable to even upload/read/process the pre set configuration pre sets which are provided as a .JSON file (C++). Is anyone able to show a code snippet in which a .JSON file is succesfully used to change the advanced settings of the generated depth image of a Realsense D400 series camera?

Wouter61636
  • 91
  • 1
  • 5
  • Does the Intel SDK include a JSON lib? If not, get one first. [RapidJSON](https://github.com/Tencent/rapidjson) and [JSON for Modern C++](https://github.com/nlohmann/json) are two popular libs. – Ted Lyngmo Nov 28 '18 at 13:18
  • Yes, the JSON libs are provided on (https://github.com/IntelRealSense/librealsense/wiki/D400-Series-Visual-Presets) – Wouter61636 Nov 28 '18 at 13:51
  • Great, then show the code you're written to try to read the preset configuration as a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Ted Lyngmo Nov 28 '18 at 13:56
  • Well, i do like to see an example on how to do this since i have no clue. From the rs_option.h headerfile i can find the following declerations: /** \brief For RS400 devices: provides optimized settings (presets) for specific types of usage. */ typedef enum rs2_rs400_visual_preset { RS2_RS400_VISUAL_PRESET_REMOVE_IR_PATTERN, RS2_RS400_VISUAL_PRESET_COUNT } rs2_rs400_visual_preset; const char* rs2_rs400_visual_preset_to_string(rs2_rs400_visual_preset preset); – Wouter61636 Nov 28 '18 at 14:36
  • I am sorry, i am having some trouble with using enter and shift+enter while typing a comment and have reached my maximum number of edits. – Wouter61636 Nov 28 '18 at 14:49
  • Another code snippet i found in rs_advanced_mode.hpp reading a most promising decleration: ` void load_json(const std::string& json_content) { rs2_error* e = nullptr; rs2_load_json(_dev.get(), json_content.data(), (unsigned int)json_content.size(), &e); rs2::error::handle(e); } ` Can anyone explain on how to either use the pre set configurations of my previous comment or how to use the above described load_json function? – Wouter61636 Nov 28 '18 at 14:49
  • Dont add it as comment. Edit your original question. – Ted Lyngmo Nov 28 '18 at 17:13

1 Answers1

4

I found out how to do it and here is the code snippet including some extra image retrieving code that has to be put in Main():

// Obtain a list of devices currently present on the system
context ctx;
auto devices = ctx.query_devices();
size_t device_count = devices.size();
if (!device_count)
{
    cout <<"No device detected. Is it plugged in?\n";
    return EXIT_SUCCESS;
}

// Get the first connected device
auto dev = devices[0];

// Enter advanced mode
   if (dev.is<rs400::advanced_mode>())
   {
       // Get the advanced mode functionality
       auto advanced_mode_dev = dev.as<rs400::advanced_mode>();

       // Load and configure .json file to device
       ifstream t("./presets/ShortRangePreset.json");
       string str((istreambuf_iterator<char>(t)), istreambuf_iterator<char>());
       advanced_mode_dev.load_json(str);
   }
   else
   {
       cout << "Current device doesn't support advanced-mode!\n";
       return EXIT_FAILURE;
   }

//Contruct a pipeline which abstracts the device
    rs2::pipeline pipe;
    rs2::device selected_device = dev;
    auto depth_sensor = selected_device.first<rs2::depth_sensor>();

    //Create a configuration for configuring the pipeline with a non default profile
    rs2::config cfg;

    //Add desired streams to configuration
    cfg.enable_stream(RS2_STREAM_INFRARED,  ImageWidth_px, ImageHeight_px, RS2_FORMAT_Y8, FrameRate);
    cfg.enable_stream(RS2_STREAM_DEPTH,     ImageWidth_px, ImageHeight_px, RS2_FORMAT_Z16, FrameRate);

    //Instruct pipeline to start streaming with the requested configuration
    pipe.start(cfg);

    // Camera warmup - dropping several first frames to let auto-exposure stabilize
    rs2::frameset frames;
    for(int i = 0; i < 5; i++)
    {
        //Wait for all configured streams to produce a frame
        frames = pipe.wait_for_frames();
    }

    bool quit = false;
    char key;

    while (!quit){

        frames = pipe.wait_for_frames();

        //Get each frame
        rs2::frame          ir_frame    = frames.first(RS2_STREAM_INFRARED);
        rs2::depth_frame    depth_frame = frames.get_depth_frame();

        // Creating OpenCV matrix from IR image
        Mat ir      (Size(ImageWidth_px, ImageHeight_px), CV_8UC1,  (void*)ir_frame.get_data(),     Mat::AUTO_STEP);
        Mat depth   (Size(ImageWidth_px, ImageHeight_px), CV_16UC1, (void*)depth_frame.get_data(),  Mat::AUTO_STEP);

        // etc the rest of your code on how you want to edit/process the images
        }
Wouter61636
  • 91
  • 1
  • 5
  • This is great, but I've just tried this and it doesn't appear to be actually applying the JSON settings to the device. I can save/serialize the device's current settings nicely, but loading with various methods stills seems to fail silently. ```advanced_mode_dev.load_json(str);``` appears to succeed but actually it doesn't apply to the device. – KyranF Sep 11 '19 at 10:38