Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
algo.h
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 
4 #pragma once
5 #include "sensor.h"
6 #include "concurrency.h"
7 #include "types.h"
8 
9 #include <stdint.h>
10 #include <vector>
11 #include <mutex>
12 #include <deque>
13 #include <cmath>
14 #include <memory>
15 
16 namespace librealsense
17 {
18  static const float ae_step_default_value = 0.5f;
19 
20  enum class auto_exposure_modes {
24  };
25 
27  {
28  public:
30  is_auto_exposure(true),
31  mode(auto_exposure_modes::auto_exposure_hybrid),
32  rate(60),
33  step(ae_step_default_value)
34  {}
35 
36  bool get_enable_auto_exposure() const;
37  auto_exposure_modes get_auto_exposure_mode() const;
38  unsigned get_auto_exposure_antiflicker_rate() const;
39  float get_auto_exposure_step() const;
40 
41  void set_enable_auto_exposure(bool value);
42  void set_auto_exposure_mode(auto_exposure_modes value);
43  void set_auto_exposure_antiflicker_rate(unsigned value);
44  void set_auto_exposure_step(float value);
45 
46  static const unsigned sample_rate = 1;
47  static const unsigned skip_frames = 2;
48 
49  private:
50  bool is_auto_exposure;
52  unsigned rate;
53  float step;
54  };
55 
56 
58  public:
59  void modify_exposure(float& exposure_value, bool& exp_modified, float& gain_value, bool& gain_modified); // exposure_value in milliseconds
60  bool analyze_image(const frame_interface* image);
62  void update_options(const auto_exposure_state& options);
63  void update_roi(const region_of_interest& ae_roi);
64 
65  private:
66  struct histogram_metric { int under_exposure_count; int over_exposure_count; int shadow_limit; int highlight_limit; int lower_q; int upper_q; float main_mean; float main_std; };
67  enum class rounding_mode_type { round, ceil, floor };
68 
69  inline void im_hist(const uint8_t* data, const region_of_interest& image_roi, const int rowStep, int h[]);
70  void increase_exposure_target(float mult, float& target_exposure);
71  void decrease_exposure_target(float mult, float& target_exposure);
72  void increase_exposure_gain(const float& target_exposure, const float& target_exposure0, float& exposure, float& gain);
73  void decrease_exposure_gain(const float& target_exposure, const float& target_exposure0, float& exposure, float& gain);
74  void static_increase_exposure_gain(const float& target_exposure, const float& target_exposure0, float& exposure, float& gain);
75  void static_decrease_exposure_gain(const float& target_exposure, const float& target_exposure0, float& exposure, float& gain);
76  void anti_flicker_increase_exposure_gain(const float& target_exposure, const float& target_exposure0, float& exposure, float& gain);
77  void anti_flicker_decrease_exposure_gain(const float& target_exposure, const float& target_exposure0, float& exposure, float& gain);
78  void hybrid_increase_exposure_gain(const float& target_exposure, const float& target_exposure0, float& exposure, float& gain);
79  void hybrid_decrease_exposure_gain(const float& target_exposure, const float& target_exposure0, float& exposure, float& gain);
80 
81  #if defined(_WINDOWS) || defined(WIN32) || defined(WIN64)
82  inline float round(float x) { return std::round(x); }
83  #else
84  inline float round(float x) { return x < 0.0 ? std::ceil(x - 0.5f) : std::floor(x + 0.5f); }
85  #endif
86 
87  float exposure_to_value(float exp_ms, rounding_mode_type rounding_mode);
88  float gain_to_value(float gain, rounding_mode_type rounding_mode);
89  template <typename T> inline T sqr(const T& x) { return (x*x); }
90  void histogram_score(std::vector<int>& h, const int total_weight, histogram_metric& score);
91 
92 
93  float minimal_exposure = 0.2f, maximal_exposure = 20.f, base_gain = 2.0f, gain_limit = 15.0f;
94  float exposure = 10.0f, gain = 2.0f, target_exposure = 0.0f;
95  uint8_t under_exposure_limit = 5, over_exposure_limit = 250; int under_exposure_noise_limit = 50, over_exposure_noise_limit = 50;
96  int direction = 0, prev_direction = 0; float hysteresis = 0.075f;// 05;
97  float eps = 0.01f, minimal_exposure_step = 0.01f;
98  std::atomic<float> exposure_step;
99  auto_exposure_state state; float flicker_cycle; bool anti_flicker_mode = true;
100  region_of_interest roi{};
101  bool is_roi_initialized = false;
102  std::recursive_mutex state_mutex;
103  };
104 
108  };
109 
111  public:
112  auto_exposure_mechanism(option& gain_option, option& exposure_option, const auto_exposure_state& auto_exposure_state);
114  void add_frame(frame_holder frame, callback_invocation_holder callback);
115  void update_auto_exposure_state(const auto_exposure_state& auto_exposure_state);
116  void update_auto_exposure_roi(const region_of_interest& roi);
117 
120  : exposure(0), frame_counter(0)
121  {}
122 
123  exposure_and_frame_counter(double exposure, unsigned long long frame_counter)
124  : exposure(exposure), frame_counter(frame_counter)
125  {}
126 
127  double exposure;
128  unsigned long long frame_counter;
129  };
130 
131  private:
132  bool try_pop_front_data(frame_and_callback* data);
133 
134  static const int queue_size = 2;
135  option& _gain_option;
136  option& _exposure_option;
137  auto_exposure_algorithm _auto_exposure_algo;
138  std::shared_ptr<std::thread> _exposure_thread;
139  std::condition_variable _cv;
140  std::atomic<bool> _keep_alive;
142  std::mutex _queue_mtx;
143  std::atomic<unsigned> _frames_counter;
144  std::atomic<unsigned> _skip_frames;
145  };
146 
147 }
Definition: backend.h:378
auto_exposure_modes
Definition: algo.h:20
Definition: streaming.h:63
Definition: options.h:20
exposure_and_frame_counter(double exposure, unsigned long long frame_counter)
Definition: algo.h:123
Definition: archive.h:63
Definition: algo.h:16
constexpr uint32_t frame_counter
Definition: ros_file_format.h:620
frame_holder f_holder
Definition: algo.h:106
auto_exposure_state()
Definition: algo.h:29
unsigned long long frame_counter
Definition: algo.h:128
Definition: concurrency.h:15
Definition: types.h:637
Definition: algo.h:105
callback_invocation_holder callback
Definition: algo.h:107