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


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Fri Aug 2 2024 08:30:01