Visual Servoing Platform  version 3.6.1 under development (2024-05-07)
tutorial-image-colormap.cpp
1 
2 #include <map>
3 #include <visp3/core/vpColormap.h>
4 #include <visp3/core/vpFont.h>
5 #include <visp3/core/vpImageTools.h>
6 #include <visp3/gui/vpDisplayGDI.h>
7 #include <visp3/gui/vpDisplayOpenCV.h>
8 #include <visp3/gui/vpDisplayX.h>
9 #include <visp3/io/vpImageIo.h>
10 
11 int main()
12 {
13 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
14  try {
15  std::map<vpColormap::vpColormapType, std::string> colormaps_str = {
16  {vpColormap::COLORMAP_AUTUMN, "Colormap Autumn"},
17  {vpColormap::COLORMAP_CIVIDIS, "Colormap Cividis"},
18  {vpColormap::COLORMAP_COOL, "Colormap Cool"},
19  {vpColormap::COLORMAP_GIST_EARTH, "Colormap Gist Earth"},
20  {vpColormap::COLORMAP_GNUPLOT, "Colormap Gnuplot"},
21  {vpColormap::COLORMAP_GNUPLOT2, "Colormap Gnuplot2"},
22  {vpColormap::COLORMAP_HOT, "Colormap Hot"},
23  {vpColormap::COLORMAP_HSV, "Colormap HSV"},
24  {vpColormap::COLORMAP_INFERNO, "Colormap Inferno"},
25  {vpColormap::COLORMAP_JET, "Colormap Jet"},
26  {vpColormap::COLORMAP_MAGMA, "Colormap Magma"},
27  {vpColormap::COLORMAP_OCEAN, "Colormap Ocean"},
28  {vpColormap::COLORMAP_PLASMA, "Colormap Plasma"},
29  {vpColormap::COLORMAP_RAINBOW, "Colormap Rainbow"},
30  {vpColormap::COLORMAP_SPRING, "Colormap Spring"},
31  {vpColormap::COLORMAP_SUMMER, "Colormap Summer"},
32  {vpColormap::COLORMAP_TERRAIN, "Colormap Terrain"},
33  {vpColormap::COLORMAP_TURBO, "Colormap Turbo"},
34  {vpColormap::COLORMAP_TWILIGHT, "Colormap Twilight"},
35  {vpColormap::COLORMAP_TWILIGHT_SHIFTED, "Colormap Twilight Shifted"},
36  {vpColormap::COLORMAP_VIRIDIS, "Colormap Viridis"},
37  {vpColormap::COLORMAP_WINTER, "Colormap Winter"}
38  };
39 
40  std::vector<vpColormap::vpColormapType> colormaps = {
52  };
53 
54  // Apply a colormap on a 3-channel floating-point image
55  {
56  vpImage<vpRGBf> Irgbf;
57  vpImageIo::readPFM_HDR(Irgbf, "memorial.pfm");
58 
59  vpImage<vpRGBa> Icolor(Irgbf.getHeight(), Irgbf.getWidth());
60  vpImage<vpRGBa> Icolor2(Irgbf.getHeight() * 2, Irgbf.getWidth() * 2);
61 
62 #if defined(VISP_HAVE_X11)
63  vpDisplayX d(Icolor2, 10, 10, "Memorial");
64 #elif defined(VISP_HAVE_GDI)
65  vpDisplayGDI d(Icolor2, 10, 10, "Memorial");
66 #elif defined(HAVE_OPENCV_HIGHGUI)
67  vpDisplayOpenCV d(Icolor2, 10, 10, "Memorial");
68 #else
69  std::cout << "No image viewer is available..." << std::endl;
70  return EXIT_SUCCESS;
71 #endif
72 
73  vpFont font(20);
74  for (size_t i = 0; i < colormaps.size(); i++) {
75  vpColormap colormap(colormaps[i]);
76  colormap.convert(Irgbf, Icolor);
78 
79  font.drawText(Icolor2, colormaps_str[colormaps[i]], vpImagePoint(20, 20), vpColor::black, vpColor::white);
80 
81  vpDisplay::display(Icolor2);
82  vpDisplay::flush(Icolor2);
83  vpDisplay::getClick(Icolor2);
84  }
85  }
86 
87  // Apply a colormap on a 8-bit RGB image
88  {
90  vpImageIo::read(I, "monkey.png");
91 
92  vpImage<vpRGBa> Icolor(I.getHeight(), I.getWidth());
93  vpImage<vpRGBa> Icolor2(I.getHeight() * 2, I.getWidth() * 2);
94 
95 #if defined(VISP_HAVE_X11)
96  vpDisplayX d(Icolor2, 10, 10, "Monkey");
97 #elif defined(VISP_HAVE_GDI)
98  vpDisplayGDI d(Icolor2, 10, 10, "Monkey");
99 #elif defined(HAVE_OPENCV_HIGHGUI)
100  vpDisplayOpenCV d(Icolor2, 10, 10, "Monkey");
101 #else
102  std::cout << "No image viewer is available..." << std::endl;
103  return EXIT_SUCCESS;
104 #endif
105 
106  vpFont font(20);
107  for (size_t i = 0; i < colormaps.size(); i++) {
108  vpColormap colormap(colormaps[i]);
109  colormap.convert(I, Icolor);
111 
112  font.drawText(Icolor2, colormaps_str[colormaps[i]], vpImagePoint(20, 20), vpColor::black, vpColor::white);
113 
114  vpDisplay::display(Icolor2);
115  vpDisplay::flush(Icolor2);
116  vpDisplay::getClick(Icolor2);
117  }
118  }
119 
120  // Apply a colormap on a 8-bit RGB image, with normalization to the [0 - 255] range
121  {
122  vpImage<vpRGBa> I;
123  vpImageIo::read(I, "monkey.png");
124 
125  vpImage<vpRGBa> Icolor(I.getHeight(), I.getWidth());
126  vpImage<vpRGBa> Icolor2(I.getHeight() * 2, I.getWidth() * 2);
127 
128 #if defined(VISP_HAVE_X11)
129  vpDisplayX d(Icolor2, 10, 10, "Monkey");
130 #elif defined(VISP_HAVE_GDI)
131  vpDisplayGDI d(Icolor2, 10, 10, "Monkey");
132 #elif defined(HAVE_OPENCV_HIGHGUI)
133  vpDisplayOpenCV d(Icolor2, 10, 10, "Monkey");
134 #else
135  std::cout << "No image viewer is available..." << std::endl;
136  return EXIT_SUCCESS;
137 #endif
138 
139  vpFont font(20);
140  for (size_t i = 0; i < colormaps.size(); i++) {
141  vpColormap colormap(colormaps[i]);
142  const bool normalise = true;
143  colormap.convert(I, Icolor, normalise);
145 
146  font.drawText(Icolor2, colormaps_str[colormaps[i]], vpImagePoint(20, 20), vpColor::black, vpColor::white);
147 
148  vpDisplay::display(Icolor2);
149  vpDisplay::flush(Icolor2);
150  vpDisplay::getClick(Icolor2);
151  }
152  }
153  }
154  catch (const vpException &e) {
155  std::cerr << "Catch an exception: " << e << std::endl;
156  }
157 #else
158  std::cout << "This tutorial should be built with c++11 support" << std::endl;
159 #endif
160  return EXIT_SUCCESS;
161 }
static const vpColor white
Definition: vpColor.h:206
static const vpColor black
Definition: vpColor.h:205
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:128
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
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:59
Font drawing functions for image.
Definition: vpFont.h:54
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:143
static void readPFM_HDR(vpImage< float > &I, const std::string &filename)
Definition: vpImageIo.cpp:1347
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:80
Definition of the vpImage class member functions.
Definition: vpImage.h:69
unsigned int getWidth() const
Definition: vpImage.h:245
unsigned int getHeight() const
Definition: vpImage.h:184