Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
tutorial-brightness-adjustment.cpp
1 
3 #include <cstdlib>
4 #include <iostream>
5 #include <visp3/core/vpConfig.h>
6 #include <visp3/core/vpImage.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 #if defined(VISP_HAVE_MODULE_IMGPROC)
14 #include <visp3/imgproc/vpImgproc.h>
16 #endif
17 
18 int main(int argc, const char **argv)
19 {
21 #if defined(VISP_HAVE_MODULE_IMGPROC) && \
22  (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)) && \
23  (defined(VISP_HAVE_PNG) || defined(VISP_HAVE_OPENCV))
26 #ifdef ENABLE_VISP_NAMESPACE
27  using namespace VISP_NAMESPACE_NAME;
28 #endif
29 
30  std::string input_filename = "Sample_low_brightness.png";
31  double alpha = 10.0, beta = 50.0;
32  double gamma = 3.5;
35  int scale = 240, scaleDiv = 3, level = 0, kernelSize = -1;
36  double dynamic = 3.0;
37 
38  for (int i = 1; i < argc; i++) {
39  if (std::string(argv[i]) == "--input" && i + 1 < argc) {
40  input_filename = std::string(argv[i + 1]);
41  }
42  else if (std::string(argv[i]) == "--alpha" && i + 1 < argc) {
43  alpha = atof(argv[i + 1]);
44  }
45  else if (std::string(argv[i]) == "--beta" && i + 1 < argc) {
46  beta = atof(argv[i + 1]);
47  }
48  else if (std::string(argv[i]) == "--gamma" && i + 1 < argc) {
49  gamma = atof(argv[i + 1]);
50  }
51  else if ((std::string(argv[i]) == "--gamma-color-handling") && ((i + 1) < argc)) {
52  ++i;
54  }
55  else if ((std::string(argv[i]) == "--gamma-method") && ((i + 1) < argc)) {
56  ++i;
58  }
59  else if (std::string(argv[i]) == "--scale" && i + 1 < argc) {
60  scale = atoi(argv[i + 1]);
61  }
62  else if (std::string(argv[i]) == "--scaleDiv" && i + 1 < argc) {
63  scaleDiv = atoi(argv[i + 1]);
64  }
65  else if (std::string(argv[i]) == "--level" && i + 1 < argc) {
66  level = atoi(argv[i + 1]);
67  }
68  else if (std::string(argv[i]) == "--kernelSize" && i + 1 < argc) {
69  kernelSize = atoi(argv[i + 1]);
70  }
71  else if (std::string(argv[i]) == "--dynamic" && i + 1 < argc) {
72  dynamic = atof(argv[i + 1]);
73  }
74  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
75  std::cout << "Usage: " << argv[0]
76  << " [--input <input image>]"
77  " [--alpha <alpha for VISP_NAMESPACE_NAME::adjust()>] [--beta <beta for "
78  "VISP_NAMESPACE_NAME::adjust()>]"
79  " [--gamma <gamma for VISP_NAMESPACE_NAME::gammaCorrection()>]"
80  " [--gamma-color-handling " << VISP_NAMESPACE_NAME::vpGammaColorHandlingList() << "]"
81  " [--gamma-method " << VISP_NAMESPACE_NAME::vpGammaMethodList() << "]"
82  " [--scale <scale for VISP_NAMESPACE_NAME::retinex()> [--scaleDiv for "
83  "VISP_NAMESPACE_NAME::retinex()]"
84  " [--level <level for VISP_NAMESPACE_NAME::retinex()> [--kernelSize "
85  "<kernelSize for VISP_NAMESPACE_NAME::retinex()>]"
86  " [--dynamic <dynamic for VISP_NAMESPACE_NAME::retinex()>] [--help]"
87  << std::endl;
88  return EXIT_SUCCESS;
89  }
90  }
91 
92  vpImage<vpRGBa> I_color;
93  vpImageIo::read(I_color, input_filename);
95  vpImageConvert::convert(I_color, I_gray);
96 
97  vpImage<vpRGBa> I_color_res(I_color.getHeight(), 2 * I_color.getWidth());
98  I_color_res.insert(I_color, vpImagePoint());
99  vpImage<unsigned char> I_gray_res(I_gray.getHeight(), 2 * I_gray.getWidth());
100  I_gray_res.insert(I_gray, vpImagePoint());
101 #ifdef VISP_HAVE_X11
102  vpDisplayX d_gray(I_gray_res);
103  vpDisplayX d(I_color_res);
104 #elif defined(VISP_HAVE_GDI)
105  vpDisplayGDI d_gray(I_gray_res);
106  vpDisplayGDI d(I_color_res);
107 #elif defined(HAVE_OPENCV_HIGHGUI)
108  vpDisplayOpenCV d_gray(I_gray_res);
109  vpDisplayOpenCV d(I_color_res);
110 #endif
111 
113  vpImage<vpRGBa> I_color_adjust;
114  VISP_NAMESPACE_NAME::adjust(I_color, I_color_adjust, alpha, beta);
116  I_color_res.insert(I_color_adjust, vpImagePoint(0, I_color.getWidth()));
117  std::stringstream ss;
118  ss << "Sample_low_brightness_alpha=" << alpha << "_beta=" << beta << ".png";
119  vpImageIo::write(I_color_res, ss.str());
120 
121  vpDisplay::display(I_color_res);
122  vpDisplay::displayText(I_color_res, 20, 20, "Brightness and contrast adjustment. Click to continue.", vpColor::red);
123  vpDisplay::flush(I_color_res);
124  vpDisplay::getClick(I_color_res);
125 
127  if (method != VISP_NAMESPACE_NAME::GAMMA_MANUAL) {
128  // If the user wants to use an automatic method, the gamma factor must be negative.
129  gamma = -1.;
130  }
131 
132  if (gamma > 0.) {
133  // If the user wants to set a constant user-defined gamma factor, the method must be set to manual.
135  }
136  vpImage<unsigned char> I_gray_gamma_correction;
137  VISP_NAMESPACE_NAME::gammaCorrection(I_gray, I_gray_gamma_correction, static_cast<float>(gamma), method);
138  vpImage<vpRGBa> I_color_gamma_correction;
139  VISP_NAMESPACE_NAME::gammaCorrection(I_color, I_color_gamma_correction, static_cast<float>(gamma), colorHandling, method);
141  I_gray_res.insert(I_gray_gamma_correction, vpImagePoint(0, I_gray.getWidth()));
142  ss.str("");
143  ss << "Sample_low_brightness_gray.png";
144  vpImageIo::write(I_gray_res, ss.str());
145 
146  vpDisplay::display(I_gray_res);
147  vpDisplay::displayText(I_gray_res, 20, 20, "Gamma correction on gray image. Click to continue.", vpColor::red);
148  vpDisplay::flush(I_gray_res);
149  vpDisplay::getClick(I_gray_res);
150 
151  I_color_res.insert(I_color_gamma_correction, vpImagePoint(0, I_color.getWidth()));
152  ss.str("");
153  ss << "Sample_low_brightness_gamma=" << gamma << ".png";
154  vpImageIo::write(I_color_res, ss.str());
155 
156  vpDisplay::display(I_color_res);
157  vpDisplay::displayText(I_color_res, 20, 20, "Gamma correction. Click to continue.", vpColor::red);
158  vpDisplay::flush(I_color_res);
159  vpDisplay::getClick(I_color_res);
160 
162  vpImage<vpRGBa> I_color_equalize_histogram;
163  VISP_NAMESPACE_NAME::equalizeHistogram(I_color, I_color_equalize_histogram);
165  I_color_res.insert(I_color_equalize_histogram, vpImagePoint(0, I_color.getWidth()));
166  ss.str("");
167  ss << "Sample_low_brightness_eqHist.png";
168  vpImageIo::write(I_color_res, ss.str());
169 
170  vpDisplay::display(I_color_res);
171  vpDisplay::displayText(I_color_res, 20, 20, "Histogram equalization. Click to continue.", vpColor::red);
172  vpDisplay::flush(I_color_res);
173  vpDisplay::getClick(I_color_res);
174 
176  vpImage<vpRGBa> I_color_retinex;
177  VISP_NAMESPACE_NAME::retinex(I_color, I_color_retinex, scale, scaleDiv, level, dynamic, kernelSize);
179  I_color_res.insert(I_color_retinex, vpImagePoint(0, I_color.getWidth()));
180 
181  ss.str("");
182  ss << "Sample_low_brightness_scale=" << scale << "_scaleDiv=" << scaleDiv << "_level=" << level
183  << "_dynamic=" << dynamic << "_kernelSize=" << kernelSize << ".png";
184  vpImageIo::write(I_color_res, ss.str());
185 
186  vpDisplay::display(I_color_res);
187  vpDisplay::displayText(I_color_res, 20, 20, "Retinex. Click to quit.", vpColor::red);
188  vpDisplay::flush(I_color_res);
189  vpDisplay::getClick(I_color_res);
190 #else
191  (void)argc;
192  (void)argv;
193 #endif
194  return EXIT_SUCCESS;
195 }
static const vpColor red
Definition: vpColor.h:217
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...
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 convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:291
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
VISP_EXPORT void adjust(VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I, double alpha, double beta)
VISP_EXPORT void gammaCorrection(VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I, const float &gamma, const vpGammaMethod &method=GAMMA_MANUAL, const VISP_NAMESPACE_ADDRESSING vpImage< bool > *p_mask=nullptr)
VISP_EXPORT void equalizeHistogram(VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I, const VISP_NAMESPACE_ADDRESSING vpImage< bool > *p_mask=nullptr)
VISP_EXPORT void retinex(VISP_NAMESPACE_ADDRESSING vpImage< VISP_NAMESPACE_ADDRESSING vpRGBa > &I, int scale=240, int scaleDiv=3, int level=RETINEX_UNIFORM, double dynamic=1.2, int kernelSize=-1)
VISP_EXPORT vpGammaMethod vpGammaMethodFromString(const std::string &name)
Cast a string into a vpGammaMethod.
Definition: vpImgproc.cpp:116
VISP_EXPORT vpGammaColorHandling vpGammaColorHandlingFromString(const std::string &name)
Cast a string into a vpGammaColorHandling.
Definition: vpImgproc.cpp:164
vpGammaColorHandling
How to handle color images when applying Gamma Correction.
Definition: vpImgproc.h:149
VISP_EXPORT std::string vpGammaMethodList(const std::string &pref="<", const std::string &sep=" , ", const std::string &suf=">")
Get the list of available vpGammaMethod.
Definition: vpImgproc.cpp:73
VISP_EXPORT std::string vpGammaColorHandlingList(const std::string &pref="<", const std::string &sep=" , ", const std::string &suf=">")
Get the list of available vpGammaColorHandling.
Definition: vpImgproc.cpp:133
vpGammaMethod
Gamma Correction automatic methods.
Definition: vpImgproc.h:100