colorizer.h
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2017 Intel Corporation. All Rights Reserved.
3 
4 #pragma once
5 
6 #include <map>
7 #include <vector>
8 
9 namespace rs2
10 {
11  class stream_profile;
12 }
13 
14 namespace librealsense {
15 
17  {
18  public:
19  color_map(std::map<float, float3> map, int steps = 4000) : _map(map)
20  {
21  initialize(steps);
22  }
23 
24  color_map(const std::vector<float3>& values, int steps = 4000)
25  {
26  for (size_t i = 0; i < values.size(); i++)
27  {
28  _map[(float)i / (values.size() - 1)] = values[i];
29  }
30  initialize(steps);
31  }
32 
33  color_map() {}
34 
35  inline float3 get(float value) const
36  {
37  if (_max == _min) return *_data;
38  auto t = (value - _min) / (_max - _min);
39  t = clamp_val(t, 0.f, 1.f);
40  return _data[(int)(t * (_size - 1))];
41  }
42 
43  float min_key() const { return _min; }
44  float max_key() const { return _max; }
45 
46  const std::vector<float3>& get_cache() const { return _cache; }
47 
48  private:
49  inline float3 lerp(const float3& a, const float3& b, float t) const
50  {
51  return b * t + a * (1 - t);
52  }
53 
54  float3 calc(float value) const
55  {
56  if (_map.size() == 0) return{ value, value, value };
57  // if we have exactly this value in the map, just return it
58  if (_map.find(value) != _map.end()) return _map.at(value);
59  // if we are beyond the limits, return the first/last element
60  if (value < _map.begin()->first) return _map.begin()->second;
61  if (value > _map.rbegin()->first) return _map.rbegin()->second;
62 
63  auto lower = _map.lower_bound(value) == _map.begin() ? _map.begin() : --(_map.lower_bound(value));
64  auto upper = _map.upper_bound(value);
65 
66  auto t = (value - lower->first) / (upper->first - lower->first);
67  auto c1 = lower->second;
68  auto c2 = upper->second;
69  return lerp(c1, c2, t);
70  }
71 
72  void initialize(int steps)
73  {
74  if (_map.size() == 0) return;
75 
76  _min = _map.begin()->first;
77  _max = _map.rbegin()->first;
78 
79  _cache.resize(steps + 1);
80  for (int i = 0; i <= steps; i++)
81  {
82  auto t = (float)i / steps;
83  auto x = _min + t*(_max - _min);
84  _cache[i] = calc(x);
85  }
86 
87  // Save size and data to avoid STL checks penalties in DEBUG
88  _size = _cache.size();
89  _data = _cache.data();
90  }
91 
92  std::map<float, float3> _map;
93  std::vector<float3> _cache;
94  float _min, _max;
95  size_t _size; float3* _data;
96  };
97 
99  {
100  public:
101  colorizer();
102 
103  template<typename T>
104  static void update_histogram(int* hist, const T* depth_data, int w, int h)
105  {
106  memset(hist, 0, MAX_DEPTH * sizeof(int));
107  for (auto i = 0; i < w*h; ++i)
108  {
109  T depth_val = depth_data[i];
110  int index = static_cast< int >( depth_val );
111  hist[index] += 1;
112  }
113 
114  for (auto i = 2; i < MAX_DEPTH; ++i) hist[i] += hist[i - 1]; // Build a cumulative histogram for the indices in [1,0xFFFF]
115  }
116 
117  static const int MAX_DEPTH = 0x10000;
118  static const int MAX_DISPARITY = 0x2710;
119 
120  protected:
121  colorizer(const char* name);
122 
123  bool should_process(const rs2::frame& frame) override;
124  rs2::frame process_frame(const rs2::frame_source& source, const rs2::frame& f) override;
125 
126  template<typename T, typename F>
127  void make_rgb_data(const T* depth_data, uint8_t* rgb_data, int width, int height, F coloring_func)
128  {
129  auto cm = _maps[_map_index];
130  for (auto i = 0; i < width*height; ++i)
131  {
132  auto d = depth_data[i];
133  colorize_pixel(rgb_data, i, cm, d, coloring_func);
134  }
135  }
136 
137  template<typename T, typename F>
138  void colorize_pixel(uint8_t* rgb_data, int idx, color_map* cm, T data, F coloring_func)
139  {
140  if (data)
141  {
142  auto f = coloring_func(data); // 0-255 based on histogram locationcolorize_pixel
143  auto c = cm->get(f);
144  rgb_data[idx * 3 + 0] = (uint8_t)c.x;
145  rgb_data[idx * 3 + 1] = (uint8_t)c.y;
146  rgb_data[idx * 3 + 2] = (uint8_t)c.z;
147  }
148  else
149  {
150  rgb_data[idx * 3 + 0] = 0;
151  rgb_data[idx * 3 + 1] = 0;
152  rgb_data[idx * 3 + 2] = 0;
153  }
154  }
155 
156  float _min, _max;
157  bool _equalize;
158 
159  std::vector<color_map*> _maps;
160  int _map_index = 0;
161 
162  std::vector<int> _histogram;
164 
165  int _preset = 0;
168 
169  float _depth_units = 0.f;
170  float _d2d_convert_factor = 0.f;
171  };
172 }
float lerp(float a, float b, float t)
Definition: rendering.h:122
GLboolean GLboolean GLboolean b
rs2::stream_profile _source_stream_profile
Definition: colorizer.h:167
GLuint const GLchar * name
float3 get(float value) const
Definition: colorizer.h:35
color_map(const std::vector< float3 > &values, int steps=4000)
Definition: colorizer.h:24
void colorize_pixel(uint8_t *rgb_data, int idx, color_map *cm, T data, F coloring_func)
Definition: colorizer.h:138
GLfloat value
Definition: cah-model.h:10
GLdouble GLdouble GLdouble w
d
Definition: rmse.py:171
#define LRS_EXTENSION_API
Definition: src/types.h:20
std::vector< float3 > _cache
Definition: colorizer.h:93
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1960
unsigned char uint8_t
Definition: stdint.h:78
GLuint index
GLdouble t
GLboolean GLboolean GLboolean GLboolean a
std::map< float, float3 > _map
Definition: colorizer.h:92
void make_rgb_data(const T *depth_data, uint8_t *rgb_data, int width, int height, F coloring_func)
Definition: colorizer.h:127
const std::vector< float3 > & get_cache() const
Definition: colorizer.h:46
GLdouble f
float max_key() const
Definition: colorizer.h:44
const GLubyte * c
Definition: glext.h:12690
std::vector< int > _histogram
Definition: colorizer.h:162
GLdouble x
T clamp_val(T val, const T &min, const T &max)
Definition: src/types.h:250
GLint GLsizei GLsizei height
std::vector< color_map * > _maps
Definition: colorizer.h:159
rs2::stream_profile _target_stream_profile
Definition: colorizer.h:166
color_map(std::map< float, float3 > map, int steps=4000)
Definition: colorizer.h:19
float3 calc(float value) const
Definition: colorizer.h:54
GLsizei const GLfloat * values
void initialize(int steps)
Definition: colorizer.h:72
float min_key() const
Definition: colorizer.h:43
GLsizei GLsizei GLchar * source
float3 lerp(const float3 &a, const float3 &b, float t) const
Definition: colorizer.h:49
int i
static void update_histogram(int *hist, const T *depth_data, int w, int h)
Definition: colorizer.h:104
GeneratorWrapper< T > map(Func &&function, GeneratorWrapper< U > &&generator)
Definition: catch.hpp:4271
Definition: parser.hpp:150
GLint GLsizei width


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:11