Visual Servoing Platform  version 3.6.1 under development (2025-02-19)
tutorial-contrast-sharpening.cpp
1 
3 #include <cstdlib>
4 #include <iostream>
5 #include <visp3/core/vpConfig.h>
6 #include <visp3/core/vpImage.h>
7 #include <visp3/gui/vpDisplayFactory.h>
8 #include <visp3/io/vpImageIo.h>
9 
10 #if defined(VISP_HAVE_MODULE_IMGPROC)
12 #include <visp3/imgproc/vpImgproc.h>
14 #endif
15 
16 int main(int argc, const char **argv)
17 {
19 #if defined(VISP_HAVE_MODULE_IMGPROC) && defined(VISP_HAVE_DISPLAY)
22 
23 #ifdef ENABLE_VISP_NAMESPACE
24  using namespace VISP_NAMESPACE_NAME;
25 #endif
26 
27  std::string input_filename = "Crayfish-low-contrast.png";
28  int blockRadius = 150;
29  int bins = 256;
30  float slope = 3.0f;
31  float sigma = 2.0f;
32  double weight = 0.5;
33 
34  for (int i = 1; i < argc; i++) {
35  if (std::string(argv[i]) == "--input" && i + 1 < argc) {
36  input_filename = std::string(argv[i + 1]);
37  }
38  else if (std::string(argv[i]) == "--blockRadius" && i + 1 < argc) {
39  blockRadius = atoi(argv[i + 1]);
40  }
41  else if (std::string(argv[i]) == "--bins" && i + 1 < argc) {
42  bins = atoi(argv[i + 1]);
43  }
44  else if (std::string(argv[i]) == "--slope" && i + 1 < argc) {
45  slope = (float)atof(argv[i + 1]);
46  }
47  else if (std::string(argv[i]) == "--sigma" && i + 1 < argc) {
48  sigma = (float)atof(argv[i + 1]);
49  }
50  else if (std::string(argv[i]) == "--weight" && i + 1 < argc) {
51  weight = atof(argv[i + 1]);
52  }
53  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
54  std::cout << "Usage: " << argv[0]
55  << " [--input <input image>]"
56  " [--blockRadius <block radius for CLAHE>] "
57  " [--bins <nb histogram bins for CLAHE>] [--slope <slope for CLAHE>]"
58  " [--sigma <Gaussian kernel standard deviation>] [--weight <unsharp mask weighting>]"
59  " [--help] [-h]"
60  << std::endl;
61  return EXIT_SUCCESS;
62  }
63  }
64 
66  vpImage<vpRGBa> I_color;
67  vpImageIo::read(I_color, input_filename);
69 
70 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
71  std::shared_ptr<vpDisplay> display = vpDisplayFactory::createDisplay(I_color, 0, 0, "Input color image");
72 #else
73  vpDisplay *display = vpDisplayFactory::allocateDisplay(I_color, 0, 0, "Input color image");
74 #endif
75 
77  vpImage<vpRGBa> I_stretch;
78  VISP_NAMESPACE_NAME::stretchContrast(I_color, I_stretch);
80 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
81  std::shared_ptr<vpDisplay> display2 = vpDisplayFactory::createDisplay(I_stretch, I_color.getWidth(), 10, "Stretch contrast");
82 #else
83  vpDisplay *display2 = vpDisplayFactory::allocateDisplay(I_stretch, I_color.getWidth(), 10, "Stretch contrast");
84 #endif
85 
87  vpImage<vpRGBa> I_stretch_hsv;
88  VISP_NAMESPACE_NAME::stretchContrastHSV(I_color, I_stretch_hsv);
90 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
91  std::shared_ptr<vpDisplay> display3 = vpDisplayFactory::createDisplay(I_stretch_hsv, 0, I_color.getHeight() + 80, "Stretch contrast HSV");
92 #else
93  vpDisplay *display3 = vpDisplayFactory::allocateDisplay(I_stretch_hsv, 0, I_color.getHeight() + 80, "Stretch contrast HSV");
94 #endif
95 
97  vpImage<vpRGBa> I_hist_eq;
98  VISP_NAMESPACE_NAME::equalizeHistogram(I_color, I_hist_eq);
100 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
101  std::shared_ptr<vpDisplay> display4 = vpDisplayFactory::createDisplay(I_hist_eq, I_color.getWidth(), I_color.getHeight() + 80, "Histogram equalization");
102 #else
103  vpDisplay *display4 = vpDisplayFactory::allocateDisplay(I_hist_eq, I_color.getWidth(), I_color.getHeight() + 80, "Histogram equalization");
104 #endif
105 
107  vpImage<vpRGBa> I_clahe;
108  VISP_NAMESPACE_NAME::clahe(I_color, I_clahe, blockRadius, bins, slope);
110 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
111  std::shared_ptr<vpDisplay> display5 = vpDisplayFactory::createDisplay(I_clahe, 0, 2 * I_color.getHeight() + 80, "CLAHE");
112 #else
113  vpDisplay *display5 = vpDisplayFactory::allocateDisplay(I_clahe, 0, 2 * I_color.getHeight() + 80, "CLAHE");
114 #endif
115 
117  vpImage<vpRGBa> I_unsharp;
118  VISP_NAMESPACE_NAME::unsharpMask(I_clahe, I_unsharp, sigma, weight);
120 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
121  std::shared_ptr<vpDisplay> display6 = vpDisplayFactory::createDisplay(I_unsharp, I_color.getWidth(), 2 * I_color.getHeight() + 80, "Unsharp mask");
122 #else
123  vpDisplay *display6 = vpDisplayFactory::allocateDisplay(I_unsharp, I_color.getWidth(), 2 * I_color.getHeight() + 80, "Unsharp mask");
124 #endif
125 
126  vpDisplay::display(I_color);
127  vpDisplay::display(I_stretch);
128  vpDisplay::display(I_stretch_hsv);
129  vpDisplay::display(I_hist_eq);
130  vpDisplay::display(I_clahe);
131  vpDisplay::display(I_unsharp);
132  vpDisplay::displayText(I_unsharp, 20, 20, "Click to quit.", vpColor::red);
133  vpDisplay::flush(I_color);
134  vpDisplay::flush(I_stretch);
135  vpDisplay::flush(I_stretch_hsv);
136  vpDisplay::flush(I_hist_eq);
137  vpDisplay::flush(I_clahe);
138  vpDisplay::flush(I_unsharp);
139  vpDisplay::getClick(I_unsharp);
140 
141 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11) && defined(VISP_HAVE_DISPLAY)
142  if (display != nullptr) {
143  delete display;
144  }
145 
146  if (display2 != nullptr) {
147  delete display2;
148  }
149 
150  if (display3 != nullptr) {
151  delete display3;
152  }
153 
154  if (display4 != nullptr) {
155  delete display4;
156  }
157 
158  if (display5 != nullptr) {
159  delete display5;
160  }
161 
162  if (display6 != nullptr) {
163  delete display6;
164  }
165 #endif
166 #else
167  (void)argc;
168  (void)argv;
169 #endif
170  return EXIT_SUCCESS;
171 }
static const vpColor red
Definition: vpColor.h:198
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
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:147
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
VISP_EXPORT void clahe(const VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I1, VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I2, int blockRadius=150, int bins=256, float slope=3.0f, bool fast=true)
VISP_EXPORT void stretchContrast(VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I)
VISP_EXPORT void stretchContrastHSV(VISP_NAMESPACE_ADDRESSING vpImage< VISP_NAMESPACE_ADDRESSING vpRGBa > &I)
VISP_EXPORT void equalizeHistogram(VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I, const VISP_NAMESPACE_ADDRESSING vpImage< bool > *p_mask=nullptr)
VISP_EXPORT void unsharpMask(VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I, float sigma, double weight=0.6)
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.