Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testImgproc.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Test imgproc functions.
32 */
33
39
40#include <cstdio>
41#include <cstdlib>
42#include <visp3/core/vpImage.h>
43#include <visp3/core/vpIoTools.h>
44#include <visp3/core/vpMath.h>
45#include <visp3/imgproc/vpImgproc.h>
46#include <visp3/io/vpImageIo.h>
47#include <visp3/io/vpParseArgv.h>
48
49// List of allowed command line options
50#define GETOPTARGS "cdi:o:h"
51
52#ifdef ENABLE_VISP_NAMESPACE
53using namespace VISP_NAMESPACE_NAME;
54#endif
55
56void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &opath, const std::string &user);
57bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user);
58
59/*
60 Print the program options.
61
62 \param name : Program name.
63 \param badparam : Bad parameter name.
64 \param ipath : Input image path.
65 \param opath : Output image path.
66 \param user : Username.
67 */
68void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &opath, const std::string &user)
69{
70 fprintf(stdout, "\n\
71Test imgproc functions.\n\
72\n\
73SYNOPSIS\n\
74 %s [-i <input image path>] [-o <output image path>]\n\
75 [-h]\n \
76",
77name);
78
79 fprintf(stdout, "\n\
80OPTIONS: Default\n\
81 -i <input image path> %s\n\
82 Set image input path.\n\
83 From this path read \"Klimt/Klimt.pgm\"\n\
84 image.\n\
85 Setting the VISP_INPUT_IMAGE_PATH environment\n\
86 variable produces the same behaviour than using\n\
87 this option.\n\
88\n\
89 -o <output image path> %s\n\
90 Set image output path.\n\
91 From this directory, creates the \"%s\"\n\
92 subdirectory depending on the username, where \n\
93 Klimt_grey.pgm output image is written.\n\
94\n\
95 -h\n\
96 Print the help.\n\n",
97 ipath.c_str(), opath.c_str(), user.c_str());
98
99 if (badparam)
100 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
101}
102
113bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user)
114{
115 const char *optarg_;
116 int c;
117 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
118
119 switch (c) {
120 case 'i':
121 ipath = optarg_;
122 break;
123 case 'o':
124 opath = optarg_;
125 break;
126 case 'h':
127 usage(argv[0], nullptr, ipath, opath, user);
128 return false;
129
130 case 'c':
131 case 'd':
132 break;
133
134 default:
135 usage(argv[0], optarg_, ipath, opath, user);
136 return false;
137 }
138 }
139
140 if ((c == 1) || (c == -1)) {
141 // standalone param or error
142 usage(argv[0], nullptr, ipath, opath, user);
143 std::cerr << "ERROR: " << std::endl;
144 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
145 return false;
146 }
147
148 return true;
149}
150
151int main(int argc, const char **argv)
152{
153 try {
154 std::string env_ipath;
155 std::string opt_ipath;
156 std::string opt_opath;
157 std::string ipath;
158 std::string opath;
159 std::string filename;
160 std::string username;
161
162#if defined(VISP_HAVE_DATASET)
163#if VISP_HAVE_DATASET_VERSION >= 0x030600
164 std::string ext("png");
165#else
166 std::string ext("pgm");
167#endif
168#else
169 // We suppose that the user will download a recent dataset
170 std::string ext("png");
171#endif
172
173 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
174 // environment variable value
176
177 // Set the default input path
178 if (!env_ipath.empty())
179 ipath = env_ipath;
180
181// Set the default output path
182#if defined(_WIN32)
183 opt_opath = "C:/temp";
184#else
185 opt_opath = "/tmp";
186#endif
187
188 // Get the user login name
189 vpIoTools::getUserName(username);
190
191 // Read the command line options
192 if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
193 return EXIT_FAILURE;
194 }
195
196 // Get the option values
197 if (!opt_ipath.empty())
198 ipath = opt_ipath;
199 if (!opt_opath.empty())
200 opath = opt_opath;
201
202 // Append to the output path string, the login name of the user
203 opath = vpIoTools::createFilePath(opath, username);
204
205 // Test if the output path exist. If no try to create it
206 if (vpIoTools::checkDirectory(opath) == false) {
207 try {
208 // Create the dirname
210 }
211 catch (...) {
212 usage(argv[0], nullptr, ipath, opt_opath, username);
213 std::cerr << std::endl << "ERROR:" << std::endl;
214 std::cerr << " Cannot create " << opath << std::endl;
215 std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
216 return EXIT_FAILURE;
217 }
218 }
219
220 // Compare ipath and env_ipath. If they differ, we take into account
221 // the input path coming from the command line option
222 if (!opt_ipath.empty() && !env_ipath.empty()) {
223 if (ipath != env_ipath) {
224 std::cout << std::endl << "WARNING: " << std::endl;
225 std::cout << " Since -i <visp image path=" << ipath << "> "
226 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
227 << " we skip the environment variable." << std::endl;
228 }
229 }
230
231 // Test if an input path is set
232 if (opt_ipath.empty() && env_ipath.empty()) {
233 usage(argv[0], nullptr, ipath, opt_opath, username);
234 std::cerr << std::endl << "ERROR:" << std::endl;
235 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
236 << " environment variable to specify the location of the " << std::endl
237 << " image path where test images are located." << std::endl
238 << std::endl;
239 return EXIT_FAILURE;
240 }
241
242 //
243 // Here starts really the test
244 //
245
246 //
247 // Test color functions using Klimt.ppm
248 //
249
250 // Read Klimt.ppm
251 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
252 vpImage<vpRGBa> I_color, Iinput_color;
253 std::cout << "Read image: " << filename << std::endl;
254 vpImageIo::read(Iinput_color, filename);
255 Iinput_color.halfSizeImage(I_color);
256 std::cout << "Image: " << I_color.getWidth() << "x" << I_color.getHeight() << std::endl;
257
258 // Adjust
259 double alpha = 1.5, beta = -10.0;
260 vpImage<vpRGBa> I_color_adjust;
261 double t = vpTime::measureTimeMs();
262 VISP_NAMESPACE_NAME::adjust(I_color, I_color_adjust, alpha, beta);
264 std::cout << "Time to do color adjust: " << t << " ms" << std::endl;
265
266 // Save adjust
267 filename = vpIoTools::createFilePath(opath, "Klimt_adjust.ppm");
268 vpImageIo::write(I_color_adjust, filename);
269
270 // Equalize Histogram
271 vpImage<vpRGBa> I_color_equalize_histogram;
273 VISP_NAMESPACE_NAME::equalizeHistogram(I_color, I_color_equalize_histogram);
275 std::cout << "Time to do color histogram equalization: " << t << " ms" << std::endl;
276
277 // Save equalizeHistogram
278 filename = vpIoTools::createFilePath(opath, "Klimt_equalize_histogram.ppm");
279 vpImageIo::write(I_color_equalize_histogram, filename);
280
281 // Gamma correction
282 vpImage<vpRGBa> I_color_gamma_correction;
283 float gamma = 2.2f;
285 VISP_NAMESPACE_NAME::gammaCorrection(I_color, I_color_gamma_correction, gamma);
287 std::cout << "Time to do color gamma correction: " << t << " ms" << std::endl;
288
289 // Save gammaCorrection
290 filename = vpIoTools::createFilePath(opath, "Klimt_gamma_correction.ppm");
291 vpImageIo::write(I_color_gamma_correction, filename);
292
293 // Retinex
294 vpImage<vpRGBa> I_color_retinex;
296 VISP_NAMESPACE_NAME::retinex(I_color, I_color_retinex);
298 std::cout << "Time to do color retinex: " << t << " ms" << std::endl;
299
300 // Save retinex
301 filename = vpIoTools::createFilePath(opath, "Klimt_retinex.ppm");
302 vpImageIo::write(I_color_retinex, filename);
303
304 // Stretch contrast
305 vpImage<vpRGBa> I_color_stretch_contrast;
307 VISP_NAMESPACE_NAME::stretchContrast(I_color, I_color_stretch_contrast);
309 std::cout << "Time to do color contrast stretching: " << t << " ms" << std::endl;
310
311 // Save stretchContrast
312 filename = vpIoTools::createFilePath(opath, "Klimt_stretch_contrast.ppm");
313 vpImageIo::write(I_color_stretch_contrast, filename);
314
315 // Stretch Contrast HSV
316 vpImage<vpRGBa> I_color_stretch_contrast_HSV;
318 VISP_NAMESPACE_NAME::stretchContrastHSV(I_color, I_color_stretch_contrast_HSV);
320 std::cout << "Time to do color HSV contrast stretching: " << t << " ms" << std::endl;
321
322 // Save stretchContrastHSV
323 filename = vpIoTools::createFilePath(opath, "Klimt_stretch_contrast_HSV.ppm");
324 vpImageIo::write(I_color_stretch_contrast_HSV, filename);
325
326 // Unsharp Mask
327 vpImage<vpRGBa> I_color_unsharp_mask;
328 const float sigma = 1.0f;
330 VISP_NAMESPACE_NAME::unsharpMask(I_color, I_color_unsharp_mask, sigma);
332 std::cout << "Time to do color unsharp mask: " << t << " ms" << std::endl;
333
334 // Save unsharpMask
335 filename = vpIoTools::createFilePath(opath, "Klimt_unsharp_mask.ppm");
336 vpImageIo::write(I_color_unsharp_mask, filename);
337
338// CLAHE
339 vpImage<vpRGBa> I_color_clahe;
341 VISP_NAMESPACE_NAME::clahe(I_color, I_color_clahe, 50);
343 std::cout << "Time to do color CLAHE: " << t << " ms" << std::endl;
344
345 // Save CLAHE
346 filename = vpIoTools::createFilePath(opath, "Klimt_CLAHE.ppm");
347 vpImageIo::write(I_color_clahe, filename);
348
349 //
350 // Test grayscale function using image0000.png
351 //
352
353 // Read image0000.png
354 filename = vpIoTools::createFilePath(ipath, "mbt/cube/image0000." + ext);
355 vpImage<unsigned char> Iinput, I;
356 std::cout << "\nRead image: " << filename << std::endl;
357 vpImageIo::read(Iinput, filename);
358 Iinput.halfSizeImage(I);
359 std::cout << "Image: " << I.getWidth() << "x" << I.getHeight() << std::endl;
360
361 // Adjust
362 vpImage<unsigned char> I_adjust;
363 beta = -20.0;
365 VISP_NAMESPACE_NAME::adjust(I, I_adjust, alpha, beta);
367 std::cout << "Time to do grayscale adjust: " << t << " ms" << std::endl;
368
369 // Save adjust
370 filename = vpIoTools::createFilePath(opath, "image0000_adjust.png");
371 vpImageIo::write(I_adjust, filename);
372
373 // Equalize Histogram
374 vpImage<unsigned char> I_equalize_histogram;
376 VISP_NAMESPACE_NAME::equalizeHistogram(I, I_equalize_histogram);
378 std::cout << "Time to do grayscale histogram equalization: " << t << " ms" << std::endl;
379
380 // Save equalizeHistogram
381 filename = vpIoTools::createFilePath(opath, "image0000_equalize_histogram.png");
382 vpImageIo::write(I_equalize_histogram, filename);
383
384 // Gamma correction
385 vpImage<unsigned char> I_gamma_correction;
386 gamma = 1.8f;
388 VISP_NAMESPACE_NAME::gammaCorrection(I, I_gamma_correction, gamma);
390 std::cout << "Time to do grayscale gamma correction: " << t << " ms" << std::endl;
391
392 // Save gammaCorrection
393 filename = vpIoTools::createFilePath(opath, "image0000_gamma_correction.png");
394 vpImageIo::write(I_gamma_correction, filename);
395
396 // Stretch contrast
397 vpImage<unsigned char> I_stretch_contrast;
399 VISP_NAMESPACE_NAME::stretchContrast(I, I_stretch_contrast);
401 std::cout << "Time to do grayscale contrast stretching: " << t << " ms" << std::endl;
402
403 // Save stretchContrast
404 filename = vpIoTools::createFilePath(opath, "image0000_stretch_contrast.png");
405 vpImageIo::write(I_stretch_contrast, filename);
406
407 // Unsharp Mask
408 vpImage<unsigned char> I_unsharp_mask;
410 VISP_NAMESPACE_NAME::unsharpMask(I, I_unsharp_mask, sigma);
412 std::cout << "Time to do grayscale unsharp mask: " << t << " ms" << std::endl;
413
414 // Save unsharpMask
415 filename = vpIoTools::createFilePath(opath, "image0000_unsharp_mask.png");
416 vpImageIo::write(I_unsharp_mask, filename);
417
418 // CLAHE
421 VISP_NAMESPACE_NAME::clahe(I, I_clahe, 50);
423 std::cout << "Time to do grayscale CLAHE: " << t << " ms" << std::endl;
424
425 // Save CLAHE
426 filename = vpIoTools::createFilePath(opath, "image0000_CLAHE.png");
427 vpImageIo::write(I_clahe, filename);
428
429 return EXIT_SUCCESS;
430 }
431 catch (const vpException &e) {
432 std::cerr << "Catch an exception: " << e.what() << std::endl;
433 return EXIT_FAILURE;
434 }
435}
error that can be emitted by ViSP classes.
Definition vpException.h:60
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition of the vpImage class member functions.
Definition vpImage.h:131
void halfSizeImage(vpImage< Type > &res) const
Definition vpImage.h:725
unsigned int getWidth() const
Definition vpImage.h:242
unsigned int getHeight() const
Definition vpImage.h:181
static std::string getViSPImagesDataPath()
static bool checkDirectory(const std::string &dirname)
static std::string getUserName()
static std::string createFilePath(const std::string &parent, const std::string &child)
static void makeDirectory(const std::string &dirname)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
VISP_EXPORT void adjust(VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I, double alpha, double beta)
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 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 void unsharpMask(VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I, float sigma, double weight=0.6)
VISP_EXPORT double measureTimeMs()