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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019