Visual Servoing Platform  version 3.5.1 under development (2022-12-02)
tutorial-brightness-adjustment.cpp
1 
3 #include <cstdlib>
4 #include <iostream>
5 #include <visp3/core/vpImage.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 #if defined(VISP_HAVE_MODULE_IMGPROC)
13 #include <visp3/imgproc/vpImgproc.h>
15 #endif
16 
17 int main(int argc, const char **argv)
18 {
20 #if defined(VISP_HAVE_MODULE_IMGPROC) && \
21  (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)) && \
22  (defined(VISP_HAVE_PNG) || defined(VISP_HAVE_OPENCV))
25  std::string input_filename = "Sample_low_brightness.png";
26  double alpha = 10.0, beta = 50.0;
27  double gamma = 3.5;
28  int scale = 240, scaleDiv = 3, level = 0, kernelSize = -1;
29  double dynamic = 3.0;
30 
31  for (int i = 1; i < argc; i++) {
32  if (std::string(argv[i]) == "--input" && i + 1 < argc) {
33  input_filename = std::string(argv[i + 1]);
34  } else if (std::string(argv[i]) == "--alpha" && i + 1 < argc) {
35  alpha = atof(argv[i + 1]);
36  } else if (std::string(argv[i]) == "--beta" && i + 1 < argc) {
37  beta = atof(argv[i + 1]);
38  } else if (std::string(argv[i]) == "--gamma" && i + 1 < argc) {
39  gamma = atof(argv[i + 1]);
40  } else if (std::string(argv[i]) == "--scale" && i + 1 < argc) {
41  scale = atoi(argv[i + 1]);
42  } else if (std::string(argv[i]) == "--scaleDiv" && i + 1 < argc) {
43  scaleDiv = atoi(argv[i + 1]);
44  } else if (std::string(argv[i]) == "--level" && i + 1 < argc) {
45  level = atoi(argv[i + 1]);
46  } else if (std::string(argv[i]) == "--kernelSize" && i + 1 < argc) {
47  kernelSize = atoi(argv[i + 1]);
48  } else if (std::string(argv[i]) == "--dynamic" && i + 1 < argc) {
49  dynamic = atof(argv[i + 1]);
50  } else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
51  std::cout << "Usage: " << argv[0]
52  << " [--input <input image>]"
53  " [--alpha <alpha for vp::adjust()>] [--beta <beta for "
54  "vp::adjust()>]"
55  " [--gamma <gamma for vp::gammaCorrection()>]"
56  " [--scale <scale for vp::retinex()> [--scaleDiv for "
57  "vp::retinex()]"
58  " [--level <level for vp::retinex()> [--kernelSize "
59  "<kernelSize for vp::retinex()>]"
60  " [--dynamic <dynamic for vp::retinex()>] [--help]"
61  << std::endl;
62  return EXIT_SUCCESS;
63  }
64  }
65 
66  vpImage<vpRGBa> I_color;
67  vpImageIo::read(I_color, input_filename);
68 
69  vpImage<vpRGBa> I_color_res(I_color.getHeight(), 2 * I_color.getWidth());
70  I_color_res.insert(I_color, vpImagePoint());
71 #ifdef VISP_HAVE_X11
72  vpDisplayX d(I_color_res);
73 #elif defined(VISP_HAVE_GDI)
74  vpDisplayGDI d(I_color_res);
75 #elif defined(VISP_HAVE_OPENCV)
76  vpDisplayOpenCV d(I_color_res);
77 #endif
78 
80  vpImage<vpRGBa> I_color_adjust;
81  vp::adjust(I_color, I_color_adjust, alpha, beta);
83  I_color_res.insert(I_color_adjust, vpImagePoint(0, I_color.getWidth()));
84  std::stringstream ss;
85  ss << "Sample_low_brightness_alpha=" << alpha << "_beta=" << beta << ".png";
86  vpImageIo::write(I_color_res, ss.str());
87 
88  vpDisplay::display(I_color_res);
89  vpDisplay::displayText(I_color_res, 20, 20, "Brightness and contrast adjustment. Click to continue.", vpColor::red);
90  vpDisplay::flush(I_color_res);
91  vpDisplay::getClick(I_color_res);
92 
94  vpImage<vpRGBa> I_color_gamma_correction;
95  vp::gammaCorrection(I_color, I_color_gamma_correction, gamma);
97  I_color_res.insert(I_color_gamma_correction, vpImagePoint(0, I_color.getWidth()));
98  ss.str("");
99  ss << "Sample_low_brightness_gamma=" << gamma << ".png";
100  vpImageIo::write(I_color_res, ss.str());
101 
102  vpDisplay::display(I_color_res);
103  vpDisplay::displayText(I_color_res, 20, 20, "Gamma correction. Click to continue.", vpColor::red);
104  vpDisplay::flush(I_color_res);
105  vpDisplay::getClick(I_color_res);
106 
108  vpImage<vpRGBa> I_color_equalize_histogram;
109  vp::equalizeHistogram(I_color, I_color_equalize_histogram);
111  I_color_res.insert(I_color_equalize_histogram, vpImagePoint(0, I_color.getWidth()));
112  ss.str("");
113  ss << "Sample_low_brightness_eqHist.png";
114  vpImageIo::write(I_color_res, ss.str());
115 
116  vpDisplay::display(I_color_res);
117  vpDisplay::displayText(I_color_res, 20, 20, "Histogram equalization. Click to continue.", vpColor::red);
118  vpDisplay::flush(I_color_res);
119  vpDisplay::getClick(I_color_res);
120 
122  vpImage<vpRGBa> I_color_retinex;
123  vp::retinex(I_color, I_color_retinex, scale, scaleDiv, level, dynamic, kernelSize);
125  I_color_res.insert(I_color_retinex, vpImagePoint(0, I_color.getWidth()));
126 
127  ss.str("");
128  ss << "Sample_low_brightness_scale=" << scale << "_scaleDiv=" << scaleDiv << "_level=" << level
129  << "_dynamic=" << dynamic << "_kernelSize=" << kernelSize << ".png";
130  vpImageIo::write(I_color_res, ss.str());
131 
132  vpDisplay::display(I_color_res);
133  vpDisplay::displayText(I_color_res, 20, 20, "Retinex. Click to quit.", vpColor::red);
134  vpDisplay::flush(I_color_res);
135  vpDisplay::getClick(I_color_res);
136 
137  return EXIT_SUCCESS;
138 #else
139  (void)argc;
140  (void)argv;
141  return 0;
142 #endif
143 }
static const vpColor red
Definition: vpColor.h:217
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
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:135
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)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:148
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:292
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:89
unsigned int getWidth() const
Definition: vpImage.h:246
unsigned int getHeight() const
Definition: vpImage.h:188
VISP_EXPORT void adjust(vpImage< unsigned char > &I, double alpha, double beta)
Definition: vpImgproc.cpp:81
VISP_EXPORT void gammaCorrection(vpImage< unsigned char > &I, double gamma)
Definition: vpImgproc.cpp:335
VISP_EXPORT void equalizeHistogram(vpImage< unsigned char > &I)
Definition: vpImgproc.cpp:165
VISP_EXPORT void retinex(vpImage< vpRGBa > &I, int scale=240, int scaleDiv=3, int level=RETINEX_UNIFORM, double dynamic=1.2, int kernelSize=-1)
Definition: vpRetinex.cpp:263