Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testUndistortImage.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 for image undistortion.
32 */
33
34#include <stdlib.h>
35#include <visp3/core/vpDebug.h>
36#include <visp3/core/vpImage.h>
37#include <visp3/core/vpImageTools.h>
38#include <visp3/core/vpIoTools.h>
39#include <visp3/core/vpRGBa.h>
40#include <visp3/core/vpTime.h>
41#include <visp3/io/vpImageIo.h>
42#include <visp3/io/vpParseArgv.h>
43
51
52// List of allowed command line options
53#define GETOPTARGS "cdi:o:t:s:h"
54
55#ifdef ENABLE_VISP_NAMESPACE
56using namespace VISP_NAMESPACE_NAME;
57#endif
58
59void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &opath,
60 const std::string &user);
61bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, const std::string &user,
62 unsigned int &nThreads, unsigned int &scale);
63
64/*
65 Print the program options.
66
67 \param name : Program name.
68 \param badparam : Bad parameter name.
69 \param ipath : Input image path.
70 \param opath : Output image path.
71 \param user : Username.
72 */
73void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &opath,
74 const std::string &user)
75{
76#if defined(VISP_HAVE_DATASET)
77#if VISP_HAVE_DATASET_VERSION >= 0x030600
78 std::string ext("png");
79#else
80 std::string ext("pgm");
81#endif
82#else
83 // We suppose that the user will download a recent dataset
84 std::string ext("png");
85#endif
86 fprintf(stdout, "\n\
87Read an image from the disk, undistort it \n\
88and save the undistorted image on the disk.\n\
89(grid36-01_undistorted.pgm).\n\
90\n\
91SYNOPSIS\n\
92 %s [-i <input image path>] [-o <output image path>] [-t <nThreads>] [-s <scale>]\n\
93 [-h]\n\
94 ",
95 name);
96
97 fprintf(stdout, "\n\
98OPTIONS: Default\n\
99 -i <input image path> %s\n\
100 Set image input path.\n\
101 From this path read \"calibration/grid36-01.%s\"\n\
102 image.\n\
103 Setting the VISP_INPUT_IMAGE_PATH environment\n\
104 variable produces the same behaviour than using\n\
105 this option.\n\
106 \n\
107 -o <output image path> %s\n\
108 Set image output path.\n\
109 From this directory, creates the \"%s\"\n\
110 subdirectory depending on the username, where \n\
111 grid36-01_undistorted.pgm output image is written.\n\
112\n\
113 -t <nThreads> \n\
114 Set the number of threads to use for vpImageTools::undistort().\n\
115\n\
116 -s <scale> \n\
117 Resize the image by the specified scale factor.\n\
118\n\
119 -h\n\
120 Print the help.\n\n",
121 ext.c_str(), ipath.c_str(), opath.c_str(), user.c_str());
122
123 if (badparam)
124 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
125}
126
139bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, const std::string &user,
140 unsigned int &nThreads, unsigned int &scale)
141{
142 const char *optarg_;
143 int c;
144 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
145
146 switch (c) {
147 case 'i':
148 ipath = optarg_;
149 break;
150 case 'o':
151 opath = optarg_;
152 break;
153 case 't':
154 nThreads = atoi(optarg_);
155 break;
156 case 's':
157 scale = atoi(optarg_);
158 break;
159 case 'h':
160 usage(argv[0], nullptr, ipath, opath, user);
161 return false;
162
163 case 'c':
164 case 'd':
165 break;
166
167 default:
168 usage(argv[0], optarg_, ipath, opath, user);
169 return false;
170 }
171 }
172
173 if ((c == 1) || (c == -1)) {
174 // standalone param or error
175 usage(argv[0], nullptr, ipath, opath, user);
176 std::cerr << "ERROR: " << std::endl;
177 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
178 return false;
179 }
180
181 return true;
182}
183
184int main(int argc, const char **argv)
185{
186 try {
187 std::string env_ipath;
188 std::string opt_ipath;
189 std::string opt_opath;
190 std::string ipath;
191 std::string opath;
192 std::string filename;
193 std::string username;
194 unsigned int nThreads = 2;
195 unsigned int scale = 1;
196
197#if defined(VISP_HAVE_DATASET)
198#if VISP_HAVE_DATASET_VERSION >= 0x030600
199 std::string ext("png");
200#else
201 std::string ext("pgm");
202#endif
203#else
204 // We suppose that the user will download a recent dataset
205 std::string ext("png");
206#endif
207
208 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
209 // environment variable value
211
212 // Set the default input path
213 if (!env_ipath.empty())
214 ipath = env_ipath;
215
216 // Set the default output path
217#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
218 opt_opath = "/tmp";
219#elif defined(_WIN32)
220 opt_opath = "C:\\temp";
221#endif
222
223 // Get the user login name
224 vpIoTools::getUserName(username);
225
226 // Read the command line options
227 if (getOptions(argc, argv, opt_ipath, opt_opath, username, nThreads, scale) == false) {
228 return EXIT_FAILURE;
229 }
230
231 // Get the option values
232 if (!opt_ipath.empty())
233 ipath = opt_ipath;
234 if (!opt_opath.empty())
235 opath = opt_opath;
236
237 // Append to the output path string, the login name of the user
238 opath = vpIoTools::createFilePath(opath, username);
239
240 // Test if the output path exist. If no try to create it
241 if (vpIoTools::checkDirectory(opath) == false) {
242 try {
243 // Create the dirname
245 }
246 catch (...) {
247 usage(argv[0], nullptr, ipath, opt_opath, username);
248 std::cerr << std::endl << "ERROR:" << std::endl;
249 std::cerr << " Cannot create " << opath << std::endl;
250 std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
251 return EXIT_FAILURE;
252 }
253 }
254
255 // Compare ipath and env_ipath. If they differ, we take into account
256 // the input path coming from the command line option
257 if (opt_ipath.empty()) {
258 if (ipath != env_ipath) {
259 std::cout << std::endl << "WARNING: " << std::endl;
260 std::cout << " Since -i <visp image path=" << ipath << "> "
261 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
262 << " we skip the environment variable." << std::endl;
263 }
264 }
265
266 // Test if an input path is set
267 if (opt_ipath.empty() && env_ipath.empty()) {
268 usage(argv[0], nullptr, ipath, opt_opath, username);
269 std::cerr << std::endl << "ERROR:" << std::endl;
270 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
271 << " environment variable to specify the location of the " << std::endl
272 << " image path where test images are located." << std::endl
273 << std::endl;
274 return EXIT_FAILURE;
275 }
276
277 //
278 // Here starts really the test
279 //
280 vpImage<vpRGBa> I, I_; // Input image
282 vpImage<vpRGBa> U; // undistorted output image
283 vpImage<unsigned char> U_gray; // undistorted output image
284 vpImage<vpRGBa> U_remap; // undistorted output image
285 vpImage<unsigned char> U_remap_gray; // undistorted output image
286
288 cam.initPersProjWithDistortion(600, 600, 320, 240, -0.17, 0.17);
289 // Read the input grey image from the disk
290 filename = vpIoTools::createFilePath(ipath, "calibration/grid36-01." + ext);
291 std::cout << "Read image: " << filename << std::endl;
292 vpImageIo::read(I_, filename);
293 if (scale > 1) {
294 std::cout << "Scale the image by a factor of " << scale << std::endl;
295 vpImageTools::resize(I_, I, I_.getWidth() * scale, I_.getHeight() * scale);
296 }
297 else {
298 I = I_;
299 }
300 std::cout << "Input image: " << I.getWidth() << "x" << I.getHeight() << std::endl;
301 vpImageConvert::convert(I, I_gray);
302
303 std::cout << "Nb threads to use for vpImageTools::undistort(): " << nThreads << std::endl;
304
305 double t_undistort = 0.0, t_remap = 0.0;
306 {
307 std::cout << "\nUndistortion in process (color image)... " << std::endl;
308 vpImageTools::undistort(I, cam, U, nThreads);
309
310 double begintime = vpTime::measureTimeMs();
311
312 // For the test, to have a significant time measure we repeat the
313 // undistortion 10 times
314 for (unsigned int i = 0; i < 10; i++)
315 // Create the undistorted image
316 vpImageTools::undistort(I, cam, U, nThreads);
317
318 double endtime = vpTime::measureTimeMs();
319 t_undistort = endtime - begintime;
320
321 std::cout << "Time for 10 color image undistortion (ms): " << t_undistort << std::endl;
322 }
323
324 {
325 std::cout << "Undistortion in process with remap (color image)... " << std::endl;
326
327 double begintime = vpTime::measureTimeMs();
328
329 // For the test, to have a significant time measure we repeat the
330 // undistortion 10 times
331 vpArray2D<int> mapU, mapV;
332 vpArray2D<float> mapDu, mapDv;
333 for (unsigned int i = 0; i < 10; i++) {
334 if (i == 0) {
335 vpImageTools::initUndistortMap(cam, I.getWidth(), I.getHeight(), mapU, mapV, mapDu, mapDv);
336 }
337 vpImageTools::remap(I, mapU, mapV, mapDu, mapDv, U_remap);
338 }
339
340 double endtime = vpTime::measureTimeMs();
341 t_remap = endtime - begintime;
342
343 std::cout << "Time for 10 color image undistortion with remap (ms): " << t_remap << std::endl;
344 std::cout << "Speed-up: " << t_undistort / t_remap << "X" << std::endl;
345 }
346
347 {
348 std::cout << "\nUndistortion in process (gray image)... " << std::endl;
349 vpImageTools::undistort(I_gray, cam, U_gray, nThreads);
350
351 double begintime = vpTime::measureTimeMs();
352
353 // For the test, to have a significant time measure we repeat the
354 // undistortion 100 times
355 for (unsigned int i = 0; i < 100; i++)
356 // Create the undistorted image
357 vpImageTools::undistort(I_gray, cam, U_gray, nThreads);
358
359 double endtime = vpTime::measureTimeMs();
360 t_undistort = endtime - begintime;
361
362 std::cout << "Time for 100 gray image undistortion (ms): " << t_undistort << std::endl;
363 }
364
365 {
366 std::cout << "Undistortion in process with remap (gray image)... " << std::endl;
367
368 double begintime = vpTime::measureTimeMs();
369
370 // For the test, to have a significant time measure we repeat the
371 // undistortion 100 times
372 vpArray2D<int> mapU, mapV;
373 vpArray2D<float> mapDu, mapDv;
374 for (unsigned int i = 0; i < 10; i++) {
375 if (i == 0) {
376 vpImageTools::initUndistortMap(cam, I.getWidth(), I.getHeight(), mapU, mapV, mapDu, mapDv);
377 }
378 vpImageTools::remap(I_gray, mapU, mapV, mapDu, mapDv, U_remap_gray);
379 }
380
381 double endtime = vpTime::measureTimeMs();
382 t_remap = endtime - begintime;
383
384 std::cout << "Time for 100 gray image undistortion with remap (ms): " << t_remap << std::endl;
385 std::cout << "Speed-up: " << t_undistort / t_remap << "X" << std::endl;
386 }
387
388 // Write the undistorted images on the disk
389 filename = vpIoTools::path(vpIoTools::createFilePath(opath, "grid36-01_undistorted_color.png"));
390 std::cout << "\nWrite undistorted image: " << filename << std::endl;
391 vpImageIo::write(U, filename);
392
393 filename = vpIoTools::path(vpIoTools::createFilePath(opath, "grid36-01_undistorted_gray.png"));
394 std::cout << "Write undistorted image: " << filename << std::endl;
395 vpImageIo::write(U_gray, filename);
396
397 filename = vpIoTools::path(vpIoTools::createFilePath(opath, "grid36-01_undistorted_remap_color.png"));
398 std::cout << "\nWrite undistorted image with remap: " << filename << std::endl;
399 vpImageIo::write(U_remap, filename);
400
401 filename = vpIoTools::path(vpIoTools::createFilePath(opath, "grid36-01_undistorted_remap_gray.png"));
402 std::cout << "Write undistorted image with remap: " << filename << std::endl;
403 vpImageIo::write(U_remap_gray, filename);
404
405 // Compute images difference
406 vpImage<vpRGBa> U_diff_abs;
407 vpImageTools::imageDifferenceAbsolute(U, U_remap, U_diff_abs);
408 double mean_diff = 0.0;
409 for (unsigned int i = 0; i < U_diff_abs.getHeight(); i++) {
410 for (unsigned int j = 0; j < U_diff_abs.getWidth(); j++) {
411 mean_diff += U_diff_abs[i][j].R;
412 mean_diff += U_diff_abs[i][j].G;
413 mean_diff += U_diff_abs[i][j].B;
414 mean_diff += U_diff_abs[i][j].A;
415 }
416 }
417 double remap_mean_error = mean_diff / (4 * U_diff_abs.getSize());
418 std::cout << "U_diff_abs mean value: " << remap_mean_error << std::endl;
419 const double remap_error_threshold = 0.5;
420 if (remap_mean_error > remap_error_threshold) {
421 std::cerr << "Issue with vpImageTools::remap() with vpRGBa image" << std::endl;
422 return EXIT_FAILURE;
423 }
424
425 vpImage<unsigned char> U_diff_gray_abs;
426 vpImageTools::imageDifferenceAbsolute(U_gray, U_remap_gray, U_diff_gray_abs);
427 double remap_mean_error_gray = U_diff_gray_abs.getSum() / U_diff_gray_abs.getSize();
428 std::cout << "U_diff_gray_abs mean value: " << remap_mean_error_gray << std::endl;
429 if (remap_mean_error_gray > remap_error_threshold) {
430 std::cerr << "Issue with vpImageTools::remap() with unsigned char image" << std::endl;
431 return EXIT_FAILURE;
432 }
433
434 // Write the undistorted difference images on the disk
435 vpImage<vpRGBa> U_diff;
436 vpImage<unsigned char> U_diff_gray;
437 vpImageTools::imageDifference(U, U_remap, U_diff);
438 filename = vpIoTools::path(vpIoTools::createFilePath(opath, "grid36-01_undistorted_diff_color.png"));
439 std::cout << "\nWrite undistorted image: " << filename << std::endl;
440 vpImageIo::write(U_diff, filename);
441
442 vpImageTools::imageDifference(U_gray, U_remap_gray, U_diff_gray);
443 filename = vpIoTools::path(vpIoTools::createFilePath(opath, "grid36-01_undistorted_diff_gray.png"));
444 std::cout << "Write undistorted image: " << filename << std::endl;
445 vpImageIo::write(U_diff_gray, filename);
446
447 return EXIT_SUCCESS;
448 }
449 catch (const vpException &e) {
450 std::cout << "Catch an exception: " << e << std::endl;
451 return EXIT_FAILURE;
452 }
453}
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition vpArray2D.h:146
Generic class defining intrinsic camera parameters.
error that can be emitted by ViSP classes.
Definition vpException.h:60
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)
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static void imageDifference(const vpImage< unsigned char > &I1, const vpImage< unsigned char > &I2, vpImage< unsigned char > &Idiff)
static void initUndistortMap(const vpCameraParameters &cam, unsigned int width, unsigned int height, vpArray2D< int > &mapU, vpArray2D< int > &mapV, vpArray2D< float > &mapDu, vpArray2D< float > &mapDv)
static void resize(const vpImage< Type > &I, vpImage< Type > &Ires, unsigned int width, unsigned int height, const vpImageInterpolationType &method=INTERPOLATION_NEAREST, unsigned int nThreads=0)
static void remap(const vpImage< unsigned char > &I, const vpArray2D< int > &mapU, const vpArray2D< int > &mapV, const vpArray2D< float > &mapDu, const vpArray2D< float > &mapDv, vpImage< unsigned char > &Iundist)
static void undistort(const vpImage< Type > &I, const vpCameraParameters &cam, vpImage< Type > &newI, unsigned int nThreads=2)
static void imageDifferenceAbsolute(const vpImage< unsigned char > &I1, const vpImage< unsigned char > &I2, vpImage< unsigned char > &Idiff)
Definition of the vpImage class member functions.
Definition vpImage.h:131
double getSum(const vpImage< bool > *p_mask=nullptr, unsigned int *nbValidPoints=nullptr) const
Compute the sum of image intensities.
unsigned int getWidth() const
Definition vpImage.h:242
unsigned int getSize() const
Definition vpImage.h:221
unsigned int getHeight() const
Definition vpImage.h:181
static std::string getViSPImagesDataPath()
static std::string path(const std::string &pathname)
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 double measureTimeMs()