IntelRealSense / librealsense

Intel® RealSense™ SDK
https://www.intelrealsense.com/
Apache License 2.0
7.53k stars 4.81k forks source link

Reading exposure value with auto-exposure enabled #4900

Closed kylevedder closed 4 years ago

kylevedder commented 4 years ago
Required Info
Camera Model D435
Firmware Version 05.11.11.100
Operating System & Version Ubuntu 18.04 LTS
Kernel Version (Linux Only) 4.15.0-62-generic + Kernel Patch
Platform PC
SDK Version 2.28.0
Language C++
Segment Robot

Issue Description

I am unable to figure out how to read the exposure value for the IR cameras with auto-exposure enabled. I tried to follow the suggestion from https://github.com/IntelRealSense/librealsense/issues/1624#issuecomment-389085387 but the approach

dev.query_sensors()[0].get_option(rs2_option::RS2_OPTION_EXPOSURE)

always produced 8500 despite clear changes to auto-exposure.

I then tried reading the metadata as suggested, but the RS2_FRAME_METADATA_ACTUAL_EXPOSURE is not provided. See the following code for printing what metadata is available:

#include <iostream>
#include <librealsense2/rs.hpp>

rs2::config ConfigPipeline() {
  int width = 640;
  int height = 480;
  rs2::config cfg;
  cfg.enable_stream(RS2_STREAM_INFRARED, 1, width, height);
  cfg.enable_stream(RS2_STREAM_INFRARED, 2, width, height);
  return cfg;
}

void ConfigSensors(rs2::device &dev) {
  for (auto &s : dev.query_sensors()) {
    if (s.supports(rs2_option::RS2_OPTION_EMITTER_ENABLED)) {
      s.set_option(rs2_option::RS2_OPTION_EMITTER_ENABLED, 0);
    }
    if (s.supports(rs2_option::RS2_OPTION_ENABLE_AUTO_EXPOSURE)) {
      s.set_option(rs2_option::RS2_OPTION_ENABLE_AUTO_EXPOSURE, 1);
    }
  }
}

void PrintMetadataStatus(rs2::video_frame &vf) {
  if (vf) {
    auto metadata =
        rs2_frame_metadata_value::RS2_FRAME_METADATA_ACTUAL_EXPOSURE;
    for (auto i = 0; i < rs2_frame_metadata_value::RS2_FRAME_METADATA_COUNT;
         ++i) {
      auto metadata = static_cast<rs2_frame_metadata_value>(i);
      bool supported = vf.supports_frame_metadata(metadata);
      std::cout << "Supports metadata " << i << " ("
                << rs2_frame_metadata_to_string(metadata) << "):\t"
                << (supported ? "true" : "false") << std::endl;
      if (supported) {
        std::cout << vf.get_frame_metadata(metadata) << std::endl;
      }
    }
  }
}

int main(int argc, char *argv[]) {
  rs2::pipeline pipe;
  pipe.start(ConfigPipeline());
  rs2::device dev = pipe.get_active_profile().get_device();
  ConfigSensors(dev);

  while (true) {
    auto frameset = pipe.wait_for_frames();
    auto infra_frame_0 = frameset.get_infrared_frame(0);
    std::cout << "Infra frame 0: " << (infra_frame_0 ? "true" : "false")
              << std::endl;
    PrintMetadataStatus(infra_frame_0);
    auto infra_frame_1 = frameset.get_infrared_frame(1);
    std::cout << "Infra frame 1: " << (infra_frame_1 ? "true" : "false")
              << std::endl;
    PrintMetadataStatus(infra_frame_1);
  }
}

compiled with

clang++ -std=c++11 -lrealsense2 metadata.cc -o metadata

which prints in a loop

...
Infra frame 0: true
Supports metadata 0 (Frame Counter):    true
2800
Supports metadata 1 (Frame Timestamp):  true
59542497
Supports metadata 2 (Sensor Timestamp): true
59537497
Supports metadata 3 (Actual Exposure):  true
10000
Supports metadata 4 (Gain Level):   true
20
Supports metadata 5 (Auto Exposure):    true
1
Supports metadata 6 (White Balance):    false
Supports metadata 7 (Time Of Arrival):  true
1569018618650
Supports metadata 8 (Temperature):  false
Supports metadata 9 (Backend Timestamp):    true
1569018618643
Supports metadata 10 (Actual Fps):  true
90
Supports metadata 11 (Frame Laser Power):   true
0
Supports metadata 12 (Frame Laser Power Mode):  true
0
Supports metadata 13 (Exposure Priority):   true
1
Supports metadata 14 (Exposure Roi Left):   true
80
Supports metadata 15 (Exposure Roi Right):  true
560
Supports metadata 16 (Exposure Roi Top):    true
60
Supports metadata 17 (Exposure Roi Bottom): true
420
Supports metadata 18 (Brightness):  false
Supports metadata 19 (Contrast):    false
Supports metadata 20 (Saturation):  false
Supports metadata 21 (Sharpness):   false
Supports metadata 22 (Auto White Balance Temperature):  false
Supports metadata 23 (Backlight Compensation):  false
Supports metadata 24 (Hue): false
Supports metadata 25 (Gamma):   false
Supports metadata 26 (Manual White Balance):    false
Supports metadata 27 (Power Line Frequency):    false
Supports metadata 28 (Low Light Compensation):  false
Infra frame 1: true
Supports metadata 0 (Frame Counter):    true
2800
Supports metadata 1 (Frame Timestamp):  true
59542497
Supports metadata 2 (Sensor Timestamp): true
59537497
Supports metadata 3 (Actual Exposure):  true
10000
Supports metadata 4 (Gain Level):   true
20
Supports metadata 5 (Auto Exposure):    true
1
Supports metadata 6 (White Balance):    false
Supports metadata 7 (Time Of Arrival):  true
1569018618650
Supports metadata 8 (Temperature):  false
Supports metadata 9 (Backend Timestamp):    true
1569018618643
Supports metadata 10 (Actual Fps):  true
90
Supports metadata 11 (Frame Laser Power):   true
0
Supports metadata 12 (Frame Laser Power Mode):  true
0
Supports metadata 13 (Exposure Priority):   true
1
Supports metadata 14 (Exposure Roi Left):   true
80
Supports metadata 15 (Exposure Roi Right):  true
560
Supports metadata 16 (Exposure Roi Top):    true
60
Supports metadata 17 (Exposure Roi Bottom): true
420
Supports metadata 18 (Brightness):  false
Supports metadata 19 (Contrast):    false
Supports metadata 20 (Saturation):  false
Supports metadata 21 (Sharpness):   false
Supports metadata 22 (Auto White Balance Temperature):  false
Supports metadata 23 (Backlight Compensation):  false
Supports metadata 24 (Hue): false
Supports metadata 25 (Gamma):   false
Supports metadata 26 (Manual White Balance):    false
Supports metadata 27 (Power Line Frequency):    false
Supports metadata 28 (Low Light Compensation):  false

...

As you can see, Supports metadata 3 (Actual Exposure) is true, but the exposure is constantly at 10000 even when the gain changes. How do I get the exposure value selected by autoexposure for the IR images taken? Thanks!

kylevedder commented 4 years ago

It turns out the lights were not emitting enough IR to trigger auto-exposure. Pointing it at the sun triggered the exposure change.