Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testImgproc.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2023 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
34#include <cstdio>
35#include <cstdlib>
36#include <visp3/core/vpImage.h>
37#include <visp3/core/vpIoTools.h>
38#include <visp3/core/vpMath.h>
39#include <visp3/imgproc/vpImgproc.h>
40#include <visp3/io/vpImageIo.h>
41#include <visp3/io/vpParseArgv.h>
42
49// List of allowed command line options
50#define GETOPTARGS "cdi:o:h"
51
52void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user);
53bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user);
54
55/*
56 Print the program options.
57
58 \param name : Program name.
59 \param badparam : Bad parameter name.
60 \param ipath: Input image path.
61 \param opath : Output image path.
62 \param user : Username.
63 */
64void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
65{
66 fprintf(stdout, "\n\
67Test imgproc functions.\n\
68\n\
69SYNOPSIS\n\
70 %s [-i <input image path>] [-o <output image path>]\n\
71 [-h]\n \
72",
73 name);
74
75 fprintf(stdout, "\n\
76OPTIONS: Default\n\
77 -i <input image path> %s\n\
78 Set image input path.\n\
79 From this path read \"Klimt/Klimt.pgm\"\n\
80 image.\n\
81 Setting the VISP_INPUT_IMAGE_PATH environment\n\
82 variable produces the same behaviour than using\n\
83 this option.\n\
84\n\
85 -o <output image path> %s\n\
86 Set image output path.\n\
87 From this directory, creates the \"%s\"\n\
88 subdirectory depending on the username, where \n\
89 Klimt_grey.pgm output image is written.\n\
90\n\
91 -h\n\
92 Print the help.\n\n",
93 ipath.c_str(), opath.c_str(), user.c_str());
94
95 if (badparam)
96 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
97}
98
109bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user)
110{
111 const char *optarg_;
112 int c;
113 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
114
115 switch (c) {
116 case 'i':
117 ipath = optarg_;
118 break;
119 case 'o':
120 opath = optarg_;
121 break;
122 case 'h':
123 usage(argv[0], NULL, ipath, opath, user);
124 return false;
125 break;
126
127 case 'c':
128 case 'd':
129 break;
130
131 default:
132 usage(argv[0], optarg_, ipath, opath, user);
133 return false;
134 break;
135 }
136 }
137
138 if ((c == 1) || (c == -1)) {
139 // standalone param or error
140 usage(argv[0], NULL, ipath, opath, user);
141 std::cerr << "ERROR: " << std::endl;
142 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
143 return false;
144 }
145
146 return true;
147}
148
149int main(int argc, const char **argv)
150{
151 try {
152 std::string env_ipath;
153 std::string opt_ipath;
154 std::string opt_opath;
155 std::string ipath;
156 std::string opath;
157 std::string filename;
158 std::string username;
159
160#if VISP_HAVE_DATASET_VERSION >= 0x030600
161 std::string ext("png");
162#else
163 std::string ext("pgm");
164#endif
165
166 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
167 // environment variable value
169
170 // Set the default input path
171 if (!env_ipath.empty())
172 ipath = env_ipath;
173
174// Set the default output path
175#if defined(_WIN32)
176 opt_opath = "C:/temp";
177#else
178 opt_opath = "/tmp";
179#endif
180
181 // Get the user login name
182 vpIoTools::getUserName(username);
183
184 // Read the command line options
185 if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
186 return EXIT_FAILURE;
187 }
188
189 // Get the option values
190 if (!opt_ipath.empty())
191 ipath = opt_ipath;
192 if (!opt_opath.empty())
193 opath = opt_opath;
194
195 // Append to the output path string, the login name of the user
196 opath = vpIoTools::createFilePath(opath, username);
197
198 // Test if the output path exist. If no try to create it
199 if (vpIoTools::checkDirectory(opath) == false) {
200 try {
201 // Create the dirname
203 } catch (...) {
204 usage(argv[0], NULL, ipath, opt_opath, username);
205 std::cerr << std::endl << "ERROR:" << std::endl;
206 std::cerr << " Cannot create " << opath << std::endl;
207 std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
208 return EXIT_FAILURE;
209 }
210 }
211
212 // Compare ipath and env_ipath. If they differ, we take into account
213 // the input path comming from the command line option
214 if (!opt_ipath.empty() && !env_ipath.empty()) {
215 if (ipath != env_ipath) {
216 std::cout << std::endl << "WARNING: " << std::endl;
217 std::cout << " Since -i <visp image path=" << ipath << "> "
218 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
219 << " we skip the environment variable." << std::endl;
220 }
221 }
222
223 // Test if an input path is set
224 if (opt_ipath.empty() && env_ipath.empty()) {
225 usage(argv[0], NULL, ipath, opt_opath, username);
226 std::cerr << std::endl << "ERROR:" << std::endl;
227 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
228 << " environment variable to specify the location of the " << std::endl
229 << " image path where test images are located." << std::endl
230 << std::endl;
231 return EXIT_FAILURE;
232 }
233
234 //
235 // Here starts really the test
236 //
237
238 //
239 // Test color functions using Klimt.ppm
240 //
241
242 // Read Klimt.ppm
243 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
244 vpImage<vpRGBa> I_color, Iinput_color;
245 std::cout << "Read image: " << filename << std::endl;
246 vpImageIo::read(Iinput_color, filename);
247 Iinput_color.halfSizeImage(I_color);
248 std::cout << "Image: " << I_color.getWidth() << "x" << I_color.getHeight() << std::endl;
249
250 // Adjust
251 double alpha = 1.5, beta = -10.0;
252 vpImage<vpRGBa> I_color_adjust;
253 double t = vpTime::measureTimeMs();
254 vp::adjust(I_color, I_color_adjust, alpha, beta);
255 t = vpTime::measureTimeMs() - t;
256 std::cout << "Time to do color adjust: " << t << " ms" << std::endl;
257
258 // Save adjust
259 filename = vpIoTools::createFilePath(opath, "Klimt_adjust.ppm");
260 vpImageIo::write(I_color_adjust, filename);
261
262 // Equalize Histogram
263 vpImage<vpRGBa> I_color_equalize_histogram;
265 vp::equalizeHistogram(I_color, I_color_equalize_histogram);
266 t = vpTime::measureTimeMs() - t;
267 std::cout << "Time to do color histogram equalization: " << t << " ms" << std::endl;
268
269 // Save equalizeHistogram
270 filename = vpIoTools::createFilePath(opath, "Klimt_equalize_histogram.ppm");
271 vpImageIo::write(I_color_equalize_histogram, filename);
272
273 // Gamma correction
274 vpImage<vpRGBa> I_color_gamma_correction;
275 double gamma = 2.2;
277 vp::gammaCorrection(I_color, I_color_gamma_correction, gamma);
278 t = vpTime::measureTimeMs() - t;
279 std::cout << "Time to do color gamma correction: " << t << " ms" << std::endl;
280
281 // Save gammaCorrection
282 filename = vpIoTools::createFilePath(opath, "Klimt_gamma_correction.ppm");
283 vpImageIo::write(I_color_gamma_correction, filename);
284
285 // Retinex
286 vpImage<vpRGBa> I_color_retinex;
288 vp::retinex(I_color, I_color_retinex);
289 t = vpTime::measureTimeMs() - t;
290 std::cout << "Time to do color retinex: " << t << " ms" << std::endl;
291
292 // Save retinex
293 filename = vpIoTools::createFilePath(opath, "Klimt_retinex.ppm");
294 vpImageIo::write(I_color_retinex, filename);
295
296 // Stretch contrast
297 vpImage<vpRGBa> I_color_stretch_contrast;
299 vp::stretchContrast(I_color, I_color_stretch_contrast);
300 t = vpTime::measureTimeMs() - t;
301 std::cout << "Time to do color contrast stretching: " << t << " ms" << std::endl;
302
303 // Save stretchContrast
304 filename = vpIoTools::createFilePath(opath, "Klimt_stretch_contrast.ppm");
305 vpImageIo::write(I_color_stretch_contrast, filename);
306
307 // Stretch Contrast HSV
308 vpImage<vpRGBa> I_color_stretch_contrast_HSV;
310 vp::stretchContrastHSV(I_color, I_color_stretch_contrast_HSV);
311 t = vpTime::measureTimeMs() - t;
312 std::cout << "Time to do color HSV contrast stretching: " << t << " ms" << std::endl;
313
314 // Save stretchContrastHSV
315 filename = vpIoTools::createFilePath(opath, "Klimt_stretch_contrast_HSV.ppm");
316 vpImageIo::write(I_color_stretch_contrast_HSV, filename);
317
318 // Unsharp Mask
319 vpImage<vpRGBa> I_color_unsharp_mask;
320 const float sigma = 1.0f;
322 vp::unsharpMask(I_color, I_color_unsharp_mask, sigma);
323 t = vpTime::measureTimeMs() - t;
324 std::cout << "Time to do color unsharp mask: " << t << " ms" << std::endl;
325
326 // Save unsharpMask
327 filename = vpIoTools::createFilePath(opath, "Klimt_unsharp_mask.ppm");
328 vpImageIo::write(I_color_unsharp_mask, filename);
329
330 // CLAHE
331 vpImage<vpRGBa> I_color_clahe;
333 vp::clahe(I_color, I_color_clahe, 50);
334 t = vpTime::measureTimeMs() - t;
335 std::cout << "Time to do color CLAHE: " << t << " ms" << std::endl;
336
337 // Save CLAHE
338 filename = vpIoTools::createFilePath(opath, "Klimt_CLAHE.ppm");
339 vpImageIo::write(I_color_clahe, filename);
340
341 //
342 // Test grayscale function using image0000.png
343 //
344
345 // Read image0000.png
346 filename = vpIoTools::createFilePath(ipath, "mbt/cube/image0000." + ext);
347 vpImage<unsigned char> Iinput, I;
348 std::cout << "\nRead image: " << filename << std::endl;
349 vpImageIo::read(Iinput, filename);
350 Iinput.halfSizeImage(I);
351 std::cout << "Image: " << I.getWidth() << "x" << I.getHeight() << std::endl;
352
353 // Adjust
354 vpImage<unsigned char> I_adjust;
355 beta = -20.0;
357 vp::adjust(I, I_adjust, alpha, beta);
358 t = vpTime::measureTimeMs() - t;
359 std::cout << "Time to do grayscale adjust: " << t << " ms" << std::endl;
360
361 // Save adjust
362 filename = vpIoTools::createFilePath(opath, "image0000_adjust.png");
363 vpImageIo::write(I_adjust, filename);
364
365 // Equalize Histogram
366 vpImage<unsigned char> I_equalize_histogram;
368 vp::equalizeHistogram(I, I_equalize_histogram);
369 t = vpTime::measureTimeMs() - t;
370 std::cout << "Time to do grayscale histogram equalization: " << t << " ms" << std::endl;
371
372 // Save equalizeHistogram
373 filename = vpIoTools::createFilePath(opath, "image0000_equalize_histogram.png");
374 vpImageIo::write(I_equalize_histogram, filename);
375
376 // Gamma correction
377 vpImage<unsigned char> I_gamma_correction;
378 gamma = 1.8;
380 vp::gammaCorrection(I, I_gamma_correction, gamma);
381 t = vpTime::measureTimeMs() - t;
382 std::cout << "Time to do grayscale gamma correction: " << t << " ms" << std::endl;
383
384 // Save gammaCorrection
385 filename = vpIoTools::createFilePath(opath, "image0000_gamma_correction.png");
386 vpImageIo::write(I_gamma_correction, filename);
387
388 // Stretch contrast
389 vpImage<unsigned char> I_stretch_contrast;
391 vp::stretchContrast(I, I_stretch_contrast);
392 t = vpTime::measureTimeMs() - t;
393 std::cout << "Time to do grayscale contrast stretching: " << t << " ms" << std::endl;
394
395 // Save stretchContrast
396 filename = vpIoTools::createFilePath(opath, "image0000_stretch_contrast.png");
397 vpImageIo::write(I_stretch_contrast, filename);
398
399 // Unsharp Mask
400 vpImage<unsigned char> I_unsharp_mask;
402 vp::unsharpMask(I, I_unsharp_mask, sigma);
403 t = vpTime::measureTimeMs() - t;
404 std::cout << "Time to do grayscale unsharp mask: " << t << " ms" << std::endl;
405
406 // Save unsharpMask
407 filename = vpIoTools::createFilePath(opath, "image0000_unsharp_mask.png");
408 vpImageIo::write(I_unsharp_mask, filename);
409
410 // CLAHE
413 vp::clahe(I, I_clahe, 50);
414 t = vpTime::measureTimeMs() - t;
415 std::cout << "Time to do grayscale CLAHE: " << t << " ms" << std::endl;
416
417 // Save CLAHE
418 filename = vpIoTools::createFilePath(opath, "image0000_CLAHE.png");
419 vpImageIo::write(I_clahe, filename);
420
421 return EXIT_SUCCESS;
422 } catch (const vpException &e) {
423 std::cerr << "Catch an exception: " << e.what() << std::endl;
424 return EXIT_FAILURE;
425 }
426}
error that can be emitted by ViSP classes.
Definition vpException.h:59
const char * what() const
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:135
void halfSizeImage(vpImage< Type > &res) const
Definition vpImage.h:1438
unsigned int getWidth() const
Definition vpImage.h:242
unsigned int getHeight() const
Definition vpImage.h:184
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 clahe(const vpImage< unsigned char > &I1, vpImage< unsigned char > &I2, int blockRadius=150, int bins=256, float slope=3.0f, bool fast=true)
Definition vpCLAHE.cpp:196
VISP_EXPORT void adjust(vpImage< unsigned char > &I, double alpha, double beta)
Definition vpImgproc.cpp:68
VISP_EXPORT void stretchContrast(vpImage< unsigned char > &I)
VISP_EXPORT void stretchContrastHSV(vpImage< vpRGBa > &I)
VISP_EXPORT void gammaCorrection(vpImage< unsigned char > &I, double gamma)
VISP_EXPORT void equalizeHistogram(vpImage< unsigned char > &I)
VISP_EXPORT void retinex(vpImage< vpRGBa > &I, int scale=240, int scaleDiv=3, int level=RETINEX_UNIFORM, double dynamic=1.2, int kernelSize=-1)
vp_deprecated VISP_EXPORT void unsharpMask(vpImage< unsigned char > &I, unsigned int size=7, double weight=0.6)
VISP_EXPORT double measureTimeMs()