MRPT  1.9.9
color_maps.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "img-precomp.h" // Precompiled headers
11 
12 #include <mrpt/img/color_maps.h>
13 #include <Eigen/Dense>
14 #include <mrpt/math/interp_fit.hpp>
15 
16 using namespace mrpt;
17 using namespace mrpt::img;
18 using namespace std;
19 
20 /*-------------------------------------------------------------
21  hsv2rgb
22 -------------------------------------------------------------*/
23 void mrpt::img::hsv2rgb(float h, float s, float v, float& r, float& g, float& b)
24 {
25  // See: http://en.wikipedia.org/wiki/HSV_color_space
26  h = max(0.0f, min(1.0f, h));
27  s = max(0.0f, min(1.0f, s));
28  v = max(0.0f, min(1.0f, v));
29 
30  int Hi = ((int)floor(h * 6)) % 6;
31  float f = (h * 6) - Hi;
32  float p = v * (1 - s);
33  float q = v * (1 - f * s);
34  float t = v * (1 - (1 - f) * s);
35 
36  switch (Hi)
37  {
38  case 0:
39  r = v;
40  g = t;
41  b = p;
42  break;
43  case 1:
44  r = q;
45  g = v;
46  b = p;
47  break;
48  case 2:
49  r = p;
50  g = v;
51  b = t;
52  break;
53  case 3:
54  r = p;
55  g = q;
56  b = v;
57  break;
58  case 4:
59  r = t;
60  g = p;
61  b = v;
62  break;
63  case 5:
64  r = v;
65  g = p;
66  b = q;
67  break;
68  }
69 }
70 
71 /*-------------------------------------------------------------
72  rgb2hsv
73 -------------------------------------------------------------*/
74 void mrpt::img::rgb2hsv(float r, float g, float b, float& h, float& s, float& v)
75 {
76  // See: http://en.wikipedia.org/wiki/HSV_color_space
77  r = max(0.0f, min(1.0f, r));
78  g = max(0.0f, min(1.0f, g));
79  b = max(0.0f, min(1.0f, b));
80 
81  float Max = max3(r, g, b);
82  float Min = min3(r, g, b);
83 
84  if (Max == Min)
85  {
86  h = 0;
87  }
88  else
89  {
90  if (Max == r)
91  {
92  if (g >= b)
93  h = (g - b) / (6 * (Max - Min));
94  else
95  h = 1 - (g - b) / (6 * (Max - Min));
96  }
97  else if (Max == g)
98  h = 1 / 3.0f + (b - r) / (6 * (Max - Min));
99  else
100  h = 2 / 3.0f + (r - g) / (6 * (Max - Min));
101  }
102 
103  if (Max == 0)
104  s = 0;
105  else
106  s = 1 - Min / Max;
107 
108  v = Max;
109 }
110 
111 /*-------------------------------------------------------------
112  colormap
113 -------------------------------------------------------------*/
115  const TColormap& color_map, const float col_indx_in, float& r, float& g,
116  float& b)
117 {
118  MRPT_START
119 
120  const float color_index = std::min(1.0f, std::max(.0f, col_indx_in));
121 
122  switch (color_map)
123  {
124  case cmJET:
125  jet2rgb(color_index, r, g, b);
126  break;
127  case cmGRAYSCALE:
128  r = g = b = color_index;
129  break;
130  case cmHOT:
131  hot2rgb(color_index, r, g, b);
132  break;
133  default:
134  THROW_EXCEPTION("Invalid color_map");
135  };
136  MRPT_END
137 }
138 
139 /*-------------------------------------------------------------
140  jet2rgb
141 -------------------------------------------------------------*/
142 void mrpt::img::jet2rgb(const float color_index, float& r, float& g, float& b)
143 {
144  static bool jet_table_done = false;
145  static Eigen::VectorXf jet_r, jet_g, jet_b;
146 
147  // Initialize tables
148  if (!jet_table_done)
149  {
150  jet_table_done = true;
151 
152  // Refer to source code of "jet" in MATLAB:
153  double JET_R[] = {
154  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
155  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
156  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
157  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
158  0.062500, 0.125000, 0.187500, 0.250000, 0.312500, 0.375000,
159  0.437500, 0.500000, 0.562500, 0.625000, 0.687500, 0.750000,
160  0.812500, 0.875000, 0.937500, 1.000000, 1.000000, 1.000000,
161  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
162  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
163  1.000000, 1.000000, 0.937500, 0.875000, 0.812500, 0.750000,
164  0.687500, 0.625000, 0.562500, 0.500000};
165  double JET_G[] = {
166  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
167  0.000000, 0.000000, 0.062500, 0.125000, 0.187500, 0.250000,
168  0.312500, 0.375000, 0.437500, 0.500000, 0.562500, 0.625000,
169  0.687500, 0.750000, 0.812500, 0.875000, 0.937500, 1.000000,
170  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
171  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
172  1.000000, 1.000000, 1.000000, 1.000000, 0.937500, 0.875000,
173  0.812500, 0.750000, 0.687500, 0.625000, 0.562500, 0.500000,
174  0.437500, 0.375000, 0.312500, 0.250000, 0.187500, 0.125000,
175  0.062500, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
176  0.000000, 0.000000, 0.000000, 0.000000};
177  double JET_B[] = {
178  0.562500, 0.625000, 0.687500, 0.750000, 0.812500, 0.875000,
179  0.937500, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
180  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
181  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
182  0.937500, 0.875000, 0.812500, 0.750000, 0.687500, 0.625000,
183  0.562500, 0.500000, 0.437500, 0.375000, 0.312500, 0.250000,
184  0.187500, 0.125000, 0.062500, 0.000000, 0.000000, 0.000000,
185  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
186  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
187  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
188  0.000000, 0.000000, 0.000000, 0.000000};
189  const size_t N = sizeof(JET_B) / sizeof(JET_B[0]);
190 
191  jet_r.resize(N);
192  jet_g.resize(N);
193  jet_b.resize(N);
194  for (size_t i = 0; i < N; i++)
195  {
196  jet_r[i] = JET_R[i];
197  jet_g[i] = JET_G[i];
198  jet_b[i] = JET_B[i];
199  }
200  }
201 
202  // Return interpolate value:
203  r = math::interpolate(color_index, jet_r, 0.0f, 1.0f);
204  g = math::interpolate(color_index, jet_g, 0.0f, 1.0f);
205  b = math::interpolate(color_index, jet_b, 0.0f, 1.0f);
206 }
207 
208 void mrpt::img::hot2rgb(const float color_index, float& r, float& g, float& b)
209 {
210  static bool table_done = false;
211  static Eigen::VectorXf hot_r, hot_g, hot_b;
212 
213  // Initialize tables
214  if (!table_done)
215  {
216  table_done = true;
217 
218  // Refer to source code of "hot" in MATLAB:
219  double HOT_R[] = {
220  0.041667, 0.083333, 0.125000, 0.166667, 0.208333, 0.250000,
221  0.291667, 0.333333, 0.375000, 0.416667, 0.458333, 0.500000,
222  0.541667, 0.583333, 0.625000, 0.666667, 0.708333, 0.750000,
223  0.791667, 0.833333, 0.875000, 0.916667, 0.958333, 1.000000,
224  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
225  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
226  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
227  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
228  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
229  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
230  1.000000, 1.000000, 1.000000, 1.000000};
231  double HOT_G[] = {
232  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
233  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
234  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
235  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
236  0.041667, 0.083333, 0.125000, 0.166667, 0.208333, 0.250000,
237  0.291667, 0.333333, 0.375000, 0.416667, 0.458333, 0.500000,
238  0.541667, 0.583333, 0.625000, 0.666667, 0.708333, 0.750000,
239  0.791667, 0.833333, 0.875000, 0.916667, 0.958333, 1.000000,
240  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
241  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
242  1.000000, 1.000000, 1.000000, 1.000000};
243  double HOT_B[] = {
244  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
245  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
246  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
247  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
248  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
249  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
250  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
251  0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
252  0.062500, 0.125000, 0.187500, 0.250000, 0.312500, 0.375000,
253  0.437500, 0.500000, 0.562500, 0.625000, 0.687500, 0.750000,
254  0.812500, 0.875000, 0.937500, 1.000000};
255  const size_t N = sizeof(HOT_B) / sizeof(HOT_B[0]);
256 
257  hot_r.resize(N);
258  hot_g.resize(N);
259  hot_b.resize(N);
260  for (size_t i = 0; i < N; i++)
261  {
262  hot_r[i] = HOT_R[i];
263  hot_g[i] = HOT_G[i];
264  hot_b[i] = HOT_B[i];
265  }
266  }
267 
268  // Return interpolate value:
269  r = math::interpolate(color_index, hot_r, 0.0f, 1.0f);
270  g = math::interpolate(color_index, hot_g, 0.0f, 1.0f);
271  b = math::interpolate(color_index, hot_b, 0.0f, 1.0f);
272 }
void colormap(const TColormap &color_map, const float color_index, float &r, float &g, float &b)
Transform a float number in the range [0,1] into RGB components.
Definition: color_maps.cpp:114
TColormap
Different colormaps for use in mrpt::img::colormap()
Definition: color_maps.h:29
#define MRPT_START
Definition: exceptions.h:241
GLdouble GLdouble t
Definition: glext.h:3695
#define min(a, b)
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
const T max3(const T &A, const T &B, const T &C)
GLdouble GLdouble GLdouble GLdouble q
Definition: glext.h:3727
T interpolate(const T &x, const VECTOR &ys, const T &x0, const T &x1)
Interpolate a data sequence "ys" ranging from "x0" to "x1" (equally spaced), to obtain the approximat...
Definition: interp_fit.hpp:17
STL namespace.
GLdouble s
Definition: glext.h:3682
void hot2rgb(const float color_index, float &r, float &g, float &b)
Computes the RGB color components (range [0,1]) for the corresponding color index in the range [0...
Definition: color_maps.cpp:208
void jet2rgb(const float color_index, float &r, float &g, float &b)
Computes the RGB color components (range [0,1]) for the corresponding color index in the range [0...
Definition: color_maps.cpp:142
[New in MRPT 1.5.0]
Definition: color_maps.h:35
GLubyte g
Definition: glext.h:6372
Undefined colormap [New in MRPT 2.0].
Definition: color_maps.h:32
GLubyte GLubyte b
Definition: glext.h:6372
void rgb2hsv(float r, float g, float b, float &h, float &s, float &v)
Transform RGB color components to HSV, all of them in the range [0,1].
Definition: color_maps.cpp:74
const T min3(const T &A, const T &B, const T &C)
const GLdouble * v
Definition: glext.h:3684
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
Definition: glext.h:3711
void hsv2rgb(float h, float s, float v, float &r, float &g, float &b)
Transform HSV color components to RGB, all of them in the range [0,1].
Definition: color_maps.cpp:23
#define MRPT_END
Definition: exceptions.h:245
GLfloat GLfloat p
Definition: glext.h:6398



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019