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  enum class auto_exposure_modes {
22  };
23 
25  {
26  public:
28  is_auto_exposure(true),
30  rate(60)
31  {}
32 
33  bool get_enable_auto_exposure() const;
35  unsigned get_auto_exposure_antiflicker_rate() const;
36 
40 
41  static const unsigned sample_rate = 1;
42  static const unsigned skip_frames = 2;
43 
44  private:
45  bool is_auto_exposure;
47  unsigned rate;
48  };
49 
50 
52  public:
53  void modify_exposure(float& exposure_value, bool& exp_modified, float& gain_value, bool& gain_modified); // exposure_value in milliseconds
54  bool analyze_image(const frame_interface* image);
56  void update_options(const auto_exposure_state& options);
57  void update_roi(const region_of_interest& ae_roi);
58 
59  private:
60  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; };
61  enum class rounding_mode_type { round, ceil, floor };
62 
63  inline void im_hist(const uint8_t* data, const region_of_interest& image_roi, const int rowStep, int h[]);
64  void increase_exposure_target(float mult, float& target_exposure);
65  void decrease_exposure_target(float mult, float& target_exposure);
66  void increase_exposure_gain(const float& target_exposure, const float& target_exposure0, float& exposure, float& gain);
67  void decrease_exposure_gain(const float& target_exposure, const float& target_exposure0, float& exposure, float& gain);
68  void static_increase_exposure_gain(const float& target_exposure, const float& target_exposure0, float& exposure, float& gain);
69  void static_decrease_exposure_gain(const float& target_exposure, const float& target_exposure0, float& exposure, float& gain);
70  void anti_flicker_increase_exposure_gain(const float& target_exposure, const float& target_exposure0, float& exposure, float& gain);
71  void anti_flicker_decrease_exposure_gain(const float& target_exposure, const float& target_exposure0, float& exposure, float& gain);
72  void hybrid_increase_exposure_gain(const float& target_exposure, const float& target_exposure0, float& exposure, float& gain);
73  void hybrid_decrease_exposure_gain(const float& target_exposure, const float& target_exposure0, float& exposure, float& gain);
74 
75  #if defined(_WINDOWS) || defined(WIN32) || defined(WIN64)
76  inline float round(float x) { return std::round(x); }
77  #else
78  inline float round(float x) { return x < 0.0 ? std::ceil(x - 0.5f) : std::floor(x + 0.5f); }
79  #endif
80 
81  float exposure_to_value(float exp_ms, rounding_mode_type rounding_mode);
82  float gain_to_value(float gain, rounding_mode_type rounding_mode);
83  template <typename T> inline T sqr(const T& x) { return (x*x); }
84  void histogram_score(std::vector<int>& h, const int total_weight, histogram_metric& score);
85 
86 
87  float minimal_exposure = 0.2f, maximal_exposure = 20.f, base_gain = 2.0f, gain_limit = 15.0f;
88  float exposure = 10.0f, gain = 2.0f, target_exposure = 0.0f;
89  uint8_t under_exposure_limit = 5, over_exposure_limit = 250; int under_exposure_noise_limit = 50, over_exposure_noise_limit = 50;
90  int direction = 0, prev_direction = 0; float hysteresis = 0.075f;// 05;
91  float eps = 0.01f, exposure_step = 0.1f, minimal_exposure_step = 0.01f;
92  auto_exposure_state state; float flicker_cycle; bool anti_flicker_mode = true;
93  region_of_interest roi{};
94  bool is_roi_initialized = false;
95  std::recursive_mutex state_mutex;
96  };
97 
101  };
102 
104  public:
105  auto_exposure_mechanism(option& gain_option, option& exposure_option, const auto_exposure_state& auto_exposure_state);
110 
113  : exposure(0), frame_counter(0)
114  {}
115 
118  {}
119 
120  double exposure;
121  unsigned long long frame_counter;
122  };
123 
124  private:
125  bool try_pop_front_data(frame_and_callback* data);
126 
127  static const int queue_size = 2;
128  option& _gain_option;
129  option& _exposure_option;
130  auto_exposure_algorithm _auto_exposure_algo;
131  std::shared_ptr<std::thread> _exposure_thread;
132  std::condition_variable _cv;
133  std::atomic<bool> _keep_alive;
135  std::mutex _queue_mtx;
136  std::atomic<unsigned> _frames_counter;
137  std::atomic<unsigned> _skip_frames;
138  };
139 
140 }
void add_frame(frame_holder frame, callback_invocation_holder callback)
static const unsigned skip_frames
Definition: algo.h:42
void update_roi(const region_of_interest &ae_roi)
Definition: backend.h:351
void update_auto_exposure_state(const auto_exposure_state &auto_exposure_state)
auto_exposure_modes
Definition: algo.h:18
Definition: streaming.h:63
unsigned get_auto_exposure_antiflicker_rate() const
Definition: options.h:20
void modify_exposure(float &exposure_value, bool &exp_modified, float &gain_value, bool &gain_modified)
auto_exposure_modes get_auto_exposure_mode() const
exposure_and_frame_counter(double exposure, unsigned long long frame_counter)
Definition: algo.h:116
void set_auto_exposure_antiflicker_rate(unsigned value)
auto_exposure_algorithm(const auto_exposure_state &auto_exposure_state)
Definition: archive.h:63
Definition: algo.h:16
void update_auto_exposure_roi(const region_of_interest &roi)
frame_holder f_holder
Definition: algo.h:99
bool analyze_image(const frame_interface *image)
auto_exposure_state()
Definition: algo.h:27
void set_auto_exposure_mode(auto_exposure_modes value)
unsigned long long frame_counter
Definition: algo.h:121
void update_options(const auto_exposure_state &options)
Definition: concurrency.h:15
Definition: types.h:587
void set_enable_auto_exposure(bool value)
callback_invocation_holder callback
Definition: algo.h:100
static const unsigned sample_rate
Definition: algo.h:41
auto_exposure_mechanism(option &gain_option, option &exposure_option, const auto_exposure_state &auto_exposure_state)