Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
tutorial-image-colormap.cpp
1 
2 #include <map>
3 #include <visp3/core/vpColormap.h>
4 #include <visp3/core/vpConfig.h>
5 #include <visp3/core/vpFont.h>
6 #include <visp3/core/vpImageTools.h>
7 #include <visp3/gui/vpDisplayGDI.h>
8 #include <visp3/gui/vpDisplayOpenCV.h>
9 #include <visp3/gui/vpDisplayX.h>
10 #include <visp3/io/vpImageIo.h>
11 
12 int main()
13 {
14 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
15 #ifdef ENABLE_VISP_NAMESPACE
16  using namespace VISP_NAMESPACE_NAME;
17 #endif
18  try {
19  std::map<vpColormap::vpColormapType, std::string> colormaps_str = {
20  {vpColormap::COLORMAP_AUTUMN, "Colormap Autumn"},
21  {vpColormap::COLORMAP_CIVIDIS, "Colormap Cividis"},
22  {vpColormap::COLORMAP_COOL, "Colormap Cool"},
23  {vpColormap::COLORMAP_GIST_EARTH, "Colormap Gist Earth"},
24  {vpColormap::COLORMAP_GNUPLOT, "Colormap Gnuplot"},
25  {vpColormap::COLORMAP_GNUPLOT2, "Colormap Gnuplot2"},
26  {vpColormap::COLORMAP_HOT, "Colormap Hot"},
27  {vpColormap::COLORMAP_HSV, "Colormap HSV"},
28  {vpColormap::COLORMAP_INFERNO, "Colormap Inferno"},
29  {vpColormap::COLORMAP_JET, "Colormap Jet"},
30  {vpColormap::COLORMAP_MAGMA, "Colormap Magma"},
31  {vpColormap::COLORMAP_OCEAN, "Colormap Ocean"},
32  {vpColormap::COLORMAP_PLASMA, "Colormap Plasma"},
33  {vpColormap::COLORMAP_RAINBOW, "Colormap Rainbow"},
34  {vpColormap::COLORMAP_SPRING, "Colormap Spring"},
35  {vpColormap::COLORMAP_SUMMER, "Colormap Summer"},
36  {vpColormap::COLORMAP_TERRAIN, "Colormap Terrain"},
37  {vpColormap::COLORMAP_TURBO, "Colormap Turbo"},
38  {vpColormap::COLORMAP_TWILIGHT, "Colormap Twilight"},
39  {vpColormap::COLORMAP_TWILIGHT_SHIFTED, "Colormap Twilight Shifted"},
40  {vpColormap::COLORMAP_VIRIDIS, "Colormap Viridis"},
41  {vpColormap::COLORMAP_WINTER, "Colormap Winter"}
42  };
43 
44  std::vector<vpColormap::vpColormapType> colormaps = {
56  };
57 
58  // Apply a colormap on a 3-channel floating-point image
59  {
60  vpImage<vpRGBf> Irgbf;
61  vpImageIo::readPFM_HDR(Irgbf, "memorial.pfm");
62 
63  vpImage<vpRGBa> Icolor(Irgbf.getHeight(), Irgbf.getWidth());
64  vpImage<vpRGBa> Icolor2(Irgbf.getHeight() * 2, Irgbf.getWidth() * 2);
65 
66 #if defined(VISP_HAVE_X11)
67  vpDisplayX d(Icolor2, 10, 10, "Memorial");
68 #elif defined(VISP_HAVE_GDI)
69  vpDisplayGDI d(Icolor2, 10, 10, "Memorial");
70 #elif defined(HAVE_OPENCV_HIGHGUI)
71  vpDisplayOpenCV d(Icolor2, 10, 10, "Memorial");
72 #else
73  std::cout << "No image viewer is available..." << std::endl;
74  return EXIT_SUCCESS;
75 #endif
76 
77  vpFont font(20);
78  for (size_t i = 0; i < colormaps.size(); i++) {
79  vpColormap colormap(colormaps[i]);
80  colormap.convert(Irgbf, Icolor);
82 
83  font.drawText(Icolor2, colormaps_str[colormaps[i]], vpImagePoint(20, 20), vpColor::black, vpColor::white);
84 
85  vpDisplay::display(Icolor2);
86  vpDisplay::flush(Icolor2);
87  vpDisplay::getClick(Icolor2);
88  }
89  }
90 
91  // Apply a colormap on a 8-bit RGB image
92  {
94  vpImageIo::read(I, "monkey.png");
95 
96  vpImage<vpRGBa> Icolor(I.getHeight(), I.getWidth());
97  vpImage<vpRGBa> Icolor2(I.getHeight() * 2, I.getWidth() * 2);
98 
99 #if defined(VISP_HAVE_X11)
100  vpDisplayX d(Icolor2, 10, 10, "Monkey");
101 #elif defined(VISP_HAVE_GDI)
102  vpDisplayGDI d(Icolor2, 10, 10, "Monkey");
103 #elif defined(HAVE_OPENCV_HIGHGUI)
104  vpDisplayOpenCV d(Icolor2, 10, 10, "Monkey");
105 #else
106  std::cout << "No image viewer is available..." << std::endl;
107  return EXIT_SUCCESS;
108 #endif
109 
110  vpFont font(20);
111  for (size_t i = 0; i < colormaps.size(); i++) {
112  vpColormap colormap(colormaps[i]);
113  colormap.convert(I, Icolor);
115 
116  font.drawText(Icolor2, colormaps_str[colormaps[i]], vpImagePoint(20, 20), vpColor::black, vpColor::white);
117 
118  vpDisplay::display(Icolor2);
119  vpDisplay::flush(Icolor2);
120  vpDisplay::getClick(Icolor2);
121  }
122  }
123 
124  // Apply a colormap on a 8-bit RGB image, with normalization to the [0 - 255] range
125  {
126  vpImage<vpRGBa> I;
127  vpImageIo::read(I, "monkey.png");
128 
129  vpImage<vpRGBa> Icolor(I.getHeight(), I.getWidth());
130  vpImage<vpRGBa> Icolor2(I.getHeight() * 2, I.getWidth() * 2);
131 
132 #if defined(VISP_HAVE_X11)
133  vpDisplayX d(Icolor2, 10, 10, "Monkey");
134 #elif defined(VISP_HAVE_GDI)
135  vpDisplayGDI d(Icolor2, 10, 10, "Monkey");
136 #elif defined(HAVE_OPENCV_HIGHGUI)
137  vpDisplayOpenCV d(Icolor2, 10, 10, "Monkey");
138 #else
139  std::cout << "No image viewer is available..." << std::endl;
140  return EXIT_SUCCESS;
141 #endif
142 
143  vpFont font(20);
144  for (size_t i = 0; i < colormaps.size(); i++) {
145  vpColormap colormap(colormaps[i]);
146  const bool normalise = true;
147  colormap.convert(I, Icolor, normalise);
149 
150  font.drawText(Icolor2, colormaps_str[colormaps[i]], vpImagePoint(20, 20), vpColor::black, vpColor::white);
151 
152  vpDisplay::display(Icolor2);
153  vpDisplay::flush(Icolor2);
154  vpDisplay::getClick(Icolor2);
155  }
156  }
157  }
158  catch (const vpException &e) {
159  std::cerr << "Catch an exception: " << e << std::endl;
160  }
161 #else
162  std::cout << "This tutorial should be built with c++11 support" << std::endl;
163 #endif
164  return EXIT_SUCCESS;
165 }
static const vpColor white
Definition: vpColor.h:212
static const vpColor black
Definition: vpColor.h:211
Creates a colormap class to be able to recolor an image with different grayscale values into some cor...
Definition: vpColormap.h:58
@ COLORMAP_OCEAN
Definition: vpColormap.h:73
@ COLORMAP_MAGMA
Definition: vpColormap.h:72
@ COLORMAP_TWILIGHT
Definition: vpColormap.h:80
@ COLORMAP_VIRIDIS
Definition: vpColormap.h:82
@ COLORMAP_COOL
Definition: vpColormap.h:64
@ COLORMAP_RAINBOW
Definition: vpColormap.h:75
@ COLORMAP_GNUPLOT
Definition: vpColormap.h:66
@ COLORMAP_INFERNO
Definition: vpColormap.h:70
@ COLORMAP_GIST_EARTH
Definition: vpColormap.h:65
@ COLORMAP_TWILIGHT_SHIFTED
Definition: vpColormap.h:81
@ COLORMAP_AUTUMN
Definition: vpColormap.h:62
@ COLORMAP_CIVIDIS
Definition: vpColormap.h:63
@ COLORMAP_SPRING
Definition: vpColormap.h:76
@ COLORMAP_JET
Definition: vpColormap.h:71
@ COLORMAP_HOT
Definition: vpColormap.h:68
@ COLORMAP_TURBO
Definition: vpColormap.h:79
@ COLORMAP_PLASMA
Definition: vpColormap.h:74
@ COLORMAP_TERRAIN
Definition: vpColormap.h:78
@ COLORMAP_HSV
Definition: vpColormap.h:69
@ COLORMAP_GNUPLOT2
Definition: vpColormap.h:67
@ COLORMAP_WINTER
Definition: vpColormap.h:83
@ COLORMAP_SUMMER
Definition: vpColormap.h:77
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
Font drawing functions for image.
Definition: vpFont.h:55
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
static void readPFM_HDR(vpImage< float > &I, const std::string &filename)
Definition: vpImageIo.cpp:1351
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
static void resize(const vpImage< Type > &I, vpImage< Type > &Ires, unsigned int width, unsigned int height, const vpImageInterpolationType &method=INTERPOLATION_NEAREST, unsigned int nThreads=0)
@ INTERPOLATION_LINEAR
Definition: vpImageTools.h:81
Definition of the vpImage class member functions.
Definition: vpImage.h:131
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181